img_from_target_files.py revision d42e97ebb45fdc5a30799a3f37e482948d318010
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  common.ZipWrite(
47      output_zip, os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
48      "android-info.txt")
49
50
51def main(argv):
52  bootable_only = [False]
53
54  def option_handler(o, _):
55    if o in ("-z", "--bootable_zip"):
56      bootable_only[0] = True
57    else:
58      return False
59    return True
60
61  args = common.ParseOptions(argv, __doc__,
62                             extra_opts="z",
63                             extra_long_opts=["bootable_zip"],
64                             extra_option_handler=option_handler)
65
66  bootable_only = bootable_only[0]
67
68  if len(args) != 2:
69    common.Usage(__doc__)
70    sys.exit(1)
71
72  OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0])
73  output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
74  CopyInfo(output_zip)
75
76  try:
77    done = False
78    images_path = os.path.join(OPTIONS.input_tmp, "IMAGES")
79    if os.path.exists(images_path):
80      # If this is a new target-files, it already contains the images,
81      # and all we have to do is copy them to the output zip.
82      images = os.listdir(images_path)
83      if images:
84        for image in images:
85          if bootable_only and image not in ("boot.img", "recovery.img"):
86            continue
87          if not image.endswith(".img"):
88            continue
89          if image == "recovery-two-step.img":
90            continue
91          common.ZipWrite(
92              output_zip, os.path.join(images_path, image), image)
93        done = True
94
95    if not done:
96      # We have an old target-files that doesn't already contain the
97      # images, so build them.
98      import add_img_to_target_files
99
100      OPTIONS.info_dict = common.LoadInfoDict(input_zip, OPTIONS.input_tmp)
101
102      boot_image = common.GetBootableImage(
103          "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
104      if boot_image:
105        boot_image.AddToZip(output_zip)
106
107      if OPTIONS.info_dict.get("no_recovery") != "true":
108        recovery_image = common.GetBootableImage(
109            "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
110        if recovery_image:
111          recovery_image.AddToZip(output_zip)
112
113      def banner(s):
114        print "\n\n++++ " + s + " ++++\n\n"
115
116      if not bootable_only:
117        banner("AddSystem")
118        add_img_to_target_files.AddSystem(output_zip, prefix="")
119        try:
120          input_zip.getinfo("VENDOR/")
121          banner("AddVendor")
122          add_img_to_target_files.AddVendor(output_zip, prefix="")
123        except KeyError:
124          pass   # no vendor partition for this device
125        banner("AddUserdata")
126        add_img_to_target_files.AddUserdata(output_zip, prefix="")
127        banner("AddCache")
128        add_img_to_target_files.AddCache(output_zip, prefix="")
129
130  finally:
131    print "cleaning up..."
132    common.ZipClose(output_zip)
133    shutil.rmtree(OPTIONS.input_tmp)
134
135  print "done."
136
137
138if __name__ == '__main__':
139  try:
140    common.CloseInheritedPipes()
141    main(sys.argv[1:])
142  except common.ExternalError as e:
143    print
144    print "   ERROR: %s" % (e,)
145    print
146    sys.exit(1)
147