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