Yifan Hong | e3ba82c | 2019-08-21 13:29:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2019 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 | """ |
| 18 | Check VINTF compatibility from a target files package. |
| 19 | |
| 20 | Usage: check_target_files_vintf target_files |
| 21 | |
| 22 | target_files can be a ZIP file or an extracted target files directory. |
| 23 | """ |
| 24 | |
| 25 | import logging |
| 26 | import subprocess |
| 27 | import sys |
| 28 | import os |
| 29 | import zipfile |
| 30 | |
| 31 | import common |
| 32 | |
| 33 | logger = logging.getLogger(__name__) |
| 34 | |
| 35 | OPTIONS = common.OPTIONS |
| 36 | |
| 37 | # Keys are paths that VINTF searches. Must keep in sync with libvintf's search |
| 38 | # paths (VintfObject.cpp). |
| 39 | # These paths are stored in different directories in target files package, so |
| 40 | # we have to search for the correct path and tell checkvintf to remap them. |
Yifan Hong | 2870d1e | 2019-12-19 13:58:00 -0800 | [diff] [blame] | 41 | # Look for TARGET_COPY_OUT_* variables in board_config.mk for possible paths for |
| 42 | # each partition. |
Yifan Hong | e3ba82c | 2019-08-21 13:29:30 -0700 | [diff] [blame] | 43 | DIR_SEARCH_PATHS = { |
| 44 | '/system': ('SYSTEM',), |
| 45 | '/vendor': ('VENDOR', 'SYSTEM/vendor'), |
| 46 | '/product': ('PRODUCT', 'SYSTEM/product'), |
Yifan Hong | 2870d1e | 2019-12-19 13:58:00 -0800 | [diff] [blame] | 47 | '/odm': ('ODM', 'VENDOR/odm', 'SYSTEM/vendor/odm'), |
Yifan Hong | 9cbb624 | 2019-12-19 13:56:59 -0800 | [diff] [blame] | 48 | '/system_ext': ('SYSTEM_EXT', 'SYSTEM/system_ext'), |
Yifan Hong | e3ba82c | 2019-08-21 13:29:30 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | UNZIP_PATTERN = ['META/*', '*/build.prop'] |
| 52 | |
| 53 | |
| 54 | def GetDirmap(input_tmp): |
| 55 | dirmap = {} |
| 56 | for device_path, target_files_rel_paths in DIR_SEARCH_PATHS.items(): |
| 57 | for target_files_rel_path in target_files_rel_paths: |
| 58 | target_files_path = os.path.join(input_tmp, target_files_rel_path) |
| 59 | if os.path.isdir(target_files_path): |
| 60 | dirmap[device_path] = target_files_path |
| 61 | break |
| 62 | if device_path not in dirmap: |
| 63 | raise ValueError("Can't determine path for device path " + device_path + |
| 64 | ". Searched the following:" + |
| 65 | ("\n".join(target_files_rel_paths))) |
| 66 | return dirmap |
| 67 | |
| 68 | |
| 69 | def GetArgsForSkus(info_dict): |
Yifan Hong | 28ffd73 | 2020-03-13 13:11:10 -0700 | [diff] [blame] | 70 | odm_skus = info_dict.get('vintf_odm_manifest_skus', '').strip().split() |
Yifan Hong | 69430e6 | 2020-03-17 15:18:34 -0700 | [diff] [blame] | 71 | if info_dict.get('vintf_include_empty_odm_sku', '') == "true" or not odm_skus: |
Yifan Hong | 28ffd73 | 2020-03-13 13:11:10 -0700 | [diff] [blame] | 72 | odm_skus += [''] |
Yifan Hong | e3ba82c | 2019-08-21 13:29:30 -0700 | [diff] [blame] | 73 | |
Yifan Hong | 28ffd73 | 2020-03-13 13:11:10 -0700 | [diff] [blame] | 74 | vendor_skus = info_dict.get('vintf_vendor_manifest_skus', '').strip().split() |
Yifan Hong | 69430e6 | 2020-03-17 15:18:34 -0700 | [diff] [blame] | 75 | if info_dict.get('vintf_include_empty_vendor_sku', '') == "true" or \ |
| 76 | not vendor_skus: |
Yifan Hong | 28ffd73 | 2020-03-13 13:11:10 -0700 | [diff] [blame] | 77 | vendor_skus += [''] |
| 78 | |
| 79 | return [['--property', 'ro.boot.product.hardware.sku=' + odm_sku, |
| 80 | '--property', 'ro.boot.product.vendor.sku=' + vendor_sku] |
| 81 | for odm_sku in odm_skus for vendor_sku in vendor_skus] |
Yifan Hong | e3ba82c | 2019-08-21 13:29:30 -0700 | [diff] [blame] | 82 | |
| 83 | def GetArgsForShippingApiLevel(info_dict): |
| 84 | shipping_api_level = info_dict['vendor.build.prop'].get( |
| 85 | 'ro.product.first_api_level') |
| 86 | if not shipping_api_level: |
| 87 | logger.warning('Cannot determine ro.product.first_api_level') |
| 88 | return [] |
| 89 | return ['--property', 'ro.product.first_api_level=' + shipping_api_level] |
| 90 | |
| 91 | |
| 92 | def GetArgsForKernel(input_tmp): |
| 93 | version_path = os.path.join(input_tmp, 'META/kernel_version.txt') |
| 94 | config_path = os.path.join(input_tmp, 'META/kernel_configs.txt') |
| 95 | |
| 96 | if not os.path.isfile(version_path) or not os.path.isfile(config_path): |
Yifan Hong | 28ffd73 | 2020-03-13 13:11:10 -0700 | [diff] [blame] | 97 | logger.info('Skipping kernel config checks because ' |
Yifan Hong | e3ba82c | 2019-08-21 13:29:30 -0700 | [diff] [blame] | 98 | 'PRODUCT_OTA_ENFORCE_VINTF_KERNEL_REQUIREMENTS is not set') |
| 99 | return [] |
| 100 | |
| 101 | with open(version_path) as f: |
| 102 | version = f.read().strip() |
| 103 | |
| 104 | return ['--kernel', '{}:{}'.format(version, config_path)] |
| 105 | |
| 106 | |
| 107 | def CheckVintfFromExtractedTargetFiles(input_tmp, info_dict=None): |
| 108 | """ |
| 109 | Checks VINTF metadata of an extracted target files directory. |
| 110 | |
| 111 | Args: |
| 112 | inp: path to the directory that contains the extracted target files archive. |
| 113 | info_dict: The build-time info dict. If None, it will be loaded from inp. |
| 114 | |
| 115 | Returns: |
| 116 | True if VINTF check is skipped or compatible, False if incompatible. Raise |
| 117 | a RuntimeError if any error occurs. |
| 118 | """ |
| 119 | |
| 120 | if info_dict is None: |
| 121 | info_dict = common.LoadInfoDict(input_tmp) |
| 122 | |
| 123 | if info_dict.get('vintf_enforce') != 'true': |
| 124 | logger.warning('PRODUCT_ENFORCE_VINTF_MANIFEST is not set, skipping checks') |
| 125 | return True |
| 126 | |
| 127 | dirmap = GetDirmap(input_tmp) |
| 128 | args_for_skus = GetArgsForSkus(info_dict) |
| 129 | shipping_api_level_args = GetArgsForShippingApiLevel(info_dict) |
| 130 | kernel_args = GetArgsForKernel(input_tmp) |
| 131 | |
| 132 | common_command = [ |
| 133 | 'checkvintf', |
| 134 | '--check-compat', |
| 135 | ] |
| 136 | for device_path, real_path in dirmap.items(): |
| 137 | common_command += ['--dirmap', '{}:{}'.format(device_path, real_path)] |
| 138 | common_command += kernel_args |
| 139 | common_command += shipping_api_level_args |
| 140 | |
| 141 | success = True |
| 142 | for sku_args in args_for_skus: |
| 143 | command = common_command + sku_args |
| 144 | proc = common.Run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 145 | out, err = proc.communicate() |
| 146 | if proc.returncode == 0: |
| 147 | logger.info("Command `%s` returns 'compatible'", ' '.join(command)) |
| 148 | elif out.strip() == "INCOMPATIBLE": |
| 149 | logger.info("Command `%s` returns 'incompatible'", ' '.join(command)) |
| 150 | success = False |
| 151 | else: |
| 152 | raise common.ExternalError( |
| 153 | "Failed to run command '{}' (exit code {}):\nstdout:{}\nstderr:{}" |
| 154 | .format(' '.join(command), proc.returncode, out, err)) |
| 155 | logger.info("stdout: %s", out) |
| 156 | logger.info("stderr: %s", err) |
| 157 | |
| 158 | return success |
| 159 | |
| 160 | |
| 161 | def GetVintfFileList(): |
| 162 | """ |
| 163 | Returns a list of VINTF metadata files that should be read from a target files |
| 164 | package before executing checkvintf. |
| 165 | """ |
| 166 | def PathToPatterns(path): |
| 167 | if path[-1] == '/': |
| 168 | path += '*' |
| 169 | for device_path, target_files_rel_paths in DIR_SEARCH_PATHS.items(): |
| 170 | if path.startswith(device_path): |
| 171 | suffix = path[len(device_path):] |
| 172 | return [rel_path + suffix for rel_path in target_files_rel_paths] |
| 173 | raise RuntimeError('Unrecognized path from checkvintf --dump-file-list: ' + |
| 174 | path) |
| 175 | |
| 176 | out = common.RunAndCheckOutput(['checkvintf', '--dump-file-list']) |
| 177 | paths = out.strip().split('\n') |
| 178 | paths = sum((PathToPatterns(path) for path in paths if path), []) |
| 179 | return paths |
| 180 | |
| 181 | |
| 182 | def CheckVintfFromTargetFiles(inp, info_dict=None): |
| 183 | """ |
| 184 | Checks VINTF metadata of a target files zip. |
| 185 | |
| 186 | Args: |
| 187 | inp: path to the target files archive. |
| 188 | info_dict: The build-time info dict. If None, it will be loaded from inp. |
| 189 | |
| 190 | Returns: |
| 191 | True if VINTF check is skipped or compatible, False if incompatible. Raise |
| 192 | a RuntimeError if any error occurs. |
| 193 | """ |
| 194 | input_tmp = common.UnzipTemp(inp, GetVintfFileList() + UNZIP_PATTERN) |
| 195 | return CheckVintfFromExtractedTargetFiles(input_tmp, info_dict) |
| 196 | |
| 197 | |
| 198 | def CheckVintf(inp, info_dict=None): |
| 199 | """ |
| 200 | Checks VINTF metadata of a target files zip or extracted target files |
| 201 | directory. |
| 202 | |
| 203 | Args: |
| 204 | inp: path to the (possibly extracted) target files archive. |
| 205 | info_dict: The build-time info dict. If None, it will be loaded from inp. |
| 206 | |
| 207 | Returns: |
| 208 | True if VINTF check is skipped or compatible, False if incompatible. Raise |
| 209 | a RuntimeError if any error occurs. |
| 210 | """ |
| 211 | if os.path.isdir(inp): |
| 212 | logger.info('Checking VINTF compatibility extracted target files...') |
| 213 | return CheckVintfFromExtractedTargetFiles(inp, info_dict) |
| 214 | |
| 215 | if zipfile.is_zipfile(inp): |
| 216 | logger.info('Checking VINTF compatibility target files...') |
| 217 | return CheckVintfFromTargetFiles(inp, info_dict) |
| 218 | |
| 219 | raise ValueError('{} is not a valid directory or zip file'.format(inp)) |
| 220 | |
| 221 | |
| 222 | def main(argv): |
| 223 | args = common.ParseOptions(argv, __doc__) |
| 224 | if len(args) != 1: |
| 225 | common.Usage(__doc__) |
| 226 | sys.exit(1) |
| 227 | common.InitLogging() |
| 228 | if not CheckVintf(args[0]): |
| 229 | sys.exit(1) |
| 230 | |
| 231 | |
| 232 | if __name__ == '__main__': |
| 233 | try: |
| 234 | common.CloseInheritedPipes() |
| 235 | main(sys.argv[1:]) |
| 236 | except common.ExternalError: |
| 237 | logger.exception('\n ERROR:\n') |
| 238 | sys.exit(1) |
| 239 | finally: |
| 240 | common.Cleanup() |