blob: e1105bb3620c7e15ce38b8f29d5cb865b8887075 [file] [log] [blame]
Doug Zongkereef39442009-04-02 12:14:19 -07001#!/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"""
Daniel Normanb8a2f9d2019-04-24 12:55:51 -070018Given target-files, produces an image zipfile suitable for use
19with 'fastboot update'.
Doug Zongkereef39442009-04-02 12:14:19 -070020
21Usage: img_from_target_files [flags] input_target_files output_image_zip
22
Daniel Normanb8a2f9d2019-04-24 12:55:51 -070023input_target_files: one of the following:
24 - directory containing extracted target files. It will load info from
25 OTA/android-info.txt and build the image zipfile using images from IMAGES/.
26 - target files package. Same as above, but extracts the archive before
27 building the image zipfile.
28
29Flags:
Doug Zongker55d93282011-01-25 17:03:34 -080030 -z (--bootable_zip)
31 Include only the bootable images (eg 'boot' and 'recovery') in
32 the output.
33
Doug Zongkereef39442009-04-02 12:14:19 -070034"""
35
Tao Bao89fbb0f2017-01-10 10:47:58 -080036from __future__ import print_function
37
Tao Bao32fcdab2018-10-12 10:30:39 -070038import logging
Tao Bao76def242017-11-21 09:25:31 -080039import os
40import shutil
Doug Zongkereef39442009-04-02 12:14:19 -070041import sys
Tao Bao76def242017-11-21 09:25:31 -080042import zipfile
43
44import common
Doug Zongkereef39442009-04-02 12:14:19 -070045
Doug Zongkercf6d5a92014-02-18 10:57:07 -080046if sys.hexversion < 0x02070000:
Tao Bao89fbb0f2017-01-10 10:47:58 -080047 print("Python 2.7 or newer is required.", file=sys.stderr)
Doug Zongkereef39442009-04-02 12:14:19 -070048 sys.exit(1)
49
Tao Bao32fcdab2018-10-12 10:30:39 -070050logger = logging.getLogger(__name__)
Doug Zongkereef39442009-04-02 12:14:19 -070051
52OPTIONS = common.OPTIONS
53
Ying Wanga0febe52013-03-20 11:02:05 -070054
Doug Zongkereef39442009-04-02 12:14:19 -070055def CopyInfo(output_zip):
56 """Copy the android-info.txt file from the input to the output."""
Tao Bao2ed665a2015-04-01 11:21:55 -070057 common.ZipWrite(
58 output_zip, os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
59 "android-info.txt")
Doug Zongkereef39442009-04-02 12:14:19 -070060
61
62def main(argv):
Tao Bao76def242017-11-21 09:25:31 -080063 # This allows modifying the value from inner function.
64 bootable_only_array = [False]
Doug Zongkereef39442009-04-02 12:14:19 -070065
Dan Albert8b72aef2015-03-23 19:13:21 -070066 def option_handler(o, _):
Doug Zongker55d93282011-01-25 17:03:34 -080067 if o in ("-z", "--bootable_zip"):
Tao Bao76def242017-11-21 09:25:31 -080068 bootable_only_array[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -070069 else:
70 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -070071 return True
Doug Zongkereef39442009-04-02 12:14:19 -070072
73 args = common.ParseOptions(argv, __doc__,
Doug Zongker3c84f562014-07-31 11:06:30 -070074 extra_opts="z",
75 extra_long_opts=["bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -070076 extra_option_handler=option_handler)
77
Tao Bao76def242017-11-21 09:25:31 -080078 bootable_only = bootable_only_array[0]
Doug Zongker55d93282011-01-25 17:03:34 -080079
Doug Zongkereef39442009-04-02 12:14:19 -070080 if len(args) != 2:
81 common.Usage(__doc__)
82 sys.exit(1)
83
Tao Bao32fcdab2018-10-12 10:30:39 -070084 common.InitLogging()
85
Daniel Normanb8a2f9d2019-04-24 12:55:51 -070086 target_files = args[0]
87 if os.path.isdir(target_files):
88 logger.info("Building image zip from extracted target files.")
89 OPTIONS.input_tmp = target_files
90 elif zipfile.is_zipfile(target_files):
91 logger.info("Building image zip from target files zip.")
92 OPTIONS.input_tmp = common.UnzipTemp(args[0], ["IMAGES/*", "OTA/*"])
93 else:
94 raise ValueError("%s is not a valid path." % target_files)
95
Doug Zongkereef39442009-04-02 12:14:19 -070096 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -070097 CopyInfo(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -070098
Doug Zongker3c84f562014-07-31 11:06:30 -070099 try:
Doug Zongker3c84f562014-07-31 11:06:30 -0700100 images_path = os.path.join(OPTIONS.input_tmp, "IMAGES")
Tao Bao2bb10972017-05-31 10:37:48 -0700101 # A target-files zip must contain the images since Lollipop.
102 assert os.path.exists(images_path)
103 for image in sorted(os.listdir(images_path)):
104 if bootable_only and image not in ("boot.img", "recovery.img"):
105 continue
106 if not image.endswith(".img"):
107 continue
108 if image == "recovery-two-step.img":
109 continue
110 common.ZipWrite(output_zip, os.path.join(images_path, image), image)
Doug Zongker3c84f562014-07-31 11:06:30 -0700111
112 finally:
Tao Bao32fcdab2018-10-12 10:30:39 -0700113 logger.info("cleaning up...")
Tao Bao2ed665a2015-04-01 11:21:55 -0700114 common.ZipClose(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -0700115
Tao Bao32fcdab2018-10-12 10:30:39 -0700116 logger.info("done.")
Doug Zongkereef39442009-04-02 12:14:19 -0700117
118
119if __name__ == '__main__':
120 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800121 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700122 main(sys.argv[1:])
Dan Albert8b72aef2015-03-23 19:13:21 -0700123 except common.ExternalError as e:
Tao Bao32fcdab2018-10-12 10:30:39 -0700124 logger.exception("\n ERROR:\n")
Doug Zongkereef39442009-04-02 12:14:19 -0700125 sys.exit(1)
Daniel Norman1bd2a1d2019-04-18 12:32:18 -0700126 finally:
127 common.Cleanup()