• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import os
17import json
18import sys
19import argparse
20import stat
21
22
23def read_json_file(input_file: str):
24    if not os.path.exists(input_file):
25        print("file '{}' doesn't exist.".format(input_file))
26        return None
27
28    data = None
29    try:
30        with open(input_file, 'r') as input_f:
31            data = json.load(input_f)
32    except json.decoder.JSONDecodeError:
33        print("The file '{}' format is incorrect.".format(input_file))
34        raise
35    return data
36
37
38def write_json_file(output_file: str, content):
39    file_dir = os.path.dirname(os.path.abspath(output_file))
40    if not os.path.exists(file_dir):
41        os.makedirs(file_dir, exist_ok=True)
42    with os.fdopen(os.open(output_file,
43                               os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 'w') as output_f:
44        json.dump(content, output_f, indent=2)
45
46
47def get_wrong_used_deps(parts_path_file: str, deps_files: str):
48
49    print("Start check deps!")
50    part_path_data = read_json_file(parts_path_file)
51    wrong_used_deps = {}
52    not_exist_part = []
53    ignore_parts = ['unittest', 'moduletest', 'systemtest']
54
55    for filename in os.listdir(deps_files):
56        file_path = os.path.join(deps_files, filename)
57        module_deps_data = read_json_file(file_path)
58
59        part_name = module_deps_data.get("part_name")
60        deps = module_deps_data.get("deps")
61        module_label = module_deps_data.get("module_label").split("(")[0]
62        part_path = part_path_data.get(part_name, "no_part_path")
63
64        if part_name in ignore_parts:
65            continue
66        if part_path == "no_part_path":
67            if part_name not in not_exist_part:
68                print("Warning! Can not find part '{}' path".format(part_name))
69                not_exist_part.append(part_name)
70            continue
71
72        _wrong_used_deps = []
73        for dep in deps:
74            dep_path = dep[2:]
75            if not dep_path.startswith(part_path):
76                _wrong_used_deps.append(dep)
77        if len(_wrong_used_deps) > 0:
78            if part_name not in wrong_used_deps:
79                wrong_used_deps[part_name] = {module_label: _wrong_used_deps}
80            else:
81                wrong_used_deps[part_name][module_label] = _wrong_used_deps
82
83    output_file = os.path.join(os.path.dirname(
84        deps_files), "module_deps_info", "wrong_used_deps.json")
85    write_json_file(output_file, wrong_used_deps)
86    print("Check deps result output to '{}'.".format(output_file))
87
88
89def main(argv):
90    parser = argparse.ArgumentParser()
91    parser.add_argument('--parts-path-file', required=True)
92    parser.add_argument('--deps-path', required=True)
93    args = parser.parse_args(argv)
94    get_wrong_used_deps(args.parts_path_file, args.deps_path)
95    return 0
96
97
98if __name__ == '__main__':
99    sys.exit(main(sys.argv[1:]))
100