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