1#!/usr/bin/env python 2# 3# Copyright (C) 2017 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"""Utility functions for VNDK snapshot.""" 18 19import glob 20import logging 21import os 22import re 23import subprocess 24import sys 25 26# Global Keys 27# All paths are relative to install_dir: prebuilts/vndk/v{version} 28ROOT_BP_PATH = 'Android.bp' 29COMMON_DIR_NAME = 'common' 30COMMON_DIR_PATH = COMMON_DIR_NAME 31COMMON_BP_PATH = os.path.join(COMMON_DIR_PATH, 'Android.bp') 32CONFIG_DIR_PATH_PATTERN = '*/configs' 33MANIFEST_FILE_NAME = 'manifest.xml' 34MODULE_PATHS_FILE_NAME = 'module_paths.txt' 35NOTICE_FILES_DIR_NAME = 'NOTICE_FILES' 36NOTICE_FILES_DIR_PATH = os.path.join(COMMON_DIR_PATH, NOTICE_FILES_DIR_NAME) 37BINDER32 = 'binder32' 38 39 40def set_logging_config(verbose_level): 41 verbose_map = (logging.WARNING, logging.INFO, logging.DEBUG) 42 verbosity = min(verbose_level, 2) 43 logging.basicConfig( 44 format='%(levelname)-8s [%(filename)s:%(lineno)d] %(message)s', 45 level=verbose_map[verbosity]) 46 47 48def check_call(cmd): 49 logging.debug('Running `{}`'.format(' '.join(cmd))) 50 subprocess.check_call(cmd) 51 52 53def check_output(cmd): 54 logging.debug('Running `{}`'.format(' '.join(cmd))) 55 output = subprocess.check_output(cmd) 56 logging.debug('Output: `{}`'.format(output)) 57 return output 58 59 60def get_android_build_top(): 61 ANDROID_BUILD_TOP = os.getenv('ANDROID_BUILD_TOP') 62 if not ANDROID_BUILD_TOP: 63 print('Error: Missing ANDROID_BUILD_TOP env variable. Please run ' 64 '\'. build/envsetup.sh; lunch <build target>\'. Exiting script.') 65 sys.exit(1) 66 return ANDROID_BUILD_TOP 67 68 69def join_realpath(root, *args): 70 return os.path.realpath(os.path.join(root, *args)) 71 72 73def _get_dir_from_env(env_var, default): 74 return os.path.realpath(os.getenv(env_var, default)) 75 76 77def get_out_dir(android_build_top): 78 return _get_dir_from_env('OUT_DIR', join_realpath(android_build_top, 79 'out')) 80 81 82def get_dist_dir(out_dir): 83 return _get_dir_from_env('DIST_DIR', join_realpath(out_dir, 'dist')) 84 85 86def get_snapshot_archs(install_dir): 87 """Returns a list of VNDK snapshot arch flavors under install_dir. 88 89 Args: 90 install_dir: string, absolute path of prebuilts/vndk/v{version} 91 """ 92 archs = [] 93 for file in glob.glob('{}/*'.format(install_dir)): 94 basename = os.path.basename(file) 95 if os.path.isdir(file) and basename != COMMON_DIR_NAME: 96 archs.append(basename) 97 return archs 98 99 100def prebuilt_arch_from_path(path): 101 """Extracts arch of prebuilts from path relative to install_dir. 102 103 Args: 104 path: string, path relative to prebuilts/vndk/v{version} 105 106 Returns: 107 string, arch of prebuilt (e.g., 'arm' or 'arm64' or 'x86' or 'x86_64') 108 """ 109 return path.split('/')[1].split('-')[1] 110 111 112def snapshot_arch_from_path(path): 113 """Extracts VNDK snapshot arch from path relative to install_dir. 114 115 Args: 116 path: string, path relative to prebuilts/vndk/v{version} 117 118 Returns: 119 string, VNDK snapshot arch (e.g. 'arm64') 120 """ 121 return path.split('/')[0] 122 123 124def find(path, names): 125 """Returns a list of files in a directory that match the given names. 126 127 Args: 128 path: string, absolute path of directory from which to find files 129 names: list of strings, names of the files to find 130 """ 131 found = [] 132 for root, _, files in os.walk(path): 133 for file_name in sorted(files): 134 if file_name in names: 135 abspath = os.path.abspath(os.path.join(root, file_name)) 136 rel_to_root = abspath.replace(os.path.abspath(path), '') 137 found.append(rel_to_root[1:]) # strip leading / 138 return found 139 140 141def fetch_artifact(branch, build, pattern, destination='.'): 142 """Fetches build artifacts from Android Build server. 143 144 Args: 145 branch: string, branch to pull build artifacts from 146 build: string, build number to pull build artifacts from 147 pattern: string, pattern of build artifact file name 148 destination: string, destination to pull build artifact to 149 """ 150 fetch_artifact_path = '/google/data/ro/projects/android/fetch_artifact' 151 cmd = [ 152 fetch_artifact_path, '--branch', branch, '--target=vndk', '--bid', 153 build, pattern, destination 154 ] 155 check_call(cmd) 156