diff --git a/GenReport/README.md b/GenReport/README.md index f4ae80572e8bcaa8120d772bfe232d086fe066f9..906c9de55c9ad23373f2ee756d0dd670c1abdb12 100644 --- a/GenReport/README.md +++ b/GenReport/README.md @@ -5,7 +5,7 @@ json结果转pdf工具 #### 依赖安装 -1. pip3 install reportlab +1. pip3 install reportlab==3.6.12 #### 使用说明 1. 基本用法,直接执行使用默认路径生成pdf @@ -16,6 +16,7 @@ json结果转pdf工具 2. 使用参数指定文件路径 - -r : 指定pdf生成结果路径 + - -e : 指定环境检测结果路径 - -l : 指定运行库检查结果文件路径 - -f : 指定文件系统层次结构检查结果文件路径 - -c : 指定常用命令检查结果文件路径 diff --git a/GenReport/pdf.py b/GenReport/pdf.py index 658aa7dc1f3b0b409448ccfde008644c73aec48f..5cecf48be7539731ffa4c6ebdddec169902f06b3 100755 --- a/GenReport/pdf.py +++ b/GenReport/pdf.py @@ -1,9 +1,11 @@ #!/usr/bin/python3 from pdfmaker import PdfMaker -from reportlab.platypus import PageBreak +from reportlab.platypus import PageBreak from reportlab.lib import colors # 颜色模块 from reportlab.platypus import Spacer +from reportlab.lib.styles import getSampleStyleSheet # 样式 +from reportlab.platypus import Paragraph import json import argparse import os @@ -19,53 +21,76 @@ g_server = "../Outputs/service_result.json" g_result = "./操作系统应用编程接口要求标准符合性测试报告.pdf" g_isConform = "符合" -#table_background_color = '#87CEEB' -default_style = [ - ('FONTNAME', (0, 0), (-1, -1), 'SimSun'), # 字体 - ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 水平居中 - ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 垂直居中对齐 - # ('ALIGN', (0, 1), (-1, -1), 'CENTER'), # 第二行到最后一行左右左对齐 - ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 所有表格上下居中对齐 - ('TEXTCOLOR', (0, 0), (-1, -1), colors.darkslategray), # 设置表格内文字颜色 - ('GRID', (0, 0), (-1, -1), 0.5, colors.black), # 设置表格框线为grey色,线宽为0.5 - ] - +# 表格样式 +# table_background_color = '#87CEEB' +default_table_style = [ + ['ALIGN', (0, 0), (-1, -1), 'CENTER'], # 水平居中 + ['VALIGN', (0, 0), (-1, -1), 'MIDDLE'], # 垂直居中对齐 + # ['ALIGN', (0, 1), (-1, -1), 'CENTER'], # 第二行到最后一行左右左对齐 + ['VALIGN', (0, 0), (-1, -1), 'MIDDLE'], # 所有表格上下居中对齐 + ['GRID', (0, 0), (-1, -1), 0.5, colors.black], # 设置表格框线为grey色,线宽为0.5 +] +default_para_style = getSampleStyleSheet()['Normal'] +default_para_style.fontName = 'SimSun' +default_para_style.wordWrap = 'CJK' # 设置自动换行 +default_para_style.fontSize = 10 +default_para_style.textColor = colors.darkslategray # 设置表格内文字颜色 +default_para_style.alignment = 1 # 居中对齐 + +# 首页样式 home_table_style = [ - ('FONTNAME', (0, 0), (-1, -1), 'SimSun'), # 字体 - ('FONTSIZE', (0, 0), (-1, -1), 20), - ('ALIGN', (0, 0), (-1, -1), 'CENTER'), # 第一行水平居中 - ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), # 所有表格上下居中对齐 - ('ALIGN', (0, 0), (0, -1), 'RIGHT'), - ('ALIGN', (1, 0), (1, -1), 'CENTER'), + ['ALIGN', (0, 0), (-1, -1), 'CENTER'], # 第一行水平居中 + ['VALIGN', (0, 0), (-1, -1), 'MIDDLE'], # 所有表格上下居中对齐 + ['ALIGN', (0, 0), (0, -1), 'RIGHT'], + ['ALIGN', (1, 0), (1, -1), 'CENTER'], ] +home_para_style = getSampleStyleSheet()['Normal'] +home_para_style.fontName = 'SimSun' +home_para_style.fontSize = 20 +home_para_style.wordWrap = 'CJK' # 设置自动换行 +home_para_style.alignment = 1 # 居中对齐 + def make_pdf_cover(content): # 添加标题 content.append(Spacer(1, 30)) - content.append(PdfMaker.draw_title('《操作系统应用编程接口要求》\n标准符合性测试报告',30)) - + content.append(PdfMaker.draw_title('《操作系统应用编程接口要求》\n标准符合性测试报告', 30)) + # 添加日期等内容 content.append(Spacer(1, 50)) if not os.path.exists(g_env): print("未发现测试环境结果文件") - content.append(PdfMaker.draw_little_title('未发现测试环境结果文件', 12, colors.red)) + content.append( + PdfMaker.draw_little_title( + '未发现测试环境结果文件', 12, colors.red)) return - with open(g_env,'r',encoding='utf8') as fp: + with open(g_env, 'r', encoding='utf8') as fp: json_data = json.load(fp) data = [ - ('产品名称:', json_data["测试对象"]["系统名称"]), - ('产品版本:', json_data["测试对象"]["版本"]), - ('测试日期:', json_data["测试时间"]) + ['产品名称:', json_data["测试对象"]["系统名称"]], + ['产品版本:', json_data["测试对象"]["版本"]], + ['测试日期:', json_data["测试时间"]] ] - content.append(PdfMaker.draw_table(home_table_style, totalWidth=500, rates=[2,7], *data)) + + for i, row in enumerate(data): + for j, cell in enumerate(row): + data[i][j] = Paragraph(str(cell), home_para_style) + + content.append( + PdfMaker.draw_table( + home_table_style, + data, + totalWidth=550, + rates=[2, 7],)) # 添加检测机构 content.append(Spacer(1, 100)) unit = json_data["送测单位"] - content.append(PdfMaker.draw_title(unit,20)) + content.append(PdfMaker.draw_title(unit, 20)) + def make_pdf_test_env(content): # 添加小标题 @@ -73,40 +98,51 @@ def make_pdf_test_env(content): if not os.path.exists(g_env): print("未发现测试环境结果文件") - content.append(PdfMaker.draw_little_title('未发现测试环境结果文件', 12, colors.red)) + content.append( + PdfMaker.draw_little_title( + '未发现测试环境结果文件', 12, colors.red)) return data = [] - with open(g_env,'r',encoding='utf8') as fp: + with open(g_env, 'r', encoding='utf8') as fp: json_data = json.load(fp) data = [ - ('测试对象', '系统名称', json_data["测试对象"]["系统名称"]), - ('测试对象', '版本', json_data["测试对象"]["版本"]), - ('送测单位', '送测单位', json_data["送测单位"]), - ('系统环境', '内核版本', json_data["系统环境"]["内核版本"]), - ('系统环境', '编译器版本', json_data["系统环境"]["编译器版本"]), - ('系统环境', 'Python版本', json_data["系统环境"]["Python版本"]), - ('环境配置', '机器型号', json_data["环境配置"]["CPU型号"]), - ('环境配置', 'CPU指令集', json_data["环境配置"]["CPU指令集"]), - ('环境配置', 'CPU型号', json_data["环境配置"]["CPU型号"]), - ('环境配置', '内存', json_data["环境配置"]["内存"]), - ('环境配置', '硬盘', json_data["环境配置"]["硬盘"]), - ('环境配置', '固件', json_data["环境配置"]["固件"]), - ('测试工具', '名称', json_data["测试工具"]["名称"]), - ('测试工具', '版本', json_data["测试工具"]["版本"]), - ('测试时间', '测试时间', json_data["测试时间"]), + ['测试对象', '系统名称', json_data["测试对象"]["系统名称"]], + ['测试对象', '版本', json_data["测试对象"]["版本"]], + ['送测单位', '送测单位', json_data["送测单位"]], + ['系统环境', '内核版本', json_data["系统环境"]["内核版本"]], + ['系统环境', '编译器版本', json_data["系统环境"]["编译器版本"]], + ['系统环境', 'Python版本', json_data["系统环境"]["Python版本"]], + ['环境配置', '机器型号', json_data["环境配置"]["CPU型号"]], + ['环境配置', 'CPU指令集', json_data["环境配置"]["CPU指令集"]], + ['环境配置', 'CPU型号', json_data["环境配置"]["CPU型号"]], + ['环境配置', '内存', json_data["环境配置"]["内存"]], + ['环境配置', '硬盘', json_data["环境配置"]["硬盘"]], + ['环境配置', '固件', json_data["环境配置"]["固件"]], + ['测试工具', '名称', json_data["测试工具"]["名称"]], + ['测试工具', '版本', json_data["测试工具"]["版本"]], + ['测试时间', '测试时间', json_data["测试时间"]], ] - - extra_style = default_style + [ - ('SPAN', (0, 0), (0, 1)), - ('SPAN', (0, 2), (1, 2)), - ('SPAN', (0, 3), (0, 5)), - ('SPAN', (0, 6), (0, 11)), - ('SPAN', (0, 12), (0, 13)), - ] - content.append(PdfMaker.draw_table(extra_style, totalWidth=total_width, rowHeight=27, rates=[1,1,3], *data)) + for i, row in enumerate(data): + for j, cell in enumerate(row): + data[i][j] = Paragraph(str(cell), default_para_style) + extra_style = default_table_style + [ + ['SPAN', (0, 0), (0, 1)], + ['SPAN', (0, 2), (1, 2)], + ['SPAN', (0, 3), (0, 5)], + ['SPAN', (0, 6), (0, 11)], + ['SPAN', (0, 12), (0, 13)], + ] + + content.append( + PdfMaker.draw_table( + extra_style, + data, + totalWidth=total_width, + rowHeight=27, + rates=[1, 1, 3])) def make_pdf_lib(content): @@ -117,284 +153,331 @@ def make_pdf_lib(content): if not os.path.exists(g_lib): print("未发现运行库检查结果文件") - content.append(PdfMaker.draw_little_title('未发现运行库检查结果文件', 12, colors.red)) + content.append( + PdfMaker.draw_little_title( + '未发现运行库检查结果文件', 12, colors.red)) return # (1)库包检查 content.append(PdfMaker.draw_little_title('(1)库包检查', 12)) # 添加表格 data = [ - ('包名', '检测信息', '--', '--', '--'), - ('包名', '检测信息', '--', '--', '--'), + ['包名', '检测信息', '--', '--', '--'], + ['包名', '检测信息', '--', '--', '--'], ] - with open(g_lib,'r',encoding='utf8') as fp: + with open(g_lib, 'r', encoding='utf8') as fp: json_data = json.load(fp) row = 2 for r in json_data: - + require_version = json_data[r]["Required version"] level = json_data[r]["Level"] - data.append((r, '版本要求', '级别', '--', '--')) - data.append((r, require_version, level, '--', '--')) - data.append((r, '二进制包名', '当前版本', '--', '测试结果')) - + data.append([r, '版本要求', '级别', '--', '--']) + data.append([r, require_version, level, '--', '--']) + data.append([r, '二进制包名', '当前版本', '--', '测试结果']) + # local_version = "-" # status = "" for pack in json_data[r]["Binary package"]: if isinstance(json_data[r]["Binary package"][pack], dict): - for lib in json_data[r]["Binary package"][pack]: + for lib in json_data[r]["Binary package"][pack]: local_version = json_data[r]["Binary package"][pack][lib]["version"] status = json_data[r]["Binary package"][pack][lib]["status"] - - if status == "compatible" : + + if status == "compatible": status = "通过" elif status == "not installed": continue else: status = "不通过" - data.append((r, lib, local_version, '--', status)) - - data.append((r, '共享对象名', '存储路径', '归属包名', '测试结果')) - if isinstance(json_data[r]["Shared library"],dict): + data.append([r, lib, local_version, '--', status]) + + data.append([r, '共享对象名', '存储路径', '归属包名', '测试结果']) + if isinstance(json_data[r]["Shared library"], dict): for shared in json_data[r]["Shared library"]: result = json_data[r]["Shared library"][shared]["status"] - if result == "compatible" : + if result == "compatible": result = "通过" else: result = "不通过" - + belongs = "-" - - if json_data[r]["Shared library"][shared]["belongs"] != "None": + + if json_data[r]["Shared library"][shared]["belongs"] != "None": belongs = json_data[r]["Shared library"][shared]["belongs"] path = json_data[r]["Shared library"][shared]["path"] if len(path) > 65: - path_list =list(path) + path_list = list(path) path_list.insert(60, '\n') path = ''.join(path_list) - data.append((r, shared, path, belongs, result)) - - lib_table_style = default_style + [ - ('FONTSIZE', (0, 0), (-1, 1), 15), # 第一,二行的字体大小 - ('FONTSIZE', (0, 2), (-1, -1), 10), # 第三行到最后一行的字体大小 - ('BACKGROUND', (0, 0), (-1, 0), table_background_color), # 设置第一行背景颜色 - ('BACKGROUND', (0, 1), (-1, 1), table_background_color), # 设置第二行背景颜色 - ('SPAN', (0, 0), (0, 1)), # 合并第一列一二行 - ('SPAN', (1, 0), (-1, 1)), # 合并第四列一二行 - ] + data.append([r, shared, path, belongs, result]) + + lib_table_style = default_table_style + [ + ['BACKGROUND', (0, 0), (-1, 0), table_background_color], # 设置第一行背景颜色 + ['BACKGROUND', (0, 1), (-1, 1), table_background_color], # 设置第二行背景颜色 + ['SPAN', (0, 0), (0, 1)], # 合并第一列一二行 + ['SPAN', (1, 0), (-1, 1)], # 合并第四列一二行 + ] - row = 10 - i = 2 - new_data = data[0:row] is_merge = False - while i < row: - if new_data[i][1] == "版本要求": - lib_table_style.append(('BACKGROUND', (1, i), (-1, i), table_background_color)) - lib_table_style.append(('SPAN', (2, i), (-1, i))) - lib_table_style.append(('SPAN', (2, i+1), (-1, i+1))) - if new_data[i][1] == "二进制包名": - lib_table_style.append(('BACKGROUND', (1, i), (-1, i), table_background_color)) - is_merge = True - - if new_data[i][1] == "共享对象名": - lib_table_style.append(('BACKGROUND', (1, i), (-1, i), table_background_color)) + for i, r in enumerate(data): + if data[i][1] == "版本要求": + lib_table_style.append( + ['BACKGROUND', (1, i), (-1, i), table_background_color]) + lib_table_style.append(['SPAN', (2, i), (-1, i)]) + lib_table_style.append(['SPAN', (2, i + 1), (-1, i + 1)]) + if data[i][1] == "二进制包名": + lib_table_style.append( + ['BACKGROUND', (1, i), (-1, i), table_background_color]) + is_merge = True + if data[i][1] == "共享对象名": + lib_table_style.append( + ['BACKGROUND', (1, i), (-1, i), table_background_color]) is_merge = False - - if is_merge: - lib_table_style.append(('SPAN', (2, i), (3, i))) - - if new_data[i][-1] == "不通过": - lib_table_style.append(('TEXTCOLOR', (0, i), (-1, i), colors.red)) - - i += 1 - - content.append(PdfMaker.draw_mul_table(lib_table_style, totalWidth=total_width, rates=[2,3,7,2,1], *new_data)) - while row