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 20 21 22def read_json_file(input_file): 23 if not os.path.exists(input_file): 24 print("file '{}' doesn't exist.".format(input_file)) 25 return None 26 27 data = None 28 try: 29 with open(input_file, 'r') as input_f: 30 data = json.load(input_f) 31 except json.decoder.JSONDecodeError: 32 print("The file '{}' format is incorrect.".format(input_file)) 33 raise 34 return data 35 36 37def write_json_file(output_file, content): 38 file_dir = os.path.dirname(os.path.abspath(output_file)) 39 if not os.path.exists(file_dir): 40 os.makedirs(file_dir, exist_ok=True) 41 with open(output_file, 'w') as output_f: 42 json.dump(content, output_f, indent=2) 43 44 45def get_wrong_used_deps(parts_path_file, deps_files): 46 47 print("Start check deps!") 48 part_path_data = read_json_file(parts_path_file) 49 wrong_used_deps = {} 50 not_exist_part = [] 51 ignore_parts = ['unittest', 'moduletest', 'systemtest'] 52 53 for filename in os.listdir(deps_files): 54 file_path = os.path.join(deps_files, filename) 55 module_deps_data = read_json_file(file_path) 56 57 part_name = module_deps_data.get("part_name") 58 deps = module_deps_data.get("deps") 59 module_label = module_deps_data.get("module_label").split("(")[0] 60 part_path = part_path_data.get(part_name, "no_part_path") 61 62 if part_name in ignore_parts: 63 continue 64 if part_path == "no_part_path": 65 if part_name not in not_exist_part: 66 print("Warning! Can not find part '{}' path".format(part_name)) 67 not_exist_part.append(part_name) 68 continue 69 70 _wrong_used_deps = [] 71 for dep in deps: 72 dep_path = dep[2:] 73 if not dep_path.startswith(part_path): 74 _wrong_used_deps.append(dep) 75 if len(_wrong_used_deps) > 0: 76 if part_name not in wrong_used_deps: 77 wrong_used_deps[part_name] = {module_label: _wrong_used_deps} 78 else: 79 wrong_used_deps[part_name][module_label] = _wrong_used_deps 80 81 output_file = os.path.join(os.path.dirname( 82 deps_files), "module_deps_info", "wrong_used_deps.json") 83 write_json_file(output_file, wrong_used_deps) 84 print("Check deps result output to '{}'.".format(output_file)) 85 86 87def main(argv): 88 parser = argparse.ArgumentParser() 89 parser.add_argument('--parts-path-file', required=True) 90 parser.add_argument('--deps-path', required=True) 91 args = parser.parse_args(argv) 92 get_wrong_used_deps(args.parts_path_file, args.deps_path) 93 return 0 94 95 96if __name__ == '__main__': 97 sys.exit(main(sys.argv[1:])) 98