JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / controller / reporters / reporter.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import os
11 import argparse
12 import logging
13 import time
14
15 from vstf.controller.reporters.report.provider.html_provider import HtmlProvider
16 from vstf.controller.reporters.report.provider.pdf_provider import PdfProvider
17 from vstf.controller.settings.template_settings import TemplateSettings
18 from vstf.controller.reporters.report.data_factory import TaskData
19 from vstf.controller.reporters.report.html.htmlcreator import HtmlCreator
20 from vstf.controller.reporters.report.pdf.pdfcreator import PdfCreator
21 from vstf.controller.database.dbinterface import DbManage
22 from vstf.controller.settings.mail_settings import MailSettings
23 from vstf.controller.reporters.mail.sendmail import SendMail
24 from vstf.controller.settings.html_settings import HtmlSettings
25 from vstf.controller.reporters.report.candy_generator import CandyGenerator
26 import vstf.common.constants as cst
27
28
29 LOG = logging.getLogger(__name__)
30
31
32 class Report(object):
33     def __init__(self, dbase, rpath):
34         """
35
36         :type dbase: object DbManage
37         """
38         self._dbase = dbase
39         self._rpath = "."
40         self._mail_settings = MailSettings()
41         if os.path.exists(rpath):
42             self._rpath = rpath
43
44     def create_pdf(self, taskid):
45         task = TaskData(taskid, self._dbase)
46         scenario_list = task.common.get_scenariolist()
47         creator = CandyGenerator(task)
48         attach_list = []
49         for scenario in scenario_list:
50             out_file = os.path.join(self._rpath, "vstf_report_%s_%s.pdf" % (scenario, time.strftime(cst.TIME_FORMAT3)))
51             LOG.info(out_file)
52             creator.create(scenario)
53             info = TemplateSettings()
54             provider = PdfProvider(info.settings)
55             reporter = PdfCreator(provider)
56             reporter.create(out_file)
57             attach_list.append(out_file)
58
59         if attach_list:
60             self._mail_settings.mset_attach(attach_list)
61
62     def create_html(self, taskid):
63         task = TaskData(taskid, self._dbase)
64
65         creator = CandyGenerator(task)
66         creator.create_all()
67
68         html_settings = HtmlSettings()
69         info = TemplateSettings()
70         LOG.info(html_settings.settings)
71
72         provider = HtmlProvider(info.settings, html_settings.settings)
73         out_file = os.path.join(self._rpath, "mail.html")
74         LOG.info(out_file)
75
76         html = HtmlCreator(provider)
77         content = html.create(out_file)
78
79         self._mail_settings.mset_subtype('html')
80         self._mail_settings.mset_content(content)
81
82     def report(self, taskid, mail_off):
83         self._mail_settings = MailSettings()
84         mail = SendMail(self._mail_settings.settings)
85         self.create_pdf(taskid)
86         self.create_html(taskid)
87         if not mail_off:
88             mail.send()
89
90
91 def main():
92     from vstf.common.log import setup_logging
93     setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-reporter.log", clevel=logging.INFO)
94
95     parser = argparse.ArgumentParser(add_help=True)
96     parser.add_argument('-rpath',
97                         action='store',
98                         default='./',
99                         type=str,
100                         help=" the path name of test results  "
101                         )
102     parser.add_argument('-mail_off',
103                         action='store_true',
104                         help="is need send mail the for the report"
105                         )
106     parser.add_argument('--taskid',
107                         action='store',
108                         default=-1,
109                         help="report depand of a history task id."
110                         )
111     args = parser.parse_args()
112     dbase = DbManage()
113
114     report = Report(dbase, args.rpath)
115     if args.taskid == -1:
116         taskid = dbase.get_last_taskid()
117     else:
118         taskid = args.taskid
119     report.report(taskid, args.mail_off)
120
121
122 if __name__ == '__main__':
123     main()