summaryrefslogtreecommitdiffstats
path: root/vstf/vstf/controller/reporters/reporter.py
blob: 1c256c611eebbf2fa23627801aa992f1d8a56011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/python
# -*- coding: utf8 -*-
# author: wly
# date: 2015-05-29
# see license for license details
import os
import argparse
import logging
import time

from vstf.controller.reporters.report.pdf.pdfcreator import PdfvSwitchCreator
from vstf.controller.reporters.report.html.htmlcreator import HtmlvSwitchCreator
from vstf.controller.reporters.report.data_factory import CommonData, TaskData, ScenarioData, HistoryData
from vstf.controller.database.dbinterface import DbManage
from vstf.controller.settings.mail_settings import MailSettings
from vstf.controller.reporters.mail.sendmail import SendMail
from vstf.controller.settings.html_settings import HtmlSettings
from vstf.controller.reporters.report.provider.html_provider import StyleProvider
import vstf.common.constants as cst


__version__ = ''' '''
LOG = logging.getLogger(__name__)


class Report(object):
    def __init__(self, dbase, rpath):
        """

        :type dbase: object DbManage
        """
        self._dbase = dbase
        self._rpath = "."
        if os.path.exists(rpath):
            self._rpath = rpath

    def create_pdf(self, taskid):
        common_data = CommonData(taskid, self._dbase)
        scenario_list = common_data.get_scenariolist()
        history_data = HistoryData(taskid, self._dbase)
        attach_list = []
        for scenario in scenario_list:
            out_file = os.path.join(self._rpath, "vstf_report_%s_%s.pdf" % (scenario, time.strftime(cst.TIME_STR)))
            LOG.info(out_file)
            scenario_data = ScenarioData(taskid, self._dbase, scenario)
            pdf = PdfvSwitchCreator(out_file, common_data, scenario_data, history_data)
            if pdf:
                pdf.create()
                attach_list.append(out_file)
        if attach_list:
            self._mail_settings.mset_attach(attach_list)

    def create_html(self, taskid):
        task_data = TaskData(taskid, self._dbase)

        html_settings = HtmlSettings()
        LOG.info(html_settings.settings)

        provider = StyleProvider(html_settings.settings)
        out_file = os.path.join(self._rpath, "mail.html")
        LOG.info(out_file)

        html = HtmlvSwitchCreator(task_data, provider, out_file)
        content = html.create()

        self._mail_settings.mset_subtype('html')
        self._mail_settings.mset_content(content)

    def report(self, taskid, mail_off):
        self._mail_settings = MailSettings()
        mail = SendMail(self._mail_settings.settings)
        self.create_pdf(taskid)
        self.create_html(taskid)
        if not mail_off:
            mail.send()


def main():
    from vstf.common.log import setup_logging
    setup_logging(level=logging.DEBUG, log_file="/var/log/vstf/vstf-reporter.log", clevel=logging.INFO)

    parser = argparse.ArgumentParser(add_help=True)
    parser.add_argument('-rpath',
                        action='store',
                        default='./',
                        type=str,
                        help=" the path name of test results  "
                        )
    parser.add_argument('-mail_off',
                        action='store_true',
                        help="is need send mail the for the report"
                        )
    parser.add_argument('--taskid',
                        action='store',
                        default=-1,
                        help="report depand of a history task id."
                        )
    args = parser.parse_args()
    dbase = DbManage()

    report = Report(dbase, args.rpath)
    if args.taskid == -1:
        taskid = dbase.get_last_taskid()
    else:
        taskid = args.taskid
    report.report(taskid, args.mail_off)


if __name__ == '__main__':
    main()