img_from_target_files.py revision 3c84f569487c4e59baa332be33b5430fdefb76b3
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 errno
36import os
37import re
38import shutil
39import subprocess
40import tempfile
41import zipfile
42
43# missing in Python 2.4 and before
44if not hasattr(os, "SEEK_SET"):
45  os.SEEK_SET = 0
46
47import common
48
49OPTIONS = common.OPTIONS
50
51
52def CopyInfo(output_zip):
53  """Copy the android-info.txt file from the input to the output."""
54  output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
55                   "android-info.txt")
56
57
58def main(argv):
59  bootable_only = [False]
60
61  def option_handler(o, a):
62    if o in ("-z", "--bootable_zip"):
63      bootable_only[0] = True
64    else:
65      return False
66    return True
67
68  args = common.ParseOptions(argv, __doc__,
69                             extra_opts="z",
70                             extra_long_opts=["bootable_zip"],
71                             extra_option_handler=option_handler)
72
73  bootable_only = bootable_only[0]
74
75  if len(args) != 2:
76    common.Usage(__doc__)
77    sys.exit(1)
78
79  OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0])
80  output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
81  CopyInfo(output_zip)
82
83  try:
84    done = False
85    images_path = os.path.join(OPTIONS.input_tmp, "IMAGES")
86    if os.path.exists(images_path):
87      # If this is a new target-files, it already contains the images,
88      # and all we have to do is copy them to the output zip.
89      images = os.listdir(images_path)
90      if images:
91        for i in images:
92          if bootable_only and i not in ("boot.img", "recovery.img"): continue
93          with open(os.path.join(images_path, i), "r") as f:
94            common.ZipWriteStr(output_zip, i, f.read())
95        done = True
96
97    if not done:
98      # We have an old target-files that doesn't already contain the
99      # images, so build them.
100      import add_img_to_target_files
101
102      OPTIONS.info_dict = common.LoadInfoDict(input_zip)
103
104      # If this image was originally labelled with SELinux contexts,
105      # make sure we also apply the labels in our new image. During
106      # building, the "file_contexts" is in the out/ directory tree,
107      # but for repacking from target-files.zip it's in the root
108      # directory of the ramdisk.
109      if "selinux_fc" in OPTIONS.info_dict:
110        OPTIONS.info_dict["selinux_fc"] = os.path.join(
111            OPTIONS.input_tmp, "BOOT", "RAMDISK", "file_contexts")
112
113      boot_image = common.GetBootableImage(
114          "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
115      if boot_image:
116          boot_image.AddToZip(output_zip)
117      recovery_image = common.GetBootableImage(
118          "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
119      if recovery_image:
120        recovery_image.AddToZip(output_zip)
121
122      def banner(s):
123        print "\n\n++++ " + s + " ++++\n\n"
124
125      if not bootable_only:
126        banner("AddSystem")
127        add_img_to_target_files.AddSystem(output_zip, prefix="")
128        try:
129          input_zip.getinfo("VENDOR/")
130          banner("AddVendor")
131          add_img_to_target_files.AddVendor(output_zip, prefix="")
132        except KeyError:
133          pass   # no vendor partition for this device
134        banner("AddUserdata")
135        add_img_to_target_files.AddUserdata(output_zip, prefix="")
136        banner("AddCache")
137        add_img_to_target_files.AddCache(output_zip, prefix="")
138
139  finally:
140    print "cleaning up..."
141    output_zip.close()
142    shutil.rmtree(OPTIONS.input_tmp)
143
144  print "done."
145
146
147if __name__ == '__main__':
148  try:
149    common.CloseInheritedPipes()
150    main(sys.argv[1:])
151  except common.ExternalError, e:
152    print
153    print "   ERROR: %s" % (e,)
154    print
155    sys.exit(1)
156