1#!/usr/bin/env python 2# 3# Copyright (C) 2023 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18from pathlib import Path 19import glob 20import os 21import subprocess 22 23def parse_set(param : str) -> set[str]: 24 """Parse symbol set based on a file or comma-separate symbols.""" 25 symbol_set = set() 26 if len(param) == 0: 27 return symbol_set 28 29 if param[0] == "@": 30 with open(param[1:], "r") as f: 31 for line in f: 32 line = line.strip() 33 symbol_set.add(line) 34 return symbol_set 35 36 list_symbols = param.split(",") 37 symbol_set.update(list_symbols) 38 return symbol_set 39 40def parse_list(param : str) -> list[str]: 41 """Parse partial order based on a file or comma-separate symbols.""" 42 symbol_order = [] 43 if len(param) == 0: 44 return symbol_order 45 46 if param[0] == "@": 47 with open(param[1:], "r") as f: 48 for line in f: 49 line = line.strip() 50 symbol_order.append(line) 51 return symbol_order 52 53 symbol_order = param.split(",") 54 return symbol_order 55 56def parse_merge_list(param : str) -> list[tuple[str,int]]: 57 """Parse partial order based on a file, folder, or comma-separate symbols.""" 58 file_list = [] 59 if len(param) == 0: 60 return file_list 61 62 if param[0] == "@": 63 file_dir = Path(param[1:]).resolve().parent 64 with open(param[1:], "r") as f: 65 for line in f: 66 line = line.strip() 67 line_list = line.split(",") 68 # Name, Weight 69 file_list.append((file_dir / line_list[0], int(line_list[1]))) 70 return file_list 71 72 if param[0] == "^": 73 file_lst = glob.glob(param[1:]+"/*.orderfile") 74 # Assumig weight of 1 for all the files. Sorting of files provides 75 # a deterministic order of orderfile. 76 file_list = sorted([(orderfile, 1) for orderfile in file_lst]) 77 return file_list 78 79 file_lst = param.split(",") 80 file_list = [(orderfile, 1) for orderfile in file_lst] 81 return file_list 82 83def check_call(cmd, *args, **kwargs): 84 """subprocess.check_call.""" 85 subprocess.check_call(cmd, *args, **kwargs) 86 87 88def check_output(cmd, *args, **kwargs): 89 """subprocess.check_output.""" 90 return subprocess.run( 91 cmd, *args, **kwargs, check=True, text=True, 92 stdout=subprocess.PIPE).stdout 93 94def check_error(cmd, *args, **kwargs): 95 """subprocess.check_error.""" 96 return subprocess.run( 97 cmd, *args, **kwargs, check=True, text=True, 98 stdout=subprocess.PIPE, stderr=subprocess.STDOUT).stdout 99 100def android_build_top(): 101 """Get top directory to find files.""" 102 THIS_DIR = os.path.realpath(os.path.dirname(__file__)) 103 return os.path.realpath(os.path.join(THIS_DIR, '../../../..'))