Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [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"); you may not |
| 6 | # use this file except in compliance with the License. You may obtain a copy of |
| 7 | # 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, WITHOUT |
| 13 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | # License for the specific language governing permissions and limitations under |
| 15 | # the License. |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 16 | """This script merges two partial target files packages. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 17 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 18 | One package contains system files, and the other contains non-system files. |
| 19 | It produces a complete target files package that can be used to generate an |
| 20 | OTA package. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 21 | |
| 22 | Usage: merge_target_files.py [args] |
| 23 | |
| 24 | --system-target-files system-target-files-zip-archive |
| 25 | The input target files package containing system bits. This is a zip |
| 26 | archive. |
| 27 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 28 | --system-item-list system-item-list-file |
| 29 | The optional path to a newline-separated config file that replaces the |
| 30 | contents of default_system_item_list if provided. |
| 31 | |
| 32 | --system-misc-info-keys system-misc-info-keys-file |
| 33 | The optional path to a newline-separated config file that replaces the |
| 34 | contents of default_system_misc_info_keys if provided. |
| 35 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 36 | --other-target-files other-target-files-zip-archive |
| 37 | The input target files package containing other bits. This is a zip |
| 38 | archive. |
| 39 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 40 | --other-item-list other-item-list-file |
| 41 | The optional path to a newline-separated config file that replaces the |
| 42 | contents of default_other_item_list if provided. |
| 43 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 44 | --output-target-files output-target-files-package |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 45 | If provided, the output merged target files package. Also a zip archive. |
| 46 | |
| 47 | --output-dir output-directory |
| 48 | If provided, the destination directory for saving merged files. Requires |
| 49 | the --output-item-list flag. |
| 50 | Can be provided alongside --output-target-files, or by itself. |
| 51 | |
| 52 | --output-item-list output-item-list-file. |
| 53 | The optional path to a newline-separated config file that specifies the |
| 54 | file patterns to copy into the --output-dir. Required if providing |
| 55 | the --output-dir flag. |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 56 | |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 57 | --output-ota output-ota-package |
| 58 | The output ota package. This is a zip archive. Use of this flag may |
| 59 | require passing the --path common flag; see common.py. |
| 60 | |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 61 | --output-img output-img-package |
| 62 | The output img package, suitable for use with 'fastboot update'. Use of |
| 63 | this flag may require passing the --path common flag; see common.py. |
| 64 | |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 65 | --output-super-empty output-super-empty-image |
| 66 | If provided, creates a super_empty.img file from the merged target |
| 67 | files package and saves it at this path. |
| 68 | |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 69 | --rebuild_recovery |
| 70 | Rebuild the recovery patch used by non-A/B devices and write it to the |
| 71 | system image. |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 72 | |
| 73 | --keep-tmp |
| 74 | Keep tempoary files for debugging purposes. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 75 | """ |
| 76 | |
| 77 | from __future__ import print_function |
| 78 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 79 | import fnmatch |
| 80 | import logging |
| 81 | import os |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 82 | import shutil |
Bill Peckham | 540d91a | 2019-04-25 14:18:16 -0700 | [diff] [blame] | 83 | import subprocess |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 84 | import sys |
| 85 | import zipfile |
| 86 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 87 | import add_img_to_target_files |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 88 | import build_super_image |
| 89 | import common |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 90 | import img_from_target_files |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 91 | import ota_from_target_files |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 92 | |
| 93 | logger = logging.getLogger(__name__) |
| 94 | OPTIONS = common.OPTIONS |
| 95 | OPTIONS.verbose = True |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 96 | OPTIONS.system_target_files = None |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 97 | OPTIONS.system_item_list = None |
| 98 | OPTIONS.system_misc_info_keys = None |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 99 | OPTIONS.other_target_files = None |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 100 | OPTIONS.other_item_list = None |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 101 | OPTIONS.output_target_files = None |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 102 | OPTIONS.output_dir = None |
| 103 | OPTIONS.output_item_list = None |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 104 | OPTIONS.output_ota = None |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 105 | OPTIONS.output_img = None |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 106 | OPTIONS.output_super_empty = None |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 107 | OPTIONS.rebuild_recovery = False |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 108 | OPTIONS.keep_tmp = False |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 109 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 110 | # default_system_item_list is a list of items to extract from the partial |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 111 | # system target files package as is, meaning these items will land in the |
| 112 | # output target files package exactly as they appear in the input partial |
| 113 | # system target files package. |
| 114 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 115 | default_system_item_list = [ |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 116 | 'META/apkcerts.txt', |
| 117 | 'META/filesystem_config.txt', |
| 118 | 'META/root_filesystem_config.txt', |
| 119 | 'META/system_manifest.xml', |
| 120 | 'META/system_matrix.xml', |
| 121 | 'META/update_engine_config.txt', |
| 122 | 'PRODUCT/*', |
| 123 | 'ROOT/*', |
| 124 | 'SYSTEM/*', |
| 125 | ] |
| 126 | |
| 127 | # system_extract_special_item_list is a list of items to extract from the |
| 128 | # partial system target files package that need some special processing, such |
| 129 | # as some sort of combination with items from the partial other target files |
| 130 | # package. |
| 131 | |
| 132 | system_extract_special_item_list = [ |
| 133 | 'META/*', |
| 134 | ] |
| 135 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 136 | # default_system_misc_info_keys is a list of keys to obtain from the system |
| 137 | # instance of META/misc_info.txt. The remaining keys from the other instance. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 138 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 139 | default_system_misc_info_keys = [ |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 140 | 'avb_system_hashtree_enable', |
| 141 | 'avb_system_add_hashtree_footer_args', |
| 142 | 'avb_system_key_path', |
| 143 | 'avb_system_algorithm', |
| 144 | 'avb_system_rollback_index_location', |
| 145 | 'avb_product_hashtree_enable', |
| 146 | 'avb_product_add_hashtree_footer_args', |
| 147 | 'avb_product_services_hashtree_enable', |
| 148 | 'avb_product_services_add_hashtree_footer_args', |
| 149 | 'system_root_image', |
| 150 | 'root_dir', |
| 151 | 'ab_update', |
| 152 | 'default_system_dev_certificate', |
| 153 | 'system_size', |
| 154 | ] |
| 155 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 156 | # default_other_item_list is a list of items to extract from the partial |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 157 | # other target files package as is, meaning these items will land in the output |
| 158 | # target files package exactly as they appear in the input partial other target |
| 159 | # files package. |
| 160 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 161 | default_other_item_list = [ |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 162 | 'META/boot_filesystem_config.txt', |
Bill Peckham | 736b223 | 2019-05-06 12:50:55 -0700 | [diff] [blame^] | 163 | 'META/file_contexts.bin', |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 164 | 'META/otakeys.txt', |
| 165 | 'META/releasetools.py', |
| 166 | 'META/vendor_filesystem_config.txt', |
| 167 | 'META/vendor_manifest.xml', |
| 168 | 'META/vendor_matrix.xml', |
| 169 | 'BOOT/*', |
| 170 | 'DATA/*', |
| 171 | 'ODM/*', |
| 172 | 'OTA/android-info.txt', |
| 173 | 'PREBUILT_IMAGES/*', |
| 174 | 'RADIO/*', |
| 175 | 'VENDOR/*', |
| 176 | ] |
| 177 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 178 | # other_extract_special_item_list is a list of items to extract from the |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 179 | # partial other target files package that need some special processing, such as |
| 180 | # some sort of combination with items from the partial system target files |
| 181 | # package. |
| 182 | |
| 183 | other_extract_special_item_list = [ |
| 184 | 'META/*', |
| 185 | ] |
| 186 | |
| 187 | |
| 188 | def extract_items(target_files, target_files_temp_dir, extract_item_list): |
| 189 | """Extract items from target files to temporary directory. |
| 190 | |
| 191 | This function extracts from the specified target files zip archive into the |
| 192 | specified temporary directory, the items specified in the extract item list. |
| 193 | |
| 194 | Args: |
| 195 | target_files: The target files zip archive from which to extract items. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 196 | target_files_temp_dir: The temporary directory where the extracted items |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 197 | will land. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 198 | extract_item_list: A list of items to extract. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 199 | """ |
| 200 | |
| 201 | logger.info('extracting from %s', target_files) |
| 202 | |
| 203 | # Filter the extract_item_list to remove any items that do not exist in the |
| 204 | # zip file. Otherwise, the extraction step will fail. |
| 205 | |
| 206 | with zipfile.ZipFile( |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 207 | target_files, 'r', allowZip64=True) as target_files_zipfile: |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 208 | target_files_namelist = target_files_zipfile.namelist() |
| 209 | |
| 210 | filtered_extract_item_list = [] |
| 211 | for pattern in extract_item_list: |
| 212 | matching_namelist = fnmatch.filter(target_files_namelist, pattern) |
| 213 | if not matching_namelist: |
| 214 | logger.warning('no match for %s', pattern) |
| 215 | else: |
| 216 | filtered_extract_item_list.append(pattern) |
| 217 | |
Bill Peckham | 8ff3fbd | 2019-02-22 10:57:43 -0800 | [diff] [blame] | 218 | # Extract from target_files into target_files_temp_dir the |
| 219 | # filtered_extract_item_list. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 220 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 221 | common.UnzipToDir(target_files, target_files_temp_dir, |
| 222 | filtered_extract_item_list) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 223 | |
| 224 | |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 225 | def copy_items(from_dir, to_dir, patterns): |
| 226 | """Similar to extract_items() except uses an input dir instead of zip.""" |
| 227 | file_paths = [] |
| 228 | for dirpath, _, filenames in os.walk(from_dir): |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 229 | file_paths.extend( |
| 230 | os.path.relpath(path=os.path.join(dirpath, filename), start=from_dir) |
| 231 | for filename in filenames) |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 232 | |
| 233 | filtered_file_paths = set() |
| 234 | for pattern in patterns: |
| 235 | filtered_file_paths.update(fnmatch.filter(file_paths, pattern)) |
| 236 | |
| 237 | for file_path in filtered_file_paths: |
| 238 | original_file_path = os.path.join(from_dir, file_path) |
| 239 | copied_file_path = os.path.join(to_dir, file_path) |
| 240 | copied_file_dir = os.path.dirname(copied_file_path) |
| 241 | if not os.path.exists(copied_file_dir): |
| 242 | os.makedirs(copied_file_dir) |
| 243 | if os.path.islink(original_file_path): |
| 244 | os.symlink(os.readlink(original_file_path), copied_file_path) |
| 245 | else: |
| 246 | shutil.copyfile(original_file_path, copied_file_path) |
| 247 | |
| 248 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 249 | def read_config_list(config_file_path): |
| 250 | """Reads a config file into a list of strings. |
| 251 | |
| 252 | Expects the file to be newline-separated. |
| 253 | |
| 254 | Args: |
| 255 | config_file_path: The path to the config file to open and read. |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 256 | |
| 257 | Returns: |
| 258 | The list of strings in the config file. |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 259 | """ |
| 260 | with open(config_file_path) as config_file: |
| 261 | return config_file.read().splitlines() |
| 262 | |
| 263 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 264 | def validate_config_lists(system_item_list, system_misc_info_keys, |
| 265 | other_item_list): |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 266 | """Performs validations on the merge config lists. |
| 267 | |
| 268 | Args: |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 269 | system_item_list: The list of items to extract from the partial system |
| 270 | target files package as is. |
| 271 | system_misc_info_keys: A list of keys to obtain from the system instance of |
| 272 | META/misc_info.txt. The remaining keys from the other instance. |
| 273 | other_item_list: The list of items to extract from the partial other target |
| 274 | files package as is. |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 275 | |
| 276 | Returns: |
| 277 | False if a validation fails, otherwise true. |
| 278 | """ |
| 279 | default_combined_item_set = set(default_system_item_list) |
| 280 | default_combined_item_set.update(default_other_item_list) |
| 281 | |
| 282 | combined_item_set = set(system_item_list) |
| 283 | combined_item_set.update(other_item_list) |
| 284 | |
| 285 | # Check that the merge config lists are not missing any item specified |
| 286 | # by the default config lists. |
| 287 | difference = default_combined_item_set.difference(combined_item_set) |
| 288 | if difference: |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 289 | logger.error('Missing merge config items: %s', list(difference)) |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 290 | logger.error('Please ensure missing items are in either the ' |
| 291 | 'system-item-list or other-item-list files provided to ' |
| 292 | 'this script.') |
| 293 | return False |
| 294 | |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 295 | if ('dynamic_partition_list' in system_misc_info_keys) or ( |
| 296 | 'super_partition_groups' in system_misc_info_keys): |
| 297 | logger.error('Dynamic partition misc info keys should come from ' |
| 298 | 'the other instance of META/misc_info.txt.') |
| 299 | return False |
| 300 | |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 301 | return True |
| 302 | |
| 303 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 304 | def process_ab_partitions_txt(system_target_files_temp_dir, |
| 305 | other_target_files_temp_dir, |
| 306 | output_target_files_temp_dir): |
| 307 | """Perform special processing for META/ab_partitions.txt. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 308 | |
| 309 | This function merges the contents of the META/ab_partitions.txt files from |
| 310 | the system directory and the other directory, placing the merged result in |
| 311 | the output directory. The precondition in that the files are already |
| 312 | extracted. The post condition is that the output META/ab_partitions.txt |
| 313 | contains the merged content. The format for each ab_partitions.txt a one |
| 314 | partition name per line. The output file contains the union of the parition |
| 315 | names. |
| 316 | |
| 317 | Args: |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 318 | system_target_files_temp_dir: The name of a directory containing the special |
| 319 | items extracted from the system target files package. |
| 320 | other_target_files_temp_dir: The name of a directory containing the special |
| 321 | items extracted from the other target files package. |
| 322 | output_target_files_temp_dir: The name of a directory that will be used to |
| 323 | create the output target files package after all the special cases are |
| 324 | processed. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 325 | """ |
| 326 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 327 | system_ab_partitions_txt = os.path.join(system_target_files_temp_dir, 'META', |
| 328 | 'ab_partitions.txt') |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 329 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 330 | other_ab_partitions_txt = os.path.join(other_target_files_temp_dir, 'META', |
| 331 | 'ab_partitions.txt') |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 332 | |
| 333 | with open(system_ab_partitions_txt) as f: |
| 334 | system_ab_partitions = f.read().splitlines() |
| 335 | |
| 336 | with open(other_ab_partitions_txt) as f: |
| 337 | other_ab_partitions = f.read().splitlines() |
| 338 | |
| 339 | output_ab_partitions = set(system_ab_partitions + other_ab_partitions) |
| 340 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 341 | output_ab_partitions_txt = os.path.join(output_target_files_temp_dir, 'META', |
| 342 | 'ab_partitions.txt') |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 343 | |
| 344 | with open(output_ab_partitions_txt, 'w') as output: |
| 345 | for partition in sorted(output_ab_partitions): |
| 346 | output.write('%s\n' % partition) |
| 347 | |
| 348 | |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 349 | def append_recovery_to_filesystem_config(output_target_files_temp_dir): |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 350 | """Perform special processing for META/filesystem_config.txt. |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 351 | |
| 352 | This function appends recovery information to META/filesystem_config.txt |
| 353 | so that recovery patch regeneration will succeed. |
| 354 | |
| 355 | Args: |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 356 | output_target_files_temp_dir: The name of a directory that will be used to |
| 357 | create the output target files package after all the special cases are |
| 358 | processed. We find filesystem_config.txt here. |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 359 | """ |
| 360 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 361 | filesystem_config_txt = os.path.join(output_target_files_temp_dir, 'META', |
| 362 | 'filesystem_config.txt') |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 363 | |
| 364 | with open(filesystem_config_txt, 'a') as f: |
| 365 | # TODO(bpeckham) this data is hard coded. It should be generated |
| 366 | # programmatically. |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 367 | f.write('system/bin/install-recovery.sh 0 0 750 ' |
| 368 | 'selabel=u:object_r:install_recovery_exec:s0 capabilities=0x0\n') |
| 369 | f.write('system/recovery-from-boot.p 0 0 644 ' |
| 370 | 'selabel=u:object_r:system_file:s0 capabilities=0x0\n') |
| 371 | f.write('system/etc/recovery.img 0 0 440 ' |
| 372 | 'selabel=u:object_r:install_recovery_exec:s0 capabilities=0x0\n') |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 373 | |
| 374 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 375 | def process_misc_info_txt(system_target_files_temp_dir, |
| 376 | other_target_files_temp_dir, |
| 377 | output_target_files_temp_dir, system_misc_info_keys): |
| 378 | """Perform special processing for META/misc_info.txt. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 379 | |
| 380 | This function merges the contents of the META/misc_info.txt files from the |
| 381 | system directory and the other directory, placing the merged result in the |
| 382 | output directory. The precondition in that the files are already extracted. |
| 383 | The post condition is that the output META/misc_info.txt contains the merged |
| 384 | content. |
| 385 | |
| 386 | Args: |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 387 | system_target_files_temp_dir: The name of a directory containing the special |
| 388 | items extracted from the system target files package. |
| 389 | other_target_files_temp_dir: The name of a directory containing the special |
| 390 | items extracted from the other target files package. |
| 391 | output_target_files_temp_dir: The name of a directory that will be used to |
| 392 | create the output target files package after all the special cases are |
| 393 | processed. |
| 394 | system_misc_info_keys: A list of keys to obtain from the system instance of |
| 395 | META/misc_info.txt. The remaining keys from the other instance. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 396 | """ |
| 397 | |
| 398 | def read_helper(d): |
| 399 | misc_info_txt = os.path.join(d, 'META', 'misc_info.txt') |
| 400 | with open(misc_info_txt) as f: |
| 401 | return list(f.read().splitlines()) |
| 402 | |
| 403 | system_info_dict = common.LoadDictionaryFromLines( |
| 404 | read_helper(system_target_files_temp_dir)) |
| 405 | |
| 406 | # We take most of the misc info from the other target files. |
| 407 | |
| 408 | merged_info_dict = common.LoadDictionaryFromLines( |
| 409 | read_helper(other_target_files_temp_dir)) |
| 410 | |
| 411 | # Replace certain values in merged_info_dict with values from |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 412 | # system_info_dict. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 413 | |
| 414 | for key in system_misc_info_keys: |
| 415 | merged_info_dict[key] = system_info_dict[key] |
| 416 | |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 417 | # Merge misc info keys used for Dynamic Partitions. |
| 418 | if (merged_info_dict.get('use_dynamic_partitions') == 'true') and ( |
| 419 | system_info_dict.get('use_dynamic_partitions') == 'true'): |
| 420 | merged_info_dict['dynamic_partition_list'] = '%s %s' % ( |
| 421 | system_info_dict.get('dynamic_partition_list', ''), |
| 422 | merged_info_dict.get('dynamic_partition_list', '')) |
| 423 | # Partition groups and group sizes are defined by the other (non-system) |
| 424 | # misc info file because these values may vary for each board that uses |
| 425 | # a shared system image. |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 426 | for partition_group in merged_info_dict['super_partition_groups'].split( |
| 427 | ' '): |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 428 | if ('super_%s_group_size' % partition_group) not in merged_info_dict: |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 429 | raise ValueError( |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 430 | 'Other META/misc_info.txt does not contain required key ' |
| 431 | 'super_%s_group_size.' % partition_group) |
| 432 | key = 'super_%s_partition_list' % partition_group |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 433 | merged_info_dict[key] = '%s %s' % (system_info_dict.get( |
| 434 | key, ''), merged_info_dict.get(key, '')) |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 435 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 436 | output_misc_info_txt = os.path.join(output_target_files_temp_dir, 'META', |
| 437 | 'misc_info.txt') |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 438 | |
| 439 | sorted_keys = sorted(merged_info_dict.keys()) |
| 440 | |
| 441 | with open(output_misc_info_txt, 'w') as output: |
| 442 | for key in sorted_keys: |
| 443 | output.write('{}={}\n'.format(key, merged_info_dict[key])) |
| 444 | |
| 445 | |
Bill Peckham | 736b223 | 2019-05-06 12:50:55 -0700 | [diff] [blame^] | 446 | def process_special_cases(system_target_files_temp_dir, |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 447 | other_target_files_temp_dir, |
| 448 | output_target_files_temp_dir, system_misc_info_keys, |
| 449 | rebuild_recovery): |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 450 | """Perform special-case processing for certain target files items. |
| 451 | |
| 452 | Certain files in the output target files package require special-case |
| 453 | processing. This function performs all that special-case processing. |
| 454 | |
| 455 | Args: |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 456 | system_target_files_temp_dir: The name of a directory containing the special |
| 457 | items extracted from the system target files package. |
| 458 | other_target_files_temp_dir: The name of a directory containing the special |
| 459 | items extracted from the other target files package. |
| 460 | output_target_files_temp_dir: The name of a directory that will be used to |
| 461 | create the output target files package after all the special cases are |
| 462 | processed. |
| 463 | system_misc_info_keys: A list of keys to obtain from the system instance of |
| 464 | META/misc_info.txt. The remaining keys from the other instance. |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 465 | rebuild_recovery: If true, rebuild the recovery patch used by non-A/B |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 466 | devices and write it to the system image. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 467 | """ |
| 468 | |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 469 | if 'ab_update' in system_misc_info_keys: |
| 470 | process_ab_partitions_txt( |
| 471 | system_target_files_temp_dir=system_target_files_temp_dir, |
| 472 | other_target_files_temp_dir=other_target_files_temp_dir, |
| 473 | output_target_files_temp_dir=output_target_files_temp_dir) |
| 474 | |
| 475 | if rebuild_recovery: |
| 476 | append_recovery_to_filesystem_config( |
| 477 | output_target_files_temp_dir=output_target_files_temp_dir) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 478 | |
| 479 | process_misc_info_txt( |
| 480 | system_target_files_temp_dir=system_target_files_temp_dir, |
| 481 | other_target_files_temp_dir=other_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 482 | output_target_files_temp_dir=output_target_files_temp_dir, |
| 483 | system_misc_info_keys=system_misc_info_keys) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 484 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 485 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 486 | def merge_target_files(temp_dir, system_target_files, system_item_list, |
| 487 | system_misc_info_keys, other_target_files, |
| 488 | other_item_list, output_target_files, output_dir, |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 489 | output_item_list, output_ota, output_img, |
| 490 | output_super_empty, rebuild_recovery): |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 491 | """Merge two target files packages together. |
| 492 | |
| 493 | This function takes system and other target files packages as input, performs |
| 494 | various file extractions, special case processing, and finally creates a |
| 495 | merged zip archive as output. |
| 496 | |
| 497 | Args: |
| 498 | temp_dir: The name of a directory we use when we extract items from the |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 499 | input target files packages, and also a scratch directory that we use for |
| 500 | temporary files. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 501 | system_target_files: The name of the zip archive containing the system |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 502 | partial target files package. |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 503 | system_item_list: The list of items to extract from the partial system |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 504 | target files package as is, meaning these items will land in the output |
| 505 | target files package exactly as they appear in the input partial system |
| 506 | target files package. |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 507 | system_misc_info_keys: The list of keys to obtain from the system instance |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 508 | of META/misc_info.txt. The remaining keys from the other instance. |
| 509 | other_target_files: The name of the zip archive containing the other partial |
| 510 | target files package. |
| 511 | other_item_list: The list of items to extract from the partial other target |
| 512 | files package as is, meaning these items will land in the output target |
| 513 | files package exactly as they appear in the input partial other target |
| 514 | files package. |
| 515 | output_target_files: The name of the output zip archive target files package |
| 516 | created by merging system and other. |
| 517 | output_dir: The destination directory for saving merged files. |
| 518 | output_item_list: The list of items to copy into the output_dir. |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 519 | output_ota: The name of the output zip archive ota package. |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 520 | output_img: The name of the output zip archive img package. |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 521 | output_super_empty: If provided, creates a super_empty.img file from the |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 522 | merged target files package and saves it at this path. |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 523 | rebuild_recovery: If true, rebuild the recovery patch used by non-A/B |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 524 | devices and write it to the system image. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 525 | """ |
| 526 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 527 | logger.info('starting: merge system %s and other %s into output %s', |
| 528 | system_target_files, other_target_files, output_target_files) |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 529 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 530 | # Create directory names that we'll use when we extract files from system, |
| 531 | # and other, and for zipping the final output. |
| 532 | |
| 533 | system_target_files_temp_dir = os.path.join(temp_dir, 'system') |
| 534 | other_target_files_temp_dir = os.path.join(temp_dir, 'other') |
| 535 | output_target_files_temp_dir = os.path.join(temp_dir, 'output') |
| 536 | |
| 537 | # Extract "as is" items from the input system partial target files package. |
| 538 | # We extract them directly into the output temporary directory since the |
| 539 | # items do not need special case processing. |
| 540 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 541 | extract_items( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 542 | target_files=system_target_files, |
| 543 | target_files_temp_dir=output_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 544 | extract_item_list=system_item_list) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 545 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 546 | # Extract "as is" items from the input other partial target files package. We |
| 547 | # extract them directly into the output temporary directory since the items |
| 548 | # do not need special case processing. |
| 549 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 550 | extract_items( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 551 | target_files=other_target_files, |
| 552 | target_files_temp_dir=output_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 553 | extract_item_list=other_item_list) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 554 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 555 | # Extract "special" items from the input system partial target files package. |
| 556 | # We extract these items to different directory since they require special |
| 557 | # processing before they will end up in the output directory. |
| 558 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 559 | extract_items( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 560 | target_files=system_target_files, |
| 561 | target_files_temp_dir=system_target_files_temp_dir, |
| 562 | extract_item_list=system_extract_special_item_list) |
| 563 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 564 | # Extract "special" items from the input other partial target files package. |
| 565 | # We extract these items to different directory since they require special |
| 566 | # processing before they will end up in the output directory. |
| 567 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 568 | extract_items( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 569 | target_files=other_target_files, |
| 570 | target_files_temp_dir=other_target_files_temp_dir, |
| 571 | extract_item_list=other_extract_special_item_list) |
| 572 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 573 | # Now that the temporary directories contain all the extracted files, perform |
| 574 | # special case processing on any items that need it. After this function |
| 575 | # completes successfully, all the files we need to create the output target |
| 576 | # files package are in place. |
| 577 | |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 578 | process_special_cases( |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 579 | system_target_files_temp_dir=system_target_files_temp_dir, |
| 580 | other_target_files_temp_dir=other_target_files_temp_dir, |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 581 | output_target_files_temp_dir=output_target_files_temp_dir, |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 582 | system_misc_info_keys=system_misc_info_keys, |
| 583 | rebuild_recovery=rebuild_recovery) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 584 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 585 | # Regenerate IMAGES in the temporary directory. |
| 586 | |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 587 | add_img_args = ['--verbose'] |
| 588 | if rebuild_recovery: |
| 589 | add_img_args.append('--rebuild_recovery') |
| 590 | add_img_args.append(output_target_files_temp_dir) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 591 | |
| 592 | add_img_to_target_files.main(add_img_args) |
| 593 | |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 594 | # Create super_empty.img using the merged misc_info.txt. |
| 595 | |
| 596 | misc_info_txt = os.path.join(output_target_files_temp_dir, 'META', |
| 597 | 'misc_info.txt') |
| 598 | |
| 599 | def read_helper(): |
| 600 | with open(misc_info_txt) as f: |
| 601 | return list(f.read().splitlines()) |
| 602 | |
| 603 | use_dynamic_partitions = common.LoadDictionaryFromLines( |
| 604 | read_helper()).get('use_dynamic_partitions') |
| 605 | |
| 606 | if use_dynamic_partitions != 'true' and output_super_empty: |
| 607 | raise ValueError( |
| 608 | 'Building super_empty.img requires use_dynamic_partitions=true.') |
| 609 | elif use_dynamic_partitions == 'true': |
| 610 | super_empty_img = os.path.join(output_target_files_temp_dir, 'IMAGES', |
| 611 | 'super_empty.img') |
| 612 | build_super_image_args = [ |
| 613 | misc_info_txt, |
| 614 | super_empty_img, |
| 615 | ] |
| 616 | build_super_image.main(build_super_image_args) |
| 617 | |
| 618 | # Copy super_empty.img to the user-provided output_super_empty location. |
| 619 | if output_super_empty: |
| 620 | shutil.copyfile(super_empty_img, output_super_empty) |
| 621 | |
Daniel Norman | b8a2f9d | 2019-04-24 12:55:51 -0700 | [diff] [blame] | 622 | # Create the IMG package from the merged target files (before zipping, in |
| 623 | # order to avoid an unnecessary unzip and copy). |
| 624 | |
| 625 | if output_img: |
| 626 | img_from_target_files_args = [ |
| 627 | output_target_files_temp_dir, |
| 628 | output_img, |
| 629 | ] |
| 630 | img_from_target_files.main(img_from_target_files_args) |
| 631 | |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 632 | # Finally, create the output target files zip archive and/or copy the |
| 633 | # output items to the output target files directory. |
| 634 | |
| 635 | if output_dir: |
| 636 | copy_items(output_target_files_temp_dir, output_dir, output_item_list) |
| 637 | |
| 638 | if not output_target_files: |
| 639 | return |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 640 | |
| 641 | output_zip = os.path.abspath(output_target_files) |
| 642 | output_target_files_list = os.path.join(temp_dir, 'output.list') |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 643 | output_target_files_meta_dir = os.path.join(output_target_files_temp_dir, |
| 644 | 'META') |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 645 | |
Bill Peckham | 9662cfb | 2019-04-24 17:59:01 -0700 | [diff] [blame] | 646 | find_command = [ |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 647 | 'find', |
| 648 | output_target_files_meta_dir, |
| 649 | ] |
Bill Peckham | 9662cfb | 2019-04-24 17:59:01 -0700 | [diff] [blame] | 650 | find_process = common.Run(find_command, stdout=subprocess.PIPE, verbose=False) |
| 651 | meta_content = common.RunAndCheckOutput(['sort'], stdin=find_process.stdout, |
| 652 | verbose=False) |
| 653 | |
| 654 | find_command = [ |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 655 | 'find', output_target_files_temp_dir, '-path', |
| 656 | output_target_files_meta_dir, '-prune', '-o', '-print' |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 657 | ] |
Bill Peckham | 9662cfb | 2019-04-24 17:59:01 -0700 | [diff] [blame] | 658 | find_process = common.Run(find_command, stdout=subprocess.PIPE, verbose=False) |
| 659 | other_content = common.RunAndCheckOutput(['sort'], stdin=find_process.stdout, |
| 660 | verbose=False) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 661 | |
| 662 | with open(output_target_files_list, 'wb') as f: |
| 663 | f.write(meta_content) |
| 664 | f.write(other_content) |
| 665 | |
| 666 | command = [ |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 667 | 'soong_zip', |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 668 | '-d', |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 669 | '-o', |
| 670 | output_zip, |
| 671 | '-C', |
| 672 | output_target_files_temp_dir, |
| 673 | '-l', |
| 674 | output_target_files_list, |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 675 | ] |
| 676 | logger.info('creating %s', output_target_files) |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 677 | common.RunAndWait(command, verbose=True) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 678 | |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 679 | # Create the OTA package from the merged target files package. |
| 680 | |
| 681 | if output_ota: |
| 682 | ota_from_target_files_args = [ |
| 683 | output_zip, |
| 684 | output_ota, |
| 685 | ] |
| 686 | ota_from_target_files.main(ota_from_target_files_args) |
| 687 | |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 688 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 689 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 690 | def call_func_with_temp_dir(func, keep_tmp): |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 691 | """Manage the creation and cleanup of the temporary directory. |
| 692 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 693 | This function calls the given function after first creating a temporary |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 694 | directory. It also cleans up the temporary directory. |
| 695 | |
| 696 | Args: |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 697 | func: The function to call. Should accept one parameter, the path to the |
| 698 | temporary directory. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 699 | keep_tmp: Keep the temporary directory after processing is complete. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 700 | """ |
| 701 | |
| 702 | # Create a temporary directory. This will serve as the parent of directories |
| 703 | # we use when we extract items from the input target files packages, and also |
| 704 | # a scratch directory that we use for temporary files. |
| 705 | |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 706 | temp_dir = common.MakeTempDir(prefix='merge_target_files_') |
| 707 | |
| 708 | try: |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 709 | func(temp_dir) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 710 | except: |
| 711 | raise |
| 712 | finally: |
| 713 | if keep_tmp: |
| 714 | logger.info('keeping %s', temp_dir) |
| 715 | else: |
| 716 | common.Cleanup() |
| 717 | |
| 718 | |
| 719 | def main(): |
| 720 | """The main function. |
| 721 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 722 | Process command line arguments, then call merge_target_files to |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 723 | perform the heavy lifting. |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 724 | """ |
| 725 | |
| 726 | common.InitLogging() |
| 727 | |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 728 | def option_handler(o, a): |
| 729 | if o == '--system-target-files': |
| 730 | OPTIONS.system_target_files = a |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 731 | elif o == '--system-item-list': |
| 732 | OPTIONS.system_item_list = a |
| 733 | elif o == '--system-misc-info-keys': |
| 734 | OPTIONS.system_misc_info_keys = a |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 735 | elif o == '--other-target-files': |
| 736 | OPTIONS.other_target_files = a |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 737 | elif o == '--other-item-list': |
| 738 | OPTIONS.other_item_list = a |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 739 | elif o == '--output-target-files': |
| 740 | OPTIONS.output_target_files = a |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 741 | elif o == '--output-dir': |
| 742 | OPTIONS.output_dir = a |
| 743 | elif o == '--output-item-list': |
| 744 | OPTIONS.output_item_list = a |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 745 | elif o == '--output-ota': |
| 746 | OPTIONS.output_ota = a |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 747 | elif o == '--output-img': |
| 748 | OPTIONS.output_img = a |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 749 | elif o == '--output-super-empty': |
| 750 | OPTIONS.output_super_empty = a |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 751 | elif o == '--rebuild_recovery': |
| 752 | OPTIONS.rebuild_recovery = True |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 753 | elif o == '--keep-tmp': |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 754 | OPTIONS.keep_tmp = True |
| 755 | else: |
| 756 | return False |
| 757 | return True |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 758 | |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 759 | args = common.ParseOptions( |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 760 | sys.argv[1:], |
| 761 | __doc__, |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 762 | extra_long_opts=[ |
| 763 | 'system-target-files=', |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 764 | 'system-item-list=', |
| 765 | 'system-misc-info-keys=', |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 766 | 'other-target-files=', |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 767 | 'other-item-list=', |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 768 | 'output-target-files=', |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 769 | 'output-dir=', |
| 770 | 'output-item-list=', |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 771 | 'output-ota=', |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 772 | 'output-img=', |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 773 | 'output-super-empty=', |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 774 | 'rebuild_recovery', |
Bill Peckham | 364c1cc | 2019-03-29 18:27:23 -0700 | [diff] [blame] | 775 | 'keep-tmp', |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 776 | ], |
| 777 | extra_option_handler=option_handler) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 778 | |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 779 | if (args or OPTIONS.system_target_files is None or |
| 780 | OPTIONS.other_target_files is None or |
| 781 | (OPTIONS.output_target_files is None and OPTIONS.output_dir is None) or |
| 782 | (OPTIONS.output_dir is not None and OPTIONS.output_item_list is None)): |
Bill Peckham | f753e15 | 2019-02-19 18:02:46 -0800 | [diff] [blame] | 783 | common.Usage(__doc__) |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 784 | sys.exit(1) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 785 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 786 | if OPTIONS.system_item_list: |
| 787 | system_item_list = read_config_list(OPTIONS.system_item_list) |
| 788 | else: |
| 789 | system_item_list = default_system_item_list |
| 790 | |
| 791 | if OPTIONS.system_misc_info_keys: |
| 792 | system_misc_info_keys = read_config_list(OPTIONS.system_misc_info_keys) |
| 793 | else: |
| 794 | system_misc_info_keys = default_system_misc_info_keys |
| 795 | |
| 796 | if OPTIONS.other_item_list: |
| 797 | other_item_list = read_config_list(OPTIONS.other_item_list) |
| 798 | else: |
| 799 | other_item_list = default_other_item_list |
| 800 | |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 801 | if OPTIONS.output_item_list: |
| 802 | output_item_list = read_config_list(OPTIONS.output_item_list) |
| 803 | else: |
| 804 | output_item_list = None |
| 805 | |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 806 | if not validate_config_lists( |
| 807 | system_item_list=system_item_list, |
Daniel Norman | 19b9fe9 | 2019-03-19 14:48:02 -0700 | [diff] [blame] | 808 | system_misc_info_keys=system_misc_info_keys, |
Daniel Norman | e596452 | 2019-03-19 10:32:03 -0700 | [diff] [blame] | 809 | other_item_list=other_item_list): |
| 810 | sys.exit(1) |
| 811 | |
Daniel Norman | 2c99c5b | 2019-03-07 13:01:48 -0800 | [diff] [blame] | 812 | call_func_with_temp_dir( |
| 813 | lambda temp_dir: merge_target_files( |
| 814 | temp_dir=temp_dir, |
| 815 | system_target_files=OPTIONS.system_target_files, |
| 816 | system_item_list=system_item_list, |
| 817 | system_misc_info_keys=system_misc_info_keys, |
| 818 | other_target_files=OPTIONS.other_target_files, |
| 819 | other_item_list=other_item_list, |
Daniel Norman | a4911da | 2019-03-15 14:36:21 -0700 | [diff] [blame] | 820 | output_target_files=OPTIONS.output_target_files, |
Daniel Norman | fdb3881 | 2019-04-15 09:47:24 -0700 | [diff] [blame] | 821 | output_dir=OPTIONS.output_dir, |
| 822 | output_item_list=output_item_list, |
Daniel Norman | 3b64ce1 | 2019-04-16 16:11:35 -0700 | [diff] [blame] | 823 | output_ota=OPTIONS.output_ota, |
Daniel Norman | 1bd2a1d | 2019-04-18 12:32:18 -0700 | [diff] [blame] | 824 | output_img=OPTIONS.output_img, |
Daniel Norman | f031825 | 2019-04-15 11:34:56 -0700 | [diff] [blame] | 825 | output_super_empty=OPTIONS.output_super_empty, |
Daniel Norman | e5b134a | 2019-04-17 14:54:06 -0700 | [diff] [blame] | 826 | rebuild_recovery=OPTIONS.rebuild_recovery), OPTIONS.keep_tmp) |
Bill Peckham | e9eb5f9 | 2019-02-01 15:52:10 -0800 | [diff] [blame] | 827 | |
| 828 | |
| 829 | if __name__ == '__main__': |
Bill Peckham | 889b0c6 | 2019-02-21 18:53:37 -0800 | [diff] [blame] | 830 | main() |