img_from_target_files.py revision 8b72aefb5a8ed4da28c6f83854e8babf53b9cb53
1#!/usr/bin/env python 2# 3# Copyright (C) 2008 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""" 18Given a target-files zipfile, produces an image zipfile suitable for 19use with 'fastboot update'. 20 21Usage: img_from_target_files [flags] input_target_files output_image_zip 22 23 -z (--bootable_zip) 24 Include only the bootable images (eg 'boot' and 'recovery') in 25 the output. 26 27""" 28 29import sys 30 31if sys.hexversion < 0x02070000: 32 print >> sys.stderr, "Python 2.7 or newer is required." 33 sys.exit(1) 34 35import os 36import shutil 37import zipfile 38 39import common 40 41OPTIONS = common.OPTIONS 42 43 44def CopyInfo(output_zip): 45 """Copy the android-info.txt file from the input to the output.""" 46 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"), 47 "android-info.txt") 48 49 50def main(argv): 51 bootable_only = [False] 52 53 def option_handler(o, _): 54 if o in ("-z", "--bootable_zip"): 55 bootable_only[0] = True 56 else: 57 return False 58 return True 59 60 args = common.ParseOptions(argv, __doc__, 61 extra_opts="z", 62 extra_long_opts=["bootable_zip"], 63 extra_option_handler=option_handler) 64 65 bootable_only = bootable_only[0] 66 67 if len(args) != 2: 68 common.Usage(__doc__) 69 sys.exit(1) 70 71 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0]) 72 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED) 73 CopyInfo(output_zip) 74 75 try: 76 done = False 77 images_path = os.path.join(OPTIONS.input_tmp, "IMAGES") 78 if os.path.exists(images_path): 79 # If this is a new target-files, it already contains the images, 80 # and all we have to do is copy them to the output zip. 81 images = os.listdir(images_path) 82 if images: 83 for image in images: 84 if bootable_only and image not in ("boot.img", "recovery.img"): 85 continue 86 if not image.endswith(".img"): 87 continue 88 common.ZipWrite( 89 output_zip, os.path.join(images_path, image), image) 90 done = True 91 92 if not done: 93 # We have an old target-files that doesn't already contain the 94 # images, so build them. 95 import add_img_to_target_files 96 97 OPTIONS.info_dict = common.LoadInfoDict(input_zip) 98 99 # If this image was originally labelled with SELinux contexts, 100 # make sure we also apply the labels in our new image. During 101 # building, the "file_contexts" is in the out/ directory tree, 102 # but for repacking from target-files.zip it's in the root 103 # directory of the ramdisk. 104 if "selinux_fc" in OPTIONS.info_dict: 105 OPTIONS.info_dict["selinux_fc"] = os.path.join( 106 OPTIONS.input_tmp, "BOOT", "RAMDISK", "file_contexts") 107 108 boot_image = common.GetBootableImage( 109 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT") 110 if boot_image: 111 boot_image.AddToZip(output_zip) 112 recovery_image = common.GetBootableImage( 113 "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY") 114 if recovery_image: 115 recovery_image.AddToZip(output_zip) 116 117 def banner(s): 118 print "\n\n++++ " + s + " ++++\n\n" 119 120 if not bootable_only: 121 banner("AddSystem") 122 add_img_to_target_files.AddSystem(output_zip, prefix="") 123 try: 124 input_zip.getinfo("VENDOR/") 125 banner("AddVendor") 126 add_img_to_target_files.AddVendor(output_zip, prefix="") 127 except KeyError: 128 pass # no vendor partition for this device 129 banner("AddUserdata") 130 add_img_to_target_files.AddUserdata(output_zip, prefix="") 131 banner("AddCache") 132 add_img_to_target_files.AddCache(output_zip, prefix="") 133 134 finally: 135 print "cleaning up..." 136 # http://b/18015246 137 # See common.py for context. zipfile also refers to ZIP64_LIMIT during 138 # close() when it writes out the central directory. 139 saved_zip64_limit = zipfile.ZIP64_LIMIT 140 zipfile.ZIP64_LIMIT = (1 << 32) - 1 141 output_zip.close() 142 zipfile.ZIP64_LIMIT = saved_zip64_limit 143 shutil.rmtree(OPTIONS.input_tmp) 144 145 print "done." 146 147 148if __name__ == '__main__': 149 try: 150 common.CloseInheritedPipes() 151 main(sys.argv[1:]) 152 except common.ExternalError as e: 153 print 154 print " ERROR: %s" % (e,) 155 print 156 sys.exit(1) 157