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