15f9996aaSopenharmony_ci#!/usr/bin/env python3
25f9996aaSopenharmony_ci# -*- coding: utf-8 -*-
35f9996aaSopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
65f9996aaSopenharmony_ci# You may obtain a copy of the License at
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95f9996aaSopenharmony_ci#
105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
145f9996aaSopenharmony_ci# limitations under the License.
155f9996aaSopenharmony_ci
165f9996aaSopenharmony_ciimport os
175f9996aaSopenharmony_ciimport json
185f9996aaSopenharmony_ciimport sys
195f9996aaSopenharmony_ciimport argparse
205f9996aaSopenharmony_ci
215f9996aaSopenharmony_ci
225f9996aaSopenharmony_cidef read_json_file(input_file: str):
235f9996aaSopenharmony_ci    if not os.path.exists(input_file):
245f9996aaSopenharmony_ci        print("file '{}' doesn't exist.".format(input_file))
255f9996aaSopenharmony_ci        return None
265f9996aaSopenharmony_ci
275f9996aaSopenharmony_ci    data = None
285f9996aaSopenharmony_ci    try:
295f9996aaSopenharmony_ci        with open(input_file, 'r') as input_f:
305f9996aaSopenharmony_ci            data = json.load(input_f)
315f9996aaSopenharmony_ci    except json.decoder.JSONDecodeError:
325f9996aaSopenharmony_ci        print("The file '{}' format is incorrect.".format(input_file))
335f9996aaSopenharmony_ci        raise
345f9996aaSopenharmony_ci    return data
355f9996aaSopenharmony_ci
365f9996aaSopenharmony_ci
375f9996aaSopenharmony_cidef write_json_file(output_file: str, content):
385f9996aaSopenharmony_ci    file_dir = os.path.dirname(os.path.abspath(output_file))
395f9996aaSopenharmony_ci    if not os.path.exists(file_dir):
405f9996aaSopenharmony_ci        os.makedirs(file_dir, exist_ok=True)
415f9996aaSopenharmony_ci    with open(output_file, 'w') as output_f:
425f9996aaSopenharmony_ci        json.dump(content, output_f, indent=2)
435f9996aaSopenharmony_ci
445f9996aaSopenharmony_ci
455f9996aaSopenharmony_cidef get_wrong_used_deps(parts_path_file: str, deps_files: str):
465f9996aaSopenharmony_ci
475f9996aaSopenharmony_ci    print("Start check deps!")
485f9996aaSopenharmony_ci    part_path_data = read_json_file(parts_path_file)
495f9996aaSopenharmony_ci    wrong_used_deps = {}
505f9996aaSopenharmony_ci    not_exist_part = []
515f9996aaSopenharmony_ci    ignore_parts = ['unittest', 'moduletest', 'systemtest']
525f9996aaSopenharmony_ci
535f9996aaSopenharmony_ci    for filename in os.listdir(deps_files):
545f9996aaSopenharmony_ci        file_path = os.path.join(deps_files, filename)
555f9996aaSopenharmony_ci        module_deps_data = read_json_file(file_path)
565f9996aaSopenharmony_ci
575f9996aaSopenharmony_ci        part_name = module_deps_data.get("part_name")
585f9996aaSopenharmony_ci        deps = module_deps_data.get("deps")
595f9996aaSopenharmony_ci        module_label = module_deps_data.get("module_label").split("(")[0]
605f9996aaSopenharmony_ci        part_path = part_path_data.get(part_name, "no_part_path")
615f9996aaSopenharmony_ci
625f9996aaSopenharmony_ci        if part_name in ignore_parts:
635f9996aaSopenharmony_ci            continue
645f9996aaSopenharmony_ci        if part_path == "no_part_path":
655f9996aaSopenharmony_ci            if part_name not in not_exist_part:
665f9996aaSopenharmony_ci                print("Warning! Can not find part '{}' path".format(part_name))
675f9996aaSopenharmony_ci                not_exist_part.append(part_name)
685f9996aaSopenharmony_ci            continue
695f9996aaSopenharmony_ci
705f9996aaSopenharmony_ci        _wrong_used_deps = []
715f9996aaSopenharmony_ci        for dep in deps:
725f9996aaSopenharmony_ci            dep_path = dep[2:]
735f9996aaSopenharmony_ci            if not dep_path.startswith(part_path):
745f9996aaSopenharmony_ci                _wrong_used_deps.append(dep)
755f9996aaSopenharmony_ci        if len(_wrong_used_deps) > 0:
765f9996aaSopenharmony_ci            if part_name not in wrong_used_deps:
775f9996aaSopenharmony_ci                wrong_used_deps[part_name] = {module_label: _wrong_used_deps}
785f9996aaSopenharmony_ci            else:
795f9996aaSopenharmony_ci                wrong_used_deps[part_name][module_label] = _wrong_used_deps
805f9996aaSopenharmony_ci
815f9996aaSopenharmony_ci    output_file = os.path.join(os.path.dirname(
825f9996aaSopenharmony_ci        deps_files), "module_deps_info", "wrong_used_deps.json")
835f9996aaSopenharmony_ci    write_json_file(output_file, wrong_used_deps)
845f9996aaSopenharmony_ci    print("Check deps result output to '{}'.".format(output_file))
855f9996aaSopenharmony_ci
865f9996aaSopenharmony_ci
875f9996aaSopenharmony_cidef main(argv):
885f9996aaSopenharmony_ci    parser = argparse.ArgumentParser()
895f9996aaSopenharmony_ci    parser.add_argument('--parts-path-file', required=True)
905f9996aaSopenharmony_ci    parser.add_argument('--deps-path', required=True)
915f9996aaSopenharmony_ci    args = parser.parse_args(argv)
925f9996aaSopenharmony_ci    get_wrong_used_deps(args.parts_path_file, args.deps_path)
935f9996aaSopenharmony_ci    return 0
945f9996aaSopenharmony_ci
955f9996aaSopenharmony_ci
965f9996aaSopenharmony_ciif __name__ == '__main__':
975f9996aaSopenharmony_ci    sys.exit(main(sys.argv[1:]))
98