OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
7 """Process Android resources to generate R.java, and prepare for packaging. | 7 """Process Android resources to generate R.java, and prepare for packaging. |
8 | 8 |
9 This will crunch images and generate v14 compatible resources | 9 This will crunch images and generate v14 compatible resources |
10 (see generate_v14_compatible_resources.py). | 10 (see generate_v14_compatible_resources.py). |
11 """ | 11 """ |
12 | 12 |
13 import codecs | 13 import codecs |
14 import optparse | 14 import optparse |
15 import os | 15 import os |
16 import re | 16 import re |
17 import shutil | 17 import shutil |
18 import sys | 18 import sys |
19 import zipfile | |
20 | 19 |
21 import generate_v14_compatible_resources | 20 import generate_v14_compatible_resources |
22 | 21 |
23 from util import build_utils | 22 from util import build_utils |
24 | 23 |
25 # Import jinja2 from third_party/jinja2 | 24 # Import jinja2 from third_party/jinja2 |
26 sys.path.insert(1, | 25 sys.path.insert(1, |
27 os.path.join(os.path.dirname(__file__), '../../../third_party')) | 26 os.path.join(os.path.dirname(__file__), '../../../third_party')) |
28 from jinja2 import Template # pylint: disable=F0401 | 27 from jinja2 import Template # pylint: disable=F0401 |
29 | 28 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 268 |
270 | 269 |
271 def ZipResources(resource_dirs, zip_path): | 270 def ZipResources(resource_dirs, zip_path): |
272 # Python zipfile does not provide a way to replace a file (it just writes | 271 # Python zipfile does not provide a way to replace a file (it just writes |
273 # another file with the same name). So, first collect all the files to put | 272 # another file with the same name). So, first collect all the files to put |
274 # in the zip (with proper overriding), and then zip them. | 273 # in the zip (with proper overriding), and then zip them. |
275 files_to_zip = dict() | 274 files_to_zip = dict() |
276 for d in resource_dirs: | 275 for d in resource_dirs: |
277 for root, _, files in os.walk(d): | 276 for root, _, files in os.walk(d): |
278 for f in files: | 277 for f in files: |
279 archive_path = os.path.join(os.path.relpath(root, d), f) | 278 archive_path = f |
| 279 parent_dir = os.path.relpath(root, d) |
| 280 if parent_dir != '.': |
| 281 archive_path = os.path.join(parent_dir, f) |
280 path = os.path.join(root, f) | 282 path = os.path.join(root, f) |
281 files_to_zip[archive_path] = path | 283 files_to_zip[archive_path] = path |
282 with zipfile.ZipFile(zip_path, 'w') as outzip: | 284 build_utils.DoZip(files_to_zip.iteritems(), zip_path) |
283 for archive_path, path in files_to_zip.iteritems(): | |
284 outzip.write(path, archive_path) | |
285 | 285 |
286 | 286 |
287 def CombineZips(zip_files, output_path): | 287 def CombineZips(zip_files, output_path): |
288 # When packaging resources, if the top-level directories in the zip file are | 288 # When packaging resources, if the top-level directories in the zip file are |
289 # of the form 0, 1, ..., then each subdirectory will be passed to aapt as a | 289 # of the form 0, 1, ..., then each subdirectory will be passed to aapt as a |
290 # resources directory. While some resources just clobber others (image files, | 290 # resources directory. While some resources just clobber others (image files, |
291 # etc), other resources (particularly .xml files) need to be more | 291 # etc), other resources (particularly .xml files) need to be more |
292 # intelligently merged. That merging is left up to aapt. | 292 # intelligently merged. That merging is left up to aapt. |
293 with zipfile.ZipFile(output_path, 'w') as outzip: | 293 def path_transform(name, src_zip): |
294 for i, z in enumerate(zip_files): | 294 return '%d/%s' % (zip_files.index(src_zip), name) |
295 with zipfile.ZipFile(z, 'r') as inzip: | 295 |
296 for name in inzip.namelist(): | 296 build_utils.MergeZips(output_path, zip_files, path_transform=path_transform) |
297 new_name = '%d/%s' % (i, name) | |
298 outzip.writestr(new_name, inzip.read(name)) | |
299 | 297 |
300 | 298 |
301 def main(): | 299 def main(): |
302 args = build_utils.ExpandFileArgs(sys.argv[1:]) | 300 args = build_utils.ExpandFileArgs(sys.argv[1:]) |
303 | 301 |
304 options = ParseArgs(args) | 302 options = ParseArgs(args) |
305 android_jar = os.path.join(options.android_sdk, 'android.jar') | 303 android_jar = os.path.join(options.android_sdk, 'android.jar') |
306 aapt = options.aapt_path | 304 aapt = options.aapt_path |
307 | 305 |
308 input_files = [] | 306 input_files = [] |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 if options.depfile: | 409 if options.depfile: |
412 input_files += build_utils.GetPythonDependencies() | 410 input_files += build_utils.GetPythonDependencies() |
413 build_utils.WriteDepfile(options.depfile, input_files) | 411 build_utils.WriteDepfile(options.depfile, input_files) |
414 | 412 |
415 if options.stamp: | 413 if options.stamp: |
416 build_utils.Touch(options.stamp) | 414 build_utils.Touch(options.stamp) |
417 | 415 |
418 | 416 |
419 if __name__ == '__main__': | 417 if __name__ == '__main__': |
420 main() | 418 main() |
OLD | NEW |