1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3# Copyright (c) 2025 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 17from common_utils import get_code_dir, load_config 18 19 20def get_parts_tag_config(part_names: list) -> set: 21 if not part_names: 22 return None 23 config_data = _load_part_download_config() 24 all_required_tags = set() 25 for part in part_names: 26 all_required_tags.update(_get_tags_by_part(config_data, part)) 27 all_required_tags.add("base") 28 print( 29 "Required tags for parts {}: {}".format( 30 ",".join(part_names), sorted(all_required_tags) 31 ) 32 ) 33 return all_required_tags 34 35 36def _load_part_download_config() -> dict: 37 current_dir = os.path.dirname(os.path.abspath(__file__)) 38 config_file = os.path.join(current_dir, "part_prebuilts_config.json") 39 if not os.path.exists(config_file): 40 raise FileNotFoundError("Config file does not exist: {}".format(config_file)) 41 return load_config(config_file) 42 43 44def _get_tags_by_part(config_data: dict, part: str) -> set: 45 all_tags = _load_all_tags() 46 if part not in config_data: 47 return set() 48 configured_tags = set(config_data[part]) 49 custom_tags = configured_tags - all_tags 50 if custom_tags: 51 raise Exception( 52 f"Custom tags {custom_tags} are not defined in prebuilts_config.json" 53 ) 54 return configured_tags 55 56 57def _load_all_tags() -> set: 58 code_dir = get_code_dir() 59 prebuilts_config_json = os.path.join(code_dir, "build", "prebuilts_config.json") 60 if not os.path.exists(prebuilts_config_json): 61 raise FileNotFoundError( 62 "Config file does not exist: {}".format(prebuilts_config_json) 63 ) 64 tool_configs = load_config(prebuilts_config_json)["tool_list"] 65 tags = {tool.get("tag") for tool in tool_configs} 66 return tags 67