OSDN Git Service

DRC59
[android-x86/build.git] / tools / releasetools / img_from_target_files
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 """
18 Given a target-files zipfile, produces an image zipfile suitable for
19 use with 'fastboot update'.
20
21 Usage:  img_from_target_files [flags] input_target_files output_image_zip
22
23   -b  (--board_config)  <file>
24       Deprecated.
25
26 """
27
28 import sys
29
30 if sys.hexversion < 0x02040000:
31   print >> sys.stderr, "Python 2.4 or newer is required."
32   sys.exit(1)
33
34 import os
35 import re
36 import shutil
37 import subprocess
38 import tempfile
39 import zipfile
40
41 # missing in Python 2.4 and before
42 if not hasattr(os, "SEEK_SET"):
43   os.SEEK_SET = 0
44
45 import common
46
47 OPTIONS = common.OPTIONS
48
49
50 def AddUserdata(output_zip):
51   """Create an empty userdata image and store it in output_zip."""
52
53   print "creating userdata.img..."
54
55   # The name of the directory it is making an image out of matters to
56   # mkyaffs2image.  So we create a temp dir, and within it we create an
57   # empty dir named "data", and build the image from that.
58   temp_dir = tempfile.mkdtemp()
59   user_dir = os.path.join(temp_dir, "data")
60   os.mkdir(user_dir)
61   img = tempfile.NamedTemporaryFile()
62
63   p = common.Run(["mkyaffs2image", "-f", user_dir, img.name])
64   p.communicate()
65   assert p.returncode == 0, "mkyaffs2image of userdata.img image failed"
66
67   common.CheckSize(img.name, "userdata.img")
68   output_zip.write(img.name, "userdata.img")
69   img.close()
70   os.rmdir(user_dir)
71   os.rmdir(temp_dir)
72
73
74 def AddSystem(output_zip):
75   """Turn the contents of SYSTEM into a system image and store it in
76   output_zip."""
77
78   print "creating system.img..."
79
80   img = tempfile.NamedTemporaryFile()
81
82   # The name of the directory it is making an image out of matters to
83   # mkyaffs2image.  It wants "system" but we have a directory named
84   # "SYSTEM", so create a symlink.
85   os.symlink(os.path.join(OPTIONS.input_tmp, "SYSTEM"),
86              os.path.join(OPTIONS.input_tmp, "system"))
87
88   p = common.Run(["mkyaffs2image", "-f",
89                   os.path.join(OPTIONS.input_tmp, "system"), img.name])
90   p.communicate()
91   assert p.returncode == 0, "mkyaffs2image of system.img image failed"
92
93   img.seek(os.SEEK_SET, 0)
94   data = img.read()
95   img.close()
96
97   common.CheckSize(data, "system.img")
98   common.ZipWriteStr(output_zip, "system.img", data)
99
100
101 def CopyInfo(output_zip):
102   """Copy the android-info.txt file from the input to the output."""
103   output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
104                    "android-info.txt")
105
106
107 def main(argv):
108
109   def option_handler(o, a):
110     if o in ("-b", "--board_config"):
111       pass       # deprecated
112     else:
113       return False
114     return True
115
116   args = common.ParseOptions(argv, __doc__,
117                              extra_opts="b:",
118                              extra_long_opts=["board_config="],
119                              extra_option_handler=option_handler)
120
121   if len(args) != 2:
122     common.Usage(__doc__)
123     sys.exit(1)
124
125   OPTIONS.input_tmp = common.UnzipTemp(args[0])
126
127   common.LoadMaxSizes()
128   if not OPTIONS.max_image_size:
129     print
130     print "  WARNING:  Failed to load max image sizes; will not enforce"
131     print "  image size limits."
132     print
133
134   output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
135
136   common.AddBoot(output_zip)
137   common.AddRecovery(output_zip)
138   AddSystem(output_zip)
139   AddUserdata(output_zip)
140   CopyInfo(output_zip)
141
142   print "cleaning up..."
143   output_zip.close()
144   shutil.rmtree(OPTIONS.input_tmp)
145
146   print "done."
147
148
149 if __name__ == '__main__':
150   try:
151     main(sys.argv[1:])
152   except common.ExternalError, e:
153     print
154     print "   ERROR: %s" % (e,)
155     print
156     sys.exit(1)