Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(551)

Side by Side Diff: build/android/gyp/mirror_images.py

Issue 106173002: Mirror images for RTL languages at build time. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/env python
2 #
3 # Copyright 2013 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 """Mirrors (i.e. horizontally flips) images in the Android res folder for use
8 in right-to-left (RTL) mode on Android.
9
10 Only some images are mirrored, as determined by the config file, typically
11 named mirror_images_config. The config file uses python syntax to define
12 two lists of image names: images_to_mirror and images_not_to_mirror. Images in
13 images_to_mirror will be mirrored by this tool. To ensure every image has been
14 considered for mirroring, the remaining images must be listed in
15 images_not_to_mirror.
16
17 Mirrorable images include directional images (e.g. back and forward buttons) and
18 most other asymmetric images. Non-mirrorable images include images with text
19 (e.g. the Chrome logo) and symmetric images (e.g. a star or X button).
20
21 Example mirror_images_config:
22
23 images_to_mirror = ['back.png', 'forward.png']
24 images_not_to_mirror = ['star.png']
25
26 Source images are taken from input_dir/res/drawable-* folders, and the
27 generated images are saved into output_dir/res/drawable-ldrtl-* folders. For
28 example: input_dir/res/drawable-hdpi/back.png would be mirrored into
29 output_dir/res/drawable-ldrtl-hdpi/back.png.
30 """
31
32 import errno
33 import multiprocessing.pool
34 import optparse
35 import os
36 import subprocess
37 import sys
38
39 from util import build_utils
40
41
42 class Image(object):
43 """Represents an image in the Android res directory."""
44
45 def __init__(self, drawable_dir, name):
46 # The image's directory, e.g. drawable-hdpi
47 self.drawable_dir = drawable_dir
48 # The image's filename, e.g. star.png
49 self.name = name
50
51
52 class Project(object):
53 """This class knows how to read the config file and mirror images in an
54 Android project."""
55
56 def __init__(self, config_file, input_res_dir, output_res_dir):
57 """Args:
58 config_file: The config file specifying which images will be mirrored.
59 input_res_dir: The directory containing source images to be mirrored.
60 output_res_dir: The directory into which mirrored images can be saved.
61 """
62 self.config_file = config_file
63 self.input_res_dir = input_res_dir
64 self.output_res_dir = output_res_dir
65
66 # List of names of images that will be mirrored, from config file.
67 self.images_to_mirror = None
68 # List of names of images that will not be mirrored, from config file.
69 self.images_not_to_mirror = None
70 # List of all images found in res/drawable* directories.
71 self.images = None
72 # List of errors found in the configuration file.
73 self.config_errors = None
74
75 def mirror_images(self):
76 """Mirrors images in the project according to the configuration.
77
78 If the project configuration contains any errors, this will fail and return
79 a list of error messages.
80
81 Returns:
82 A list of error messages that must be addressed manually before any images
83 can be mirrored. If this list is empty, then mirroring succeeded.
84 """
85 self.config_errors = []
Kibeom Kim (inactive) 2013/12/06 20:07:27 how about using exception? or.. passing config_err
newt (away) 2013/12/06 23:04:21 Done.
86 self._read_config_file()
87 if not self.config_errors:
88 self._read_drawable_dirs()
89 self._verify_config()
90 if not self.config_errors:
91 self._mirror_images()
92 return self.config_errors
93
94 def _read_config_file(self):
95 """Reads the lists of images that should and should not be mirrored from the
96 config file.
97 """
98 exec_env = {}
99 execfile(self.config_file, exec_env)
100 self.images_to_mirror = exec_env.get('images_to_mirror')
101 self.images_not_to_mirror = exec_env.get('images_not_to_mirror')
102 self._verify_config_list_well_formed(self.images_to_mirror,
103 'images_to_mirror')
104 self._verify_config_list_well_formed(self.images_not_to_mirror,
105 'images_not_to_mirror')
106
107 def _verify_config_list_well_formed(self, config_list, list_name):
108 """Checks that config_list is a list of strings. If not, adds an error
109 message(s) to self.config_errors."""
110 if type(config_list) != list:
111 self.config_errors.append('The config file must contain a list named ' +
112 list_name)
113 return
114 for item in config_list:
115 if not isinstance(item, basestring):
116 self.config_errors.append('List {0} contains a non-string item: {1}'
117 .format(list_name, item))
118
119 def _read_drawable_dirs(self):
120 """Gets the list of images in the input drawable directories."""
121 self.images = []
122
123 for dir_name in os.listdir(self.input_res_dir):
124 dir_components = dir_name.split('-')
125 if dir_components[0] != 'drawable' or 'ldrtl' in dir_components[1:]:
126 continue
127 dir_path = os.path.join(self.input_res_dir, dir_name)
128 if not os.path.isdir(dir_path):
129 continue
130
131 for image_name in os.listdir(dir_path):
132 if image_name.endswith('.png'):
133 self.images.append(Image(dir_name, image_name))
134
135 def _verify_config(self):
136 """Checks the config file for errors. Stores the list of error messages in
137 self.config_errors."""
138 errors = []
139
140 # Ensure images_to_mirror and images_not_to_mirror are sorted with no
141 # duplicates.
142 for l in self.images_to_mirror, self.images_not_to_mirror:
143 for i in range(len(l) - 1):
144 if l[i + 1] == l[i]:
145 errors.append(l[i + 1] + ' is listed multiple times')
146 elif l[i + 1] < l[i]:
147 errors.append(l[i + 1] + ' is not in sorted order')
148
149 # Ensure no overlap between images_to_mirror and images_not_to_mirror.
150 overlap = set(self.images_to_mirror).intersection(self.images_not_to_mirror)
151 for item in sorted(overlap):
152 errors.append(item + ' is listed multiple times.')
cjhopman 2013/12/06 17:28:23 I'd change this message so its different than the
newt (away) 2013/12/06 19:22:51 Done.
153
154 # Ensure all images on disk are listed in config file.
Kibeom Kim (inactive) 2013/12/06 20:07:27 totally unimportant but: how about, instead of 'on
newt (away) 2013/12/06 23:04:21 Done.
155 images_in_config = set(self.images_to_mirror + self.images_not_to_mirror)
156 images_on_disk = [i.name for i in self.images]
157 images_missing_from_config = set(images_on_disk).difference(
158 images_in_config)
159 for image_name in sorted(images_missing_from_config):
160 errors.append(image_name + ' exists in a res/drawable* folder but is not '
161 'listed in the config file. Update the config file to specify '
162 'whether this image should be mirrored for right-to-left users (or '
163 'remove the image).')
Kibeom Kim (inactive) 2013/12/06 20:07:27 Since this will be the error message people see mo
newt (away) 2013/12/06 23:04:21 Good idea. I added an extra message after all the
164
165 # Ensure only images on disk are listed in config file.
166 images_not_on_disk = set(images_in_config).difference(
167 images_on_disk)
168 for image_name in sorted(images_not_on_disk):
169 errors.append(image_name + ' is listed in the config file, but does not '
170 'exist in any res/drawable* folders. Remove this image name from the '
171 'config file (or add the image to a drawable folder).')
172
173 self.config_errors.extend(errors)
174
175 def _mirror_image(self, image):
176 ltr_path = os.path.join(self.input_res_dir, image.drawable_dir, image.name)
177 rtl_path = os.path.join(self.output_res_dir,
178 get_rtl_dir(image.drawable_dir), image.name)
179 build_utils.MakeDirectory(os.path.dirname(rtl_path))
180 mirror_image(ltr_path, rtl_path)
181
182 def _mirror_images(self):
183 pool = multiprocessing.pool.ThreadPool()
184 images_to_mirror = [i for i in self.images if
185 i.name in self.images_to_mirror]
186 pool.map(self._mirror_image, images_to_mirror)
Kibeom Kim (inactive) 2013/12/06 20:07:27 I'm not sure, but managing threadpool inside a scr
newt (away) 2013/12/06 23:04:21 I agree that it would nice to have the parallelism
187
188
189 def get_rtl_dir(drawable_dir_ltr):
190 """Returns the RTL drawable directory corresponding to drawable_dir_ltr.
191
192 Example:
193 drawable-hdpi -> drawable-ldrtl-hdpi
194 """
195 dir_components = drawable_dir_ltr.split('-')
196 assert 'ldrtl' not in dir_components
197 # ldrtl is always the first qualifier, as long as mobile country code or
198 # language and region aren't used as qualifiers:
199 # http://developer.android.com/guide/topics/resources/providing-resources.html
200 dir_components.insert(1, 'ldrtl')
201 return '-'.join(dir_components)
202
203
204 def mirror_image(src_path, dst_path):
205 """Mirrors a single image horizontally.
206
207 Args:
208 src_path: The image to be mirrored.
209 dst_path: The path where the mirrored image will be saved.
210 """
211 if src_path.endswith('.9.png'):
212 raise Exception('Cannot mirror {}: mirroring 9-patches is not supported. '
213 'If you need this functionality, please implement it.'.format(src_path))
214 try:
215 build_utils.CheckOutput(['convert', '-flop', src_path, dst_path])
216 except OSError as e:
217 if e.errno == errno.ENOENT:
218 raise Exception('Executable "convert" (from the imagemagick package) not '
219 'found. Run build/install-build-deps-android.sh and ensure '
220 'that "convert" is on your path.')
221 raise
222
223
224 def parse_args(args=None):
225 parser = optparse.OptionParser()
226 parser.add_option('--config-file', help='Configuration file specifying which '
227 'images should be mirrored')
228 parser.add_option('--input-res-dir', help='The res folder containing the '
229 'source images.')
230 parser.add_option('--output-res-dir', help='The res folder into which '
231 'mirrored images will be saved.')
232
233 if args is None:
234 args = sys.argv[1:]
235 options, args = parser.parse_args(args)
236
237 # Check that required options have been provided.
238 required_options = ('config_file', 'input_res_dir', 'output_res_dir')
239 build_utils.CheckOptions(options, parser, required=required_options)
240
241 return options
242
243
244 def main(args=None):
245 options = parse_args(args)
246 project = Project(options.config_file, options.input_res_dir,
247 options.output_res_dir)
248 config_errors = project.mirror_images()
249 if config_errors:
250 sys.stderr.write('Failed to mirror images.\n')
251 sys.stderr.write('{0} error(s) in config file {1}:\n'.format(
252 len(config_errors), os.path.abspath(options.config_file)))
253 for error in config_errors:
254 sys.stderr.write(' - {0}\n'.format(error))
255 sys.exit(1)
256
257
258 if __name__ == '__main__':
259 main()
OLDNEW
« no previous file with comments | « no previous file | build/android/gyp/process_resources.py » ('j') | build/android/gyp/process_resources.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698