#!/usr/bin/env python3
# coding=utf-8
#
# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import os
import sys
import stat
import datetime
from importlib import reload
reload(sys)
FLAGS_WRITE = os.O_WRONLY | os.O_CREAT | os.O_EXCL
FLAGS_ADD = os.O_WRONLY | os.O_APPEND | os.O_CREAT
MODES = stat.S_IWUSR | stat.S_IRUSR
HTML_HEAD = """
接口覆盖率邮件模板
"""
HTML_BODY_START = """
"""
HTML_BODY_ENDED = """
"""
HTML_ENDED = """
"""
def sort_by_field_element(elem):
return int(float(elem[3][:-1])) # @######value是浮点数,如1.0,那么需要先转float再转int
def sort_by_field_element_data(elem):
return elem[2]
def create_html_start(reportpath):
try:
if os.path.exists(reportpath):
os.remove(reportpath)
with os.fdopen(os.open(reportpath, FLAGS_WRITE, MODES), 'w') as report:
report.write(HTML_HEAD)
report.write(HTML_BODY_START)
except(IOError, ValueError):
print("Error for create html start ",)
def create_title(reportpath, title_name, summary_list):
currdatetime = datetime.date.today().strftime("%Y-%m-%d")
report_title = """
%s coverage report (%s)
"""
content = report_title % (title_name, currdatetime)
report_title = content + """
Summary Report
接口总数%s, 已覆盖%s, 未覆盖%s
|
"""
subsystems = ""
count = 0
for item in summary_list:
subsystem = item[0]
if count < 3:
subsystems = "%s、%s" % (subsystems, subsystem)
count = count + 1
if subsystem == "Summary":
nocoverd = item[1] - item[2]
report_title = report_title % (item[1], item[2], nocoverd)
try:
with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report:
report.write(report_title)
except(IOError, ValueError):
print("Error for create html title")
def create_summary(reportpath, summary_list):
table_title = """
Summary
"""
table_start = """"""
table_head = """
| PartName |
TotalCount |
CoveredCount |
Coverage |
"""
table_line = """
| %s |
%s |
%s |
%s |
"""
table_ended = """
"""
try:
if len(summary_list) == 0:
return
with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report:
report.write(table_title)
report.write(table_start)
report.write(table_head)
for item in summary_list:
content = table_line % (item[0], str(item[1]), str(item[2]), item[3])
report.write(content)
report.write(table_ended)
except(IOError, ValueError):
print("Error for create html summary")
def create_table_test(reportpath, subsystem_name, datalist, total_count, covered_count):
table_title = """
%s details:
"""
table_start = """"""
table_head = """
| PartName |
ClassName |
InterfaceName |
IsCovered |
"""
table_line = """
| %s |
%s |
%s |
%s |
"""
table_summary = """
| TotalCount: %s, CoveredCount:%s, Coverage: %s |
"""
table_ended = """
"""
try:
with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report:
print("part_name==" + subsystem_name)
tabletitle = table_title % (subsystem_name)
print("tabletitle==" + tabletitle)
tabletitle = "" + tabletitle + "
"
report.write(tabletitle)
report.write(table_start)
report.write(table_head)
datalist.sort(key=sort_by_field_element_data, reverse=False)
for line in datalist:
if str(line[2]) == "N":
content = table_line % (
"" + subsystem_name + "", "" + line[0] + "",
"" + line[1] + "",
"" + str(line[2]) + "")
report.write(content)
else:
content = table_line % (
"" + subsystem_name + "", "" + line[0] + "",
"" + line[1] + "",
"" + str(line[2]) + "")
report.write(content)
if 0 != total_count:
coverage = str("%.2f" % (covered_count * 100 / total_count)) + "%"
else:
coverage = "0%"
coverage = table_summary % (total_count, covered_count, coverage)
report.write(coverage)
report.write(table_ended)
except(IOError, ValueError):
print("Error for create html table test")
def create_html_ended(reportpath):
try:
with os.fdopen(os.open(reportpath, FLAGS_ADD, MODES), 'a') as report:
report.write(HTML_BODY_ENDED)
report.write(HTML_ENDED)
except(IOError, ValueError):
print("Error for create html end")