• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# coding=utf-8
3
4#
5# Copyright (c) 2020-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
19import os
20import subprocess
21
22
23def get_file_list(find_path, postfix=""):
24    file_names = os.listdir(find_path)
25    file_list = []
26    if len(file_names) > 0:
27        for fn in file_names:
28            if postfix != "":
29                if fn.find(postfix) != -1 and fn[-len(postfix):] == postfix:
30                    file_list.append(fn)
31            else:
32                file_list.append(fn)
33    return file_list
34
35
36def get_file_list_by_postfix(path, postfix=""):
37    file_list = []
38    for dirs in os.walk(path):
39        files = get_file_list(find_path=dirs[0], postfix=postfix)
40        for file_name in files:
41            if "" != file_name and -1 == file_name.find(__file__):
42                file_name = os.path.join(dirs[0], file_name)
43                if os.path.isfile(file_name):
44                    file_list.append(file_name)
45    return file_list
46
47
48def recover_source_file(cpp_list, keys):
49    if not cpp_list:
50        print("no any .cpp file here")
51        return
52
53    for path in cpp_list:
54        if not os.path.exists(path):
55            return
56        for key in keys:
57            with open(path, "r") as read_fp:
58                code_lines = read_fp.readlines()
59            with open(f"{path.split('.')[0]}_bk.html", "w") as write_fp:
60                for line in code_lines:
61                    if key in line:
62                        write_fp.write(line.strip("\n").strip("\n\r").replace(" //LCOV_EXCL_BR_LINE", ""))
63                        write_fp.write("\n")
64                    else:
65                        write_fp.write(line)
66
67            os.remove(path)
68            subprocess.Popen("mv %s %s" % (f"{path.split('.')[0]}_bk.html", path),
69                             shell=True).communicate()
70
71
72if __name__ == '__main__':
73    current_path = os.getcwd()
74    root_path = current_path.split("/test/testfwk/developer_test")[0]
75    html_path = os.path.join(
76        root_path,
77        "test/testfwk/developer_test/localCoverage/codeCoverage/results/coverage/reports/cxx/html")
78    cpp_arr_list = get_file_list_by_postfix(html_path, ".html")
79    recover_source_file(cpp_arr_list, keys=[" //LCOV_EXCL_BR_LINE"])
80