summaryrefslogtreecommitdiffstats
path: root/testsuites/vstf/vstf_scripts/vstf/controller/reporters/reporter.py
blob: ea0a1ad0cb61c62d34349051f0c9885ef310db07 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
##############################################################################
# Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

import os
import argparse
import logging
import time

from vstf.controller.reporters.report.provider.html_provider import HtmlProvider
from vstf.controller.reporters.report.provider.pdf_provider import PdfProvider
from vstf.controller.settings.template_settings import TemplateSettings
from vstf.controller.reporters.report.data_factory import TaskData
from vstf.controller.reporters.report.html.htmlcreator import HtmlCreator
from vstf.controller.reporters.report.pdf.pdfcreator import PdfCreator
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.candy_generator import CandyGenerator
import vstf.common.constants as cst


LOG = logging.getLogger(__name__)


class Report(object):

    def __init__(self, dbase, rpath):
        """

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

    def create_pdf(self, taskid):
        task = TaskData(taskid, self._dbase)
        scenario_list = task.common.get_scenariolist()
        creator = CandyGenerator(task)
        attach_list = []
        for scenario in scenario_list:
            out_file = os.path.join(
                self._rpath, "vstf_report_%s_%s.pdf" %
                (scenario, time.strftime(
                    cst.TIME_FORMAT3)))
            LOG.info(out_file)
            creator.create(scenario)
            info = TemplateSettings()
            provider = PdfProvider(info.settings)
            reporter = PdfCreator(provider)
            reporter.create(out_file)
            attach_list.append(out_file)

        if attach_list:
            self._mail_settings.mset_attach(attach_list)

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

        creator = CandyGenerator(task)
        creator.create_all()

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

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

        html = HtmlCreator(provider)
        content = html.create(out_file)

        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()