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 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 library resources to generate R.java and crunched images.""" | 7 """Process Android library resources to generate R.java and crunched images.""" |
8 | 8 |
9 import multiprocessing.pool | |
cjhopman
2013/12/06 17:28:23
unused?
newt (away)
2013/12/06 19:22:51
Done.
| |
9 import optparse | 10 import optparse |
10 import os | 11 import os |
11 import shlex | 12 import shlex |
12 import shutil | 13 import shutil |
14 import tempfile | |
13 | 15 |
14 from util import build_utils | 16 from util import build_utils |
17 import mirror_images | |
15 | 18 |
16 def ParseArgs(): | 19 def ParseArgs(): |
17 """Parses command line options. | 20 """Parses command line options. |
18 | 21 |
19 Returns: | 22 Returns: |
20 An options object as from optparse.OptionsParser.parse_args() | 23 An options object as from optparse.OptionsParser.parse_args() |
21 """ | 24 """ |
22 parser = optparse.OptionParser() | 25 parser = optparse.OptionParser() |
23 parser.add_option('--android-sdk', help='path to the Android SDK folder') | 26 parser.add_option('--android-sdk', help='path to the Android SDK folder') |
24 parser.add_option('--android-sdk-tools', | 27 parser.add_option('--android-sdk-tools', |
25 help='path to the Android SDK build tools folder') | 28 help='path to the Android SDK build tools folder') |
26 parser.add_option('--R-dir', help='directory to hold generated R.java') | 29 parser.add_option('--R-dir', help='directory to hold generated R.java') |
27 parser.add_option('--res-dirs', | 30 parser.add_option('--res-dirs', |
28 help='directories containing resources to be packaged') | 31 help='directories containing resources to be packaged') |
29 parser.add_option('--crunch-input-dir', | 32 parser.add_option('--image-input-dir', help='directory containing images to ' |
30 help='directory containing images to be crunched') | 33 'be crunched and possibly mirrored.') |
31 parser.add_option('--crunch-output-dir', | 34 parser.add_option('--crunch-output-dir', |
32 help='directory to hold crunched resources') | 35 help='directory to hold crunched resources') |
36 parser.add_option('--mirror-config', help='config file specifying which ' | |
37 'images should be mirrored. Images will be mirrored only ' | |
38 'if this is provided.') | |
39 parser.add_option('--mirror-output-dir', | |
40 help='directory where mirrored images should be saved') | |
33 parser.add_option('--non-constant-id', action='store_true') | 41 parser.add_option('--non-constant-id', action='store_true') |
34 parser.add_option('--custom-package', help='Java package for R.java') | 42 parser.add_option('--custom-package', help='Java package for R.java') |
35 parser.add_option('--android-manifest', help='AndroidManifest.xml path') | 43 parser.add_option('--android-manifest', help='AndroidManifest.xml path') |
36 parser.add_option('--stamp', help='File to touch on success') | 44 parser.add_option('--stamp', help='File to touch on success') |
37 | 45 |
38 # This is part of a temporary fix for crbug.com/177552. | 46 # This is part of a temporary fix for crbug.com/177552. |
39 # TODO(newt): remove this once crbug.com/177552 is fixed in ninja. | 47 # TODO(newt): remove this once crbug.com/177552 is fixed in ninja. |
40 parser.add_option('--ignore', help='this argument is ignored') | 48 parser.add_option('--ignore', help='this argument is ignored') |
41 (options, args) = parser.parse_args() | 49 (options, args) = parser.parse_args() |
42 | 50 |
43 if args: | 51 if args: |
44 parser.error('No positional arguments should be given.') | 52 parser.error('No positional arguments should be given.') |
45 | 53 |
54 if (options.mirror_config is None) != (options.mirror_output_dir is None): | |
55 parser.error('--mirror-config and --mirror-output-dir must both be present ' | |
56 'or neither present.') | |
57 | |
46 # Check that required options have been provided. | 58 # Check that required options have been provided. |
47 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir', | 59 required_options = ('android_sdk', 'android_sdk_tools', 'R_dir', |
48 'res_dirs', 'crunch_input_dir', 'crunch_output_dir') | 60 'res_dirs', 'image_input_dir', 'crunch_output_dir') |
49 build_utils.CheckOptions(options, parser, required=required_options) | 61 build_utils.CheckOptions(options, parser, required=required_options) |
50 | 62 |
51 return options | 63 return options |
52 | 64 |
53 | 65 |
54 def MoveImagesToNonMdpiFolders(res_root): | 66 def MoveImagesToNonMdpiFolders(res_root): |
55 """Move images from drawable-*-mdpi-* folders to drawable-* folders. | 67 """Move images from drawable-*-mdpi-* folders to drawable-* folders. |
56 | 68 |
57 Why? http://crbug.com/289843 | 69 Why? http://crbug.com/289843 |
58 """ | 70 """ |
(...skipping 11 matching lines...) Expand all Loading... | |
70 build_utils.MakeDirectory(dst_dir) | 82 build_utils.MakeDirectory(dst_dir) |
71 for src_file_name in os.listdir(src_dir): | 83 for src_file_name in os.listdir(src_dir): |
72 if not src_file_name.endswith('.png'): | 84 if not src_file_name.endswith('.png'): |
73 continue | 85 continue |
74 src_file = os.path.join(src_dir, src_file_name) | 86 src_file = os.path.join(src_dir, src_file_name) |
75 dst_file = os.path.join(dst_dir, src_file_name) | 87 dst_file = os.path.join(dst_dir, src_file_name) |
76 assert not os.path.lexists(dst_file) | 88 assert not os.path.lexists(dst_file) |
77 shutil.move(src_file, dst_file) | 89 shutil.move(src_file, dst_file) |
78 | 90 |
79 | 91 |
92 def CrunchImages(aapt, input_res_dir, output_res_dir): | |
93 build_utils.MakeDirectory(output_res_dir) | |
94 aapt_cmd = [aapt, | |
95 'crunch', | |
96 '-S', input_res_dir, | |
97 '-C', output_res_dir] | |
98 build_utils.CheckOutput(aapt_cmd, fail_if_stderr=True) | |
99 | |
100 | |
80 def main(): | 101 def main(): |
81 options = ParseArgs() | 102 options = ParseArgs() |
82 android_jar = os.path.join(options.android_sdk, 'android.jar') | 103 android_jar = os.path.join(options.android_sdk, 'android.jar') |
83 aapt = os.path.join(options.android_sdk_tools, 'aapt') | 104 aapt = os.path.join(options.android_sdk_tools, 'aapt') |
84 | 105 |
85 build_utils.MakeDirectory(options.R_dir) | 106 build_utils.MakeDirectory(options.R_dir) |
86 | 107 |
87 # Generate R.java. This R.java contains non-final constants and is used only | 108 # Generate R.java. This R.java contains non-final constants and is used only |
88 # while compiling the library jar (e.g. chromium_content.jar). When building | 109 # while compiling the library jar (e.g. chromium_content.jar). When building |
89 # an apk, a new R.java file with the correct resource -> ID mappings will be | 110 # an apk, a new R.java file with the correct resource -> ID mappings will be |
90 # generated by merging the resources from all libraries and the main apk | 111 # generated by merging the resources from all libraries and the main apk |
91 # project. | 112 # project. |
92 package_command = [aapt, | 113 package_command = [aapt, |
93 'package', | 114 'package', |
94 '-m', | 115 '-m', |
95 '-M', options.android_manifest, | 116 '-M', options.android_manifest, |
96 '--auto-add-overlay', | 117 '--auto-add-overlay', |
97 '-I', android_jar, | 118 '-I', android_jar, |
98 '--output-text-symbols', options.R_dir, | 119 '--output-text-symbols', options.R_dir, |
99 '-J', options.R_dir] | 120 '-J', options.R_dir] |
100 res_dirs = shlex.split(options.res_dirs) | 121 res_dirs = shlex.split(options.res_dirs) |
101 for res_dir in res_dirs: | 122 for res_dir in res_dirs: |
102 package_command += ['-S', res_dir] | 123 package_command += ['-S', res_dir] |
103 if options.non_constant_id: | 124 if options.non_constant_id: |
104 package_command.append('--non-constant-id') | 125 package_command.append('--non-constant-id') |
105 if options.custom_package: | 126 if options.custom_package: |
106 package_command += ['--custom-package', options.custom_package] | 127 package_command += ['--custom-package', options.custom_package] |
107 build_utils.CheckOutput(package_command) | 128 build_utils.CheckOutput(package_command) |
108 | 129 |
130 # Mirror images if requested. | |
131 if options.mirror_config: | |
132 # Mirrored images are generated into a temp dir. Once these images are | |
133 # crunched, they'll go in the desired mirror output dir. | |
134 mirrored_uncrunched_dir = tempfile.mkdtemp() | |
135 try: | |
136 mirror_images.main(['--config', options.mirror_config, | |
137 '--input-res-dir', options.image_input_dir, | |
138 '--output-res-dir', mirrored_uncrunched_dir]) | |
139 CrunchImages(aapt, mirrored_uncrunched_dir, options.mirror_output_dir) | |
140 finally: | |
141 shutil.rmtree(mirrored_uncrunched_dir) | |
142 | |
109 # Crunch image resources. This shrinks png files and is necessary for 9-patch | 143 # Crunch image resources. This shrinks png files and is necessary for 9-patch |
110 # images to display correctly. | 144 # images to display correctly. |
111 build_utils.MakeDirectory(options.crunch_output_dir) | 145 CrunchImages(aapt, options.image_input_dir, options.crunch_output_dir) |
newt (away)
2013/12/05 19:33:22
Chris, do we ever delete the res_crunched director
cjhopman
2013/12/06 17:28:23
I don't think we do.
I think the correct approach
| |
112 aapt_cmd = [aapt, | |
113 'crunch', | |
114 '-S', options.crunch_input_dir, | |
115 '-C', options.crunch_output_dir] | |
116 build_utils.CheckOutput(aapt_cmd, fail_if_stderr=True) | |
117 | |
118 MoveImagesToNonMdpiFolders(options.crunch_output_dir) | |
119 | 146 |
120 if options.stamp: | 147 if options.stamp: |
121 build_utils.Touch(options.stamp) | 148 build_utils.Touch(options.stamp) |
122 | 149 |
123 | 150 |
124 if __name__ == '__main__': | 151 if __name__ == '__main__': |
125 main() | 152 main() |
OLD | NEW |