OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # | 2 # |
3 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 """Convert Android xml resources to API 14 compatible. | 7 """Convert Android xml resources to API 14 compatible. |
8 | 8 |
9 There are two reasons that we cannot just use API 17 attributes, | 9 There are two reasons that we cannot just use API 17 attributes, |
10 so we are generating another set of resources by this script. | 10 so we are generating another set of resources by this script. |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 list(root_node.getElementsByTagName('style'))) | 91 list(root_node.getElementsByTagName('style'))) |
92 | 92 |
93 | 93 |
94 def ErrorIfStyleResourceExistsInDir(input_dir): | 94 def ErrorIfStyleResourceExistsInDir(input_dir): |
95 """If a style resource is in input_dir, exist with an error message.""" | 95 """If a style resource is in input_dir, exist with an error message.""" |
96 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 96 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
97 dom = minidom.parse(input_filename) | 97 dom = minidom.parse(input_filename) |
98 if HasStyleResource(dom): | 98 if HasStyleResource(dom): |
99 raise Exception('error: style file ' + input_filename + | 99 raise Exception('error: style file ' + input_filename + |
100 ' should be under ' + input_dir + | 100 ' should be under ' + input_dir + |
101 '-v17 directory. Please refer to crbug.com/243952 ' | 101 '-v17 directory. Please refer to ' |
102 'for the details.') | 102 'http://crbug.com/243952 for the details.') |
103 | 103 |
104 | 104 |
105 def GenerateV14LayoutResourceDom(dom, filename): | 105 def GenerateV14LayoutResourceDom(dom, filename_for_warning): |
newt (away)
2013/07/08 22:55:49
it's OK to print both a warning and an error if a
Kibeom Kim (inactive)
2013/07/09 00:20:50
Looks like we have to skip warning for pre-v17 bec
newt (away)
2013/07/09 00:52:49
Where is that call? I only see this method called
| |
106 """Convert layout resource to API 14 compatible layout resource. | 106 """Convert layout resource to API 14 compatible layout resource. |
107 | 107 |
108 Args: | 108 Args: |
109 dom: parsed minidom object to be modified. | 109 dom: parsed minidom object to be modified. |
110 filename: file name to display in case we print warnings. | 110 filename_for_warning: file name to display in case we print warnings. |
111 If None, do not print warning. | |
111 Returns: | 112 Returns: |
112 True if dom is modified, False otherwise. | 113 True if dom is modified, False otherwise. |
113 """ | 114 """ |
114 is_modified = False | 115 is_modified = False |
115 | 116 |
116 # Iterate all the elements' attributes to find attributes to convert. | 117 # Iterate all the elements' attributes to find attributes to convert. |
117 for element in IterateXmlElements(dom): | 118 for element in IterateXmlElements(dom): |
118 for name, value in list(element.attributes.items()): | 119 for name, value in list(element.attributes.items()): |
119 # Convert any API 17 Start/End attributes to Left/Right attributes. | 120 # Convert any API 17 Start/End attributes to Left/Right attributes. |
120 # For example, from paddingStart="10dp" to paddingLeft="10dp" | 121 # For example, from paddingStart="10dp" to paddingLeft="10dp" |
121 # Note: gravity attributes are not necessary to convert because | 122 # Note: gravity attributes are not necessary to convert because |
122 # start/end values are backward-compatible. Explained at | 123 # start/end values are backward-compatible. Explained at |
123 # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom | 124 # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom |
124 if name in ATTRIBUTES_TO_MAP: | 125 if name in ATTRIBUTES_TO_MAP: |
125 element.setAttribute(ATTRIBUTES_TO_MAP[name], value) | 126 element.setAttribute(ATTRIBUTES_TO_MAP[name], value) |
126 del element.attributes[name] | 127 del element.attributes[name] |
127 is_modified = True | 128 is_modified = True |
128 else: | 129 elif filename_for_warning: |
129 WarnIfDeprecatedAttribute(name, value, filename) | 130 WarnIfDeprecatedAttribute(name, value, filename_for_warning) |
130 | 131 |
131 return is_modified | 132 return is_modified |
132 | 133 |
133 | 134 |
134 def GenerateV14StyleResourceDom(dom, filename): | 135 def GenerateV14StyleResourceDom(dom, filename_for_warning): |
135 """Convert style resource to API 14 compatible style resource. | 136 """Convert style resource to API 14 compatible style resource. |
136 | 137 |
137 Args: | 138 Args: |
138 dom: parsed minidom object to be modified. | 139 dom: parsed minidom object to be modified. |
139 filename: file name to display in case we print warnings. | 140 filename_for_warning: file name to display in case we print warnings. |
141 If None, do not print warning. | |
142 Returns: | |
143 True if dom is modified, False otherwise. | |
140 """ | 144 """ |
145 is_modified = False | |
146 | |
141 for style_element in dom.getElementsByTagName('style'): | 147 for style_element in dom.getElementsByTagName('style'): |
142 for item_element in style_element.getElementsByTagName('item'): | 148 for item_element in style_element.getElementsByTagName('item'): |
143 name = item_element.attributes['name'].value | 149 name = item_element.attributes['name'].value |
144 value = item_element.childNodes[0].nodeValue | 150 value = item_element.childNodes[0].nodeValue |
145 if name in ATTRIBUTES_TO_MAP: | 151 if name in ATTRIBUTES_TO_MAP: |
146 item_element.attributes['name'].value = ATTRIBUTES_TO_MAP[name] | 152 item_element.attributes['name'].value = ATTRIBUTES_TO_MAP[name] |
147 else: | 153 is_modified = True |
148 WarnIfDeprecatedAttribute(name, value, filename) | 154 elif filename_for_warning: |
155 WarnIfDeprecatedAttribute(name, value, filename_for_warning) | |
149 | 156 |
157 return is_modified | |
150 | 158 |
151 def GenerateV14LayoutResource(input_filename, output_v14_filename, | 159 def GenerateV14LayoutResource(input_filename, output_v14_filename, |
152 output_v17_filename): | 160 output_v17_filename): |
153 """Convert API 17 layout resource to API 14 compatible layout resource. | 161 """Convert API 17 layout resource to API 14 compatible layout resource. |
154 | 162 |
155 It's mostly a simple replacement, s/Start/Left s/End/Right, | 163 It's mostly a simple replacement, s/Start/Left s/End/Right, |
156 on the attribute names. | 164 on the attribute names. |
157 If the generated resource is identical to the original resource, | 165 If the generated resource is identical to the original resource, |
158 don't do anything. If not, write the generated resource to | 166 don't do anything. If not, write the generated resource to |
159 output_v14_filename, and copy the original resource to output_v17_filename. | 167 output_v14_filename, and copy the original resource to output_v17_filename. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 output_v17_filename) | 202 output_v17_filename) |
195 | 203 |
196 | 204 |
197 def GenerateV14StyleResourcesInDir(input_dir, output_v14_dir): | 205 def GenerateV14StyleResourcesInDir(input_dir, output_v14_dir): |
198 """Convert style resources to API 14 compatible resources in input_dir.""" | 206 """Convert style resources to API 14 compatible resources in input_dir.""" |
199 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 207 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
200 rel_filename = os.path.relpath(input_filename, input_dir) | 208 rel_filename = os.path.relpath(input_filename, input_dir) |
201 output_v14_filename = os.path.join(output_v14_dir, rel_filename) | 209 output_v14_filename = os.path.join(output_v14_dir, rel_filename) |
202 GenerateV14StyleResource(input_filename, output_v14_filename) | 210 GenerateV14StyleResource(input_filename, output_v14_filename) |
203 | 211 |
212 def VerifyNoRtlAttributeInDir(input_dir): | |
newt (away)
2013/07/08 22:55:49
how about "VerifyV14StyleResourcesInDir"?
Kibeom Kim (inactive)
2013/07/09 00:20:50
Done.
| |
213 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | |
214 dom = minidom.parse(input_filename) | |
215 if (GenerateV14LayoutResourceDom(dom, None) or | |
newt (away)
2013/07/08 22:55:49
it's a bit odd to run both of these methods on the
Kibeom Kim (inactive)
2013/07/09 00:20:50
Done.
| |
216 GenerateV14StyleResourceDom(dom, None)): | |
217 raise Exception('error : ' + input_filename + | |
218 ' has an RTL attribute, i.e., attribute that has "start"' | |
219 'or "end" in its name. Pre-v17 resources should not ' | |
220 'include it because it can cause crashes on certain ' | |
221 'devices. Please refer to http://crbug.com/243952 ' | |
222 'for the details.') | |
204 | 223 |
205 def ParseArgs(): | 224 def ParseArgs(): |
206 """Parses command line options. | 225 """Parses command line options. |
207 | 226 |
208 Returns: | 227 Returns: |
209 An options object as from optparse.OptionsParser.parse_args() | 228 An options object as from optparse.OptionsParser.parse_args() |
210 """ | 229 """ |
211 parser = optparse.OptionParser() | 230 parser = optparse.OptionParser() |
212 parser.add_option('--res-dir', | 231 parser.add_option('--res-dir', |
213 help='directory containing resources ' | 232 help='directory containing resources ' |
214 'used to generate v14 compatible resources') | 233 'used to generate v14 compatible resources') |
215 parser.add_option('--res-v14-compatibility-dir', | 234 parser.add_option('--res-v14-compatibility-dir', |
216 help='output directory into which ' | 235 help='output directory into which ' |
217 'v14 compatible resources will be generated') | 236 'v14 compatible resources will be generated') |
218 parser.add_option('--stamp', help='File to touch on success') | 237 parser.add_option('--stamp', help='File to touch on success') |
238 parser.add_option('--only-verify', help='Do not generate v14 resources. ' | |
newt (away)
2013/07/08 22:55:49
"Do not generate v14 resources. Instead, just veri
Kibeom Kim (inactive)
2013/07/09 00:20:50
Done.
| |
239 'Only verify it and raise an error if the current resource can ' | |
240 'cause crashes on certain devices.') | |
newt (away)
2013/07/08 22:55:49
use 'action="store_true"' here. then options.only_
Kibeom Kim (inactive)
2013/07/09 00:20:50
Done.
| |
219 | 241 |
220 options, args = parser.parse_args() | 242 options, args = parser.parse_args() |
221 | 243 |
222 if args: | 244 if args: |
223 parser.error('No positional arguments should be given.') | 245 parser.error('No positional arguments should be given.') |
224 | 246 |
225 # Check that required options have been provided. | 247 # Check that required options have been provided. |
226 required_options = ('res_dir', 'res_v14_compatibility_dir') | 248 required_options = ('res_dir', 'res_v14_compatibility_dir') |
227 build_utils.CheckOptions(options, parser, required=required_options) | 249 build_utils.CheckOptions(options, parser, required=required_options) |
228 return options | 250 return options |
229 | 251 |
230 | 252 |
231 def main(argv): | 253 def main(argv): |
232 options = ParseArgs() | 254 options = ParseArgs() |
255 only_verify = bool(options.only_verify) and int(options.only_verify) > 0 | |
newt (away)
2013/07/08 22:55:49
remove this
Kibeom Kim (inactive)
2013/07/09 00:20:50
Done.
| |
233 | 256 |
234 build_utils.DeleteDirectory(options.res_v14_compatibility_dir) | 257 build_utils.DeleteDirectory(options.res_v14_compatibility_dir) |
235 build_utils.MakeDirectory(options.res_v14_compatibility_dir) | 258 build_utils.MakeDirectory(options.res_v14_compatibility_dir) |
236 | 259 |
237 for name in os.listdir(options.res_dir): | 260 for name in os.listdir(options.res_dir): |
238 if not os.path.isdir(os.path.join(options.res_dir, name)): | 261 if not os.path.isdir(os.path.join(options.res_dir, name)): |
239 continue | 262 continue |
240 | 263 |
241 dir_pieces = name.split('-') | 264 dir_pieces = name.split('-') |
242 resource_type = dir_pieces[0] | 265 resource_type = dir_pieces[0] |
243 qualifiers = dir_pieces[1:] | 266 qualifiers = dir_pieces[1:] |
244 | 267 |
245 api_level_qualifier_index = -1 | 268 api_level_qualifier_index = -1 |
246 api_level_qualifier = '' | 269 api_level_qualifier = '' |
247 for index, qualifier in enumerate(qualifiers): | 270 for index, qualifier in enumerate(qualifiers): |
248 if re.match('v[0-9]+$', qualifier): | 271 if re.match('v[0-9]+$', qualifier): |
249 api_level_qualifier_index = index | 272 api_level_qualifier_index = index |
250 api_level_qualifier = qualifier | 273 api_level_qualifier = qualifier |
251 break | 274 break |
252 | 275 |
253 # Android pre-v17 API doesn't support RTL. Skip. | 276 # Android pre-v17 API doesn't support RTL. Skip. |
254 if 'ldrtl' in qualifiers: | 277 if 'ldrtl' in qualifiers: |
255 continue | 278 continue |
256 | 279 |
257 # We also need to copy the original v17 resource to *-v17 directory | 280 input_dir = os.path.abspath(os.path.join(options.res_dir, name)) |
258 # because the generated v14 resource will hide the original resource. | |
259 input_dir = os.path.join(options.res_dir, name) | |
260 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, name) | |
261 output_v17_dir = os.path.join(options.res_v14_compatibility_dir, name + | |
262 '-v17') | |
263 | 281 |
264 # We only convert layout resources under layout*/, xml*/, | 282 if only_verify: |
265 # and style resources under values*/. | 283 if not api_level_qualifier or int(api_level_qualifier[1:]) < 17: |
266 if resource_type in ('layout', 'xml'): | 284 if resource_type in ('layout', 'xml', 'values'): |
267 if not api_level_qualifier: | 285 VerifyNoRtlAttributeInDir(input_dir) |
268 GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, | 286 else: |
269 output_v17_dir) | 287 # We also need to copy the original v17 resource to *-v17 directory |
270 elif resource_type == 'values': | 288 # because the generated v14 resource will hide the original resource. |
271 if api_level_qualifier == 'v17': | 289 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, name) |
272 output_qualifiers = qualifiers[:] | 290 output_v17_dir = os.path.join(options.res_v14_compatibility_dir, name + |
273 del output_qualifiers[api_level_qualifier_index] | 291 '-v17') |
274 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, | 292 |
275 '-'.join([resource_type] + | 293 # We only convert layout resources under layout*/, xml*/, |
276 output_qualifiers)) | 294 # and style resources under values*/. |
277 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) | 295 if resource_type in ('layout', 'xml'): |
278 elif not api_level_qualifier: | 296 if not api_level_qualifier: |
279 ErrorIfStyleResourceExistsInDir(input_dir) | 297 GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, |
298 output_v17_dir) | |
299 elif resource_type == 'values': | |
300 if api_level_qualifier == 'v17': | |
301 output_qualifiers = qualifiers[:] | |
302 del output_qualifiers[api_level_qualifier_index] | |
303 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, | |
304 '-'.join([resource_type] + | |
305 output_qualifiers)) | |
306 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) | |
307 elif not api_level_qualifier: | |
308 ErrorIfStyleResourceExistsInDir(input_dir) | |
280 | 309 |
281 if options.stamp: | 310 if options.stamp: |
282 build_utils.Touch(options.stamp) | 311 build_utils.Touch(options.stamp) |
283 | 312 |
284 if __name__ == '__main__': | 313 if __name__ == '__main__': |
285 sys.exit(main(sys.argv)) | 314 sys.exit(main(sys.argv)) |
286 | 315 |
OLD | NEW |