1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2022 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 threading 20import logging 21import os 22from jinja2 import Environment, FileSystemLoader 23 24from devicetest.utils.file_util import create_dir 25from xdevice import FilePermission, platform_logger 26from xdevice import Variables 27 28log = platform_logger(name="ReporterHelper") 29# 常用的logger 30log_names = ["AppTest", "DeviceTest", "Device", "Hdc", "Utils", "TestRunner", "WindowsTest"] 31 32 33class BufferHandler(logging.Handler): 34 35 def __init__(self): 36 super().__init__() 37 self.buffer = [] 38 self.thread = None 39 40 def emit(self, record): 41 if record.thread != self.thread: 42 return 43 msg = self.format(record) 44 if record.levelno == logging.ERROR: 45 msg = "<div class=\"error\">{}</div>".format(msg) 46 if record.levelno == logging.WARNING: 47 msg = "<div class=\"warning\">{}</div>".format(msg) 48 self.buffer.append(msg) 49 50 51def add_log_caching_handler(buffer_hdl=None): 52 """添加日志缓存handler""" 53 if buffer_hdl is None: 54 buffer_hdl = BufferHandler() 55 buffer_hdl.thread = threading.currentThread().ident 56 buffer_hdl.setFormatter(logging.Formatter(Variables.report_vars.log_format)) 57 for name in log_names: 58 logger = platform_logger(name) 59 logger.platform_log.handlers.append(buffer_hdl) 60 return buffer_hdl 61 62 63def del_log_caching_handler(buffer_hdl): 64 """移除日志缓存handler""" 65 if buffer_hdl is None: 66 return 67 for name in log_names: 68 logger = platform_logger(name) 69 handlers = logger.platform_log.handlers 70 handlers.remove(buffer_hdl) 71 72 73def get_caching_logs(buffer_hdl): 74 """获取日志缓存记录""" 75 return buffer_hdl.buffer if isinstance(buffer_hdl, BufferHandler) else [] 76 77 78def generate_report(to_file, template="case.html", **kwargs): 79 """生成用例html报告 80 Args: 81 to_file : str, render to file 82 template: str, render template 83 Example: 84 generate_report(to_file, case=case_info, logs=log_content) 85 """ 86 try: 87 create_dir(os.path.dirname(to_file)) 88 template_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../res/template") 89 env = Environment( 90 loader=FileSystemLoader(template_path), 91 lstrip_blocks=True, 92 trim_blocks=True) 93 template = env.get_template(template) 94 95 html_fd = os.open(to_file, os.O_CREAT | os.O_WRONLY, FilePermission.mode_644) 96 with os.fdopen(html_fd, mode="w", encoding="utf-8") as html_f: 97 html_f.write(template.render(kwargs)) 98 log.info("report is generated in path: {}".format(to_file)) 99 except Exception as exception: 100 log.error("report generating failed! {}".format(exception)) 101