• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2023 Huawei Device Co., Ltd.
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#     http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19
20import sys
21import datetime
22from importlib import reload
23reload(sys)
24import os
25import stat
26
27FLAGS_WRITE = os.O_WRONLY | os.O_CREAT | os.O_EXCL
28FLAGS_ADD = os.O_WRONLY | os.O_APPEND | os.O_CREAT
29MODES = stat.S_IWUSR | stat.S_IRUSR
30
31
32html_head = """
33<!DOCTYPE html>
34<html lang="en" xmlns:th="http://www.thymeleaf.org">
35<head>
36    <meta charset="UTF-8"/>
37    <title>接口覆盖率邮件模板</title>
38    <style type="text/css">
39        table.reference, table.tecspec {
40            border-collapse: collapse;
41            width: 95%;
42            margin-bottom: 4px;
43            margin-top: 4px;
44            text-align: center;
45        }
46        table.reference tr:nth-child(even) {
47            background-color: #fff;
48        }
49        table.reference tr:nth-child(odd) {
50            background-color: #f6f4f0;
51        }
52        table.reference th {
53            color: #fff;
54            background-color: #14b8c7;
55            border: 1px solid #555;
56            font-size: 18px;
57            padding: 3px;
58            vertical-align: top;
59        }
60        table.reference td {
61            line-height: 1.5em;
62            min-width: 24px;
63            border: 1px solid #d4d4d4;
64            padding: 5px;
65            padding-top: 1px;
66            padding-bottom: 1px;
67            vertical-align: top;
68        }
69        .article-body h3 {
70            font-size: 1.8em;
71            margin: 2px 0;
72            line-height: 1.8em;
73        }
74
75    </style>
76</head>
77"""
78html_body_start = """
79<body>"""
80
81html_body_ended = """
82</body>"""
83html_ended = """
84</html>"""
85
86
87def sort_by_field_element(elem):
88    return int(float(elem[3][:-1]))   # @######value是浮点数,如1.0,那么需要先转float再转int
89
90
91def sort_by_field_element_data(elem):
92    return elem[2]
93
94
95def create_html_start(reportpath):
96    try:
97        if os.path.exists(reportpath):
98            os.remove(reportpath)
99        with os.fdopen(os.open(reportpath, FLAGS_WRITE, MODES), 'w') as report:
100            report.write(html_head)
101            report.write(html_body_start)
102    except(IOError, ValueError):
103        print("Error for create html start ",)
104
105
106def create_title(reportpath, title_name, summary_list):
107    currdatetime = datetime.date.today().strftime("%Y-%m-%d")
108    report_title = """
109    <h2 style="text-align: center;font-family: 微软雅黑">%s coverage report (%s)</h2>
110    """
111    content = report_title % (title_name, currdatetime)
112    report_title = content + """
113    <div><table align="center"><tbody>
114      <tr>
115          <th align='left'>
116          <h4 style="font-family: 微软雅黑;">Summary Report</h4>
117          <h4 style="font-family: 微软雅黑;">接口总数%s, 已覆盖%s, 未覆盖%s</h4>
118      </tr>
119   </tbody>
120   </table>
121   </div>
122    """
123    subsystems = ""
124    count = 0
125    for item in summary_list:
126        subsystem = item[0]
127        if count < 3:
128            subsystems = subsystems + "、" + subsystem
129            count = count + 1
130        if subsystem == "Summary":
131            nocoverd = item[1] - item[2]
132            report_title = report_title % (item[1], item[2], nocoverd)
133    try:
134        with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report:
135            report.write(report_title)
136    except(IOError, ValueError):
137        print("Error for create html title")
138
139
140def create_summary(reportpath, summary_list):
141    table_title = """
142        <h4 style="text-align: left;font-family: 微软雅黑;margin-left: 3%;">Summary</h4>
143        """
144
145    table_start = """<div><table class="reference" align="center"><tbody>"""
146
147    table_head = """
148            <tr>
149                <th style="width:20%;">PartName</th>
150                <th style="width:20%;">TotalCount</th>
151                <th style="width:20%;">CoveredCount</th>
152                <th style="width:20%;">Coverage</th>
153            </tr>"""
154
155    table_line = """
156            <tr class=normal>
157                <td>%s</td>
158                <td>%s</td>
159                <td>%s</td>
160                <td>%s</td>
161            </tr>
162        """
163
164    table_ended = """</tbody></table></div>
165        """
166    try:
167        if len(summary_list) == 0:
168            return
169
170        with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report:
171            report.write(table_title)
172            report.write(table_start)
173            report.write(table_head)
174            for item in summary_list:
175                content = table_line % (item[0], str(item[1]), str(item[2]), item[3])
176                report.write(content)
177            report.write(table_ended)
178    except(IOError, ValueError):
179        print("Error for create html summary")
180
181
182def create_table_test(reportpath, subsystem_name, datalist, total_count, covered_count):
183    table_title = """
184    %s details:</h4>
185    """
186    table_start = """<div><table class="reference" align="center"><tbody>"""
187    table_head = """
188        <tr>
189            <th>PartName</th>
190            <th>ClassName</th>
191            <th>InterfaceName</th>
192            <th>IsCovered</th>
193        </tr>"""
194    table_line = """
195        <tr class=normal>
196            <td>%s</td>
197            <td>%s</td>
198            <td>%s</td>
199            <td>%s</td>
200        </tr>
201    """
202    table_summary = """
203                <tr class='normal' align="center">
204                    <td colspan="4" >TotalCount: %s, CoveredCount:%s, Coverage: %s</td>
205                </tr>
206            """
207    table_ended = """</tbody></table></div>
208    """
209    try:
210        with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report:
211            print("part_name==" + subsystem_name)
212            tabletitle = table_title % (subsystem_name)
213            print("tabletitle==" + tabletitle)
214            tabletitle = "<h4 style=\"text-align: left;font-family: 微软雅黑;margin-left: 3%;\">" + tabletitle + "</h4>"
215            report.write(tabletitle)
216            report.write(table_start)
217            report.write(table_head)
218            datalist.sort(key=sort_by_field_element_data, reverse=False)
219
220            for line in datalist:
221                if str(line[2]) == "N":
222                    content = table_line % (
223                        "<font=>" + subsystem_name + "</font>", "<font>" + line[0] + "</font>",
224                        "<font>" + line[1] + "</font>",
225                        "<font color=\"red\">" + str(line[2]) + "</font>")
226                    report.write(content)
227                else:
228                    content = table_line % (
229                        "<font>" + subsystem_name + "</font>", "<font>" + line[0] + "</font>",
230                        "<font>" + line[1] + "</font>",
231                        "<font color=\"green\">" + str(line[2]) + "</font>")
232                    report.write(content)
233            if 0 != total_count:
234                coverage = str("%.2f" % (covered_count * 100 / total_count)) + "%"
235            else:
236                coverage = "0%"
237            coverage = table_summary % (total_count, covered_count, coverage)
238            report.write(coverage)
239            report.write(table_ended)
240    except(IOError, ValueError):
241        print("Error for create html table test")
242
243
244def create_html_ended(reportpath):
245    try:
246        with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report:
247            report.write(html_body_ended)
248            report.write(html_ended)
249    except(IOError, ValueError):
250        print("Error for create html end")
251