1#!/usr/bin/env python3 2# coding=utf-8 3 4# 5# Copyright (c) 2024 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 os 20import subprocess 21 22 23def sub_command(command): 24 proc = subprocess.Popen(command, shell=True) 25 try: 26 proc.communicate() 27 except subprocess.TimeoutExpired: 28 proc.kill() 29 proc.terminate() 30 31 32def install_tool(home_path): 33 coverage_sh = os.path.join( 34 home_path, "test/testfwk/developer_test/local_coverage/automate_execute/coverage.sh") 35 sub_command("chmod +x %s" % coverage_sh) 36 sub_command(coverage_sh) 37 38 39def update_lcovrc(): 40 print("修改/etc/lcovrc文件中:lcov_branch_coverage=0为lcov_branch_coverage=1") 41 subprocess.call(["sudo", "echo", ""]) 42 file_path = "/etc/lcovrc" 43 os.chmod(file_path, 0o777) 44 with open(file_path, "r", encoding="utf-8") as f: 45 txt = f.read() 46 txt = txt.replace("lcov_branch_coverage = 0", "lcov_branch_coverage = 1") 47 with open(file_path, "w", encoding="utf-8") as f1: 48 f1.write(txt) 49 50 51if __name__ == '__main__': 52 current_path = os.getcwd() 53 root_path = current_path.split("/test/testfwk/developer_test")[0] 54 if os.geteuid() == 0: 55 install_tool(root_path) 56 update_lcovrc() 57 else: 58 print("当前用户不是root用户,请在root用户下执行该脚本,因为lcov只能在root下安装," 59 "且需要修改/etc/lcovrc文件,其他用户没有权限修改,会导致分支覆盖率无法生成!") 60 61 62 63 64 65