Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 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 | |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 19 | import glob |
Jae Shin | 9407521 | 2018-06-14 14:54:34 +0900 | [diff] [blame] | 20 | import logging |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 21 | import os |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 22 | import re |
Jae Shin | 25d3abf | 2018-04-18 18:00:26 +0900 | [diff] [blame] | 23 | import subprocess |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 24 | import sys |
| 25 | |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 26 | # Global Keys |
| 27 | # All paths are relative to install_dir: prebuilts/vndk/v{version} |
Jaewoong Jung | 6a5aaca | 2019-01-17 15:41:06 -0800 | [diff] [blame] | 28 | ROOT_BP_PATH = 'Android.bp' |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 29 | COMMON_DIR_NAME = 'common' |
| 30 | COMMON_DIR_PATH = COMMON_DIR_NAME |
Jaewoong Jung | 6fbb9d2 | 2018-11-28 09:25:22 -0800 | [diff] [blame] | 31 | COMMON_BP_PATH = os.path.join(COMMON_DIR_PATH, 'Android.bp') |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 32 | CONFIG_DIR_PATH_PATTERN = '*/configs' |
| 33 | MANIFEST_FILE_NAME = 'manifest.xml' |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 34 | MODULE_PATHS_FILE_NAME = 'module_paths.txt' |
| 35 | NOTICE_FILES_DIR_NAME = 'NOTICE_FILES' |
| 36 | NOTICE_FILES_DIR_PATH = os.path.join(COMMON_DIR_PATH, NOTICE_FILES_DIR_NAME) |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 37 | BINDER32 = 'binder32' |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 38 | |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 39 | |
Jae Shin | 9407521 | 2018-06-14 14:54:34 +0900 | [diff] [blame] | 40 | def 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 | |
Jae Shin | bd82ebb | 2018-06-21 14:13:46 +0900 | [diff] [blame] | 48 | def check_call(cmd): |
| 49 | logging.debug('Running `{}`'.format(' '.join(cmd))) |
Jae Shin | 9407521 | 2018-06-14 14:54:34 +0900 | [diff] [blame] | 50 | subprocess.check_call(cmd) |
| 51 | |
| 52 | |
Jae Shin | bd82ebb | 2018-06-21 14:13:46 +0900 | [diff] [blame] | 53 | def check_output(cmd): |
| 54 | logging.debug('Running `{}`'.format(' '.join(cmd))) |
Jae Shin | ce3bb75 | 2018-06-18 12:40:53 +0900 | [diff] [blame] | 55 | output = subprocess.check_output(cmd) |
Jae Shin | bd82ebb | 2018-06-21 14:13:46 +0900 | [diff] [blame] | 56 | logging.debug('Output: `{}`'.format(output)) |
Jae Shin | ce3bb75 | 2018-06-18 12:40:53 +0900 | [diff] [blame] | 57 | return output |
| 58 | |
| 59 | |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 60 | def 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 | |
| 69 | def join_realpath(root, *args): |
| 70 | return os.path.realpath(os.path.join(root, *args)) |
| 71 | |
| 72 | |
| 73 | def _get_dir_from_env(env_var, default): |
| 74 | return os.path.realpath(os.getenv(env_var, default)) |
| 75 | |
| 76 | |
| 77 | def get_out_dir(android_build_top): |
| 78 | return _get_dir_from_env('OUT_DIR', join_realpath(android_build_top, |
| 79 | 'out')) |
| 80 | |
| 81 | |
| 82 | def get_dist_dir(out_dir): |
| 83 | return _get_dir_from_env('DIST_DIR', join_realpath(out_dir, 'dist')) |
| 84 | |
| 85 | |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 86 | def get_snapshot_archs(install_dir): |
| 87 | """Returns a list of VNDK snapshot arch flavors under install_dir. |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 88 | |
| 89 | Args: |
| 90 | install_dir: string, absolute path of prebuilts/vndk/v{version} |
| 91 | """ |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 92 | archs = [] |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 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: |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 96 | archs.append(basename) |
| 97 | return archs |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 98 | |
| 99 | |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 100 | def prebuilt_arch_from_path(path): |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 101 | """Extracts arch of prebuilts from path relative to install_dir. |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 102 | |
| 103 | Args: |
| 104 | path: string, path relative to prebuilts/vndk/v{version} |
| 105 | |
| 106 | Returns: |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 107 | string, arch of prebuilt (e.g., 'arm' or 'arm64' or 'x86' or 'x86_64') |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 108 | """ |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 109 | return path.split('/')[1].split('-')[1] |
| 110 | |
| 111 | |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 112 | def snapshot_arch_from_path(path): |
| 113 | """Extracts VNDK snapshot arch from path relative to install_dir. |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 114 | |
| 115 | Args: |
| 116 | path: string, path relative to prebuilts/vndk/v{version} |
| 117 | |
| 118 | Returns: |
Jae Shin | af0c003 | 2018-06-21 11:54:05 +0900 | [diff] [blame] | 119 | string, VNDK snapshot arch (e.g. 'arm64') |
Jae Shin | 4db81a5 | 2017-12-29 13:24:54 +0900 | [diff] [blame] | 120 | """ |
| 121 | return path.split('/')[0] |
Jae Shin | 5233fe1 | 2017-12-18 22:29:48 +0900 | [diff] [blame] | 122 | |
| 123 | |
| 124 | def 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 |
Jae Shin | 25d3abf | 2018-04-18 18:00:26 +0900 | [diff] [blame] | 139 | |
| 140 | |
| 141 | def 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 | ] |
Jae Shin | bd82ebb | 2018-06-21 14:13:46 +0900 | [diff] [blame] | 155 | check_call(cmd) |