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 22import os 23import stat 24from importlib import reload 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 = 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 with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report: 169 report.write(table_title) 170 report.write(table_start) 171 report.write(table_head) 172 for item in summary_list: 173 content = table_line % (item[0], str(item[1]), str(item[2]), item[3]) 174 report.write(content) 175 report.write(table_ended) 176 except(IOError, ValueError): 177 print("Error for create html summary") 178 179 180def create_table_test(reportpath, subsystem_name, datalist, total_count, covered_count): 181 table_title = """ 182 %s details:</h4> 183 """ 184 table_start = """<div><table class="reference" align="center"><tbody>""" 185 table_head = """ 186 <tr> 187 <th>PartName</th> 188 <th>ClassName</th> 189 <th>InterfaceName</th> 190 <th>IsCovered</th> 191 </tr>""" 192 table_line = """ 193 <tr class=normal> 194 <td>%s</td> 195 <td>%s</td> 196 <td>%s</td> 197 <td>%s</td> 198 </tr> 199 """ 200 table_summary = """ 201 <tr class='normal' align="center"> 202 <td colspan="4" >TotalCount: %s, CoveredCount:%s, Coverage: %s</td> 203 </tr> 204 """ 205 table_ended = """</tbody></table></div> 206 """ 207 try: 208 with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report: 209 print("part_name==" + subsystem_name) 210 tabletitle = table_title % (subsystem_name) 211 print("tabletitle==" + tabletitle) 212 tabletitle = "<h4 style=\"text-align: left;font-family: 微软雅黑;margin-left: 3%;\">" + tabletitle + "</h4>" 213 report.write(tabletitle) 214 report.write(table_start) 215 report.write(table_head) 216 datalist.sort(key=sort_by_field_element_data, reverse=False) 217 218 for line in datalist: 219 if str(line[2]) == "N": 220 content = table_line % ( 221 "<font=>" + subsystem_name + "</font>", "<font>" + line[0] + "</font>", 222 "<font>" + line[1] + "</font>", 223 "<font color=\"red\">" + str(line[2]) + "</font>") 224 report.write(content) 225 else: 226 content = table_line % ( 227 "<font>" + subsystem_name + "</font>", "<font>" + line[0] + "</font>", 228 "<font>" + line[1] + "</font>", 229 "<font color=\"green\">" + str(line[2]) + "</font>") 230 report.write(content) 231 if 0 != total_count: 232 coverage = str("%.2f" % (covered_count * 100 / total_count)) + "%" 233 else: 234 coverage = "0%" 235 coverage = table_summary % (total_count, covered_count, coverage) 236 report.write(coverage) 237 report.write(table_ended) 238 except(IOError, ValueError): 239 print("Error for create html table test") 240 241 242def create_html_ended(reportpath): 243 try: 244 with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report: 245 report.write(html_body_ended) 246 report.write(html_ended) 247 except(IOError, ValueError): 248 print("Error for create html end") 249