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 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 options, args = parser.parse_args() | 273 options, args = parser.parse_args() |
274 | 274 |
275 if args: | 275 if args: |
276 parser.error('No positional arguments should be given.') | 276 parser.error('No positional arguments should be given.') |
277 | 277 |
278 # Check that required options have been provided. | 278 # Check that required options have been provided. |
279 required_options = ('res_dir', 'res_v14_compatibility_dir') | 279 required_options = ('res_dir', 'res_v14_compatibility_dir') |
280 build_utils.CheckOptions(options, parser, required=required_options) | 280 build_utils.CheckOptions(options, parser, required=required_options) |
281 return options | 281 return options |
282 | 282 |
| 283 def GenerateV14Resources(res_dir, res_v14_dir, verify_only): |
| 284 build_utils.DeleteDirectory(res_v14_dir) |
| 285 build_utils.MakeDirectory(res_v14_dir) |
283 | 286 |
284 def main(): | 287 for name in os.listdir(res_dir): |
285 options = ParseArgs() | 288 if not os.path.isdir(os.path.join(res_dir, name)): |
286 | |
287 build_utils.DeleteDirectory(options.res_v14_compatibility_dir) | |
288 build_utils.MakeDirectory(options.res_v14_compatibility_dir) | |
289 | |
290 for name in os.listdir(options.res_dir): | |
291 if not os.path.isdir(os.path.join(options.res_dir, name)): | |
292 continue | 289 continue |
293 | 290 |
294 dir_pieces = name.split('-') | 291 dir_pieces = name.split('-') |
295 resource_type = dir_pieces[0] | 292 resource_type = dir_pieces[0] |
296 qualifiers = dir_pieces[1:] | 293 qualifiers = dir_pieces[1:] |
297 | 294 |
298 api_level_qualifier_index = -1 | 295 api_level_qualifier_index = -1 |
299 api_level_qualifier = '' | 296 api_level_qualifier = '' |
300 for index, qualifier in enumerate(qualifiers): | 297 for index, qualifier in enumerate(qualifiers): |
301 if re.match('v[0-9]+$', qualifier): | 298 if re.match('v[0-9]+$', qualifier): |
302 api_level_qualifier_index = index | 299 api_level_qualifier_index = index |
303 api_level_qualifier = qualifier | 300 api_level_qualifier = qualifier |
304 break | 301 break |
305 | 302 |
306 # Android pre-v17 API doesn't support RTL. Skip. | 303 # Android pre-v17 API doesn't support RTL. Skip. |
307 if 'ldrtl' in qualifiers: | 304 if 'ldrtl' in qualifiers: |
308 continue | 305 continue |
309 | 306 |
310 input_dir = os.path.abspath(os.path.join(options.res_dir, name)) | 307 input_dir = os.path.abspath(os.path.join(res_dir, name)) |
311 | 308 |
312 if options.verify_only: | 309 if verify_only: |
313 if not api_level_qualifier or int(api_level_qualifier[1:]) < 17: | 310 if not api_level_qualifier or int(api_level_qualifier[1:]) < 17: |
314 VerifyV14ResourcesInDir(input_dir, resource_type) | 311 VerifyV14ResourcesInDir(input_dir, resource_type) |
315 else: | 312 else: |
316 AssertNoDeprecatedAttributesInDir(input_dir, resource_type) | 313 AssertNoDeprecatedAttributesInDir(input_dir, resource_type) |
317 else: | 314 else: |
318 # We also need to copy the original v17 resource to *-v17 directory | 315 # We also need to copy the original v17 resource to *-v17 directory |
319 # because the generated v14 resource will hide the original resource. | 316 # because the generated v14 resource will hide the original resource. |
320 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, name) | 317 output_v14_dir = os.path.join(res_v14_dir, name) |
321 output_v17_dir = os.path.join(options.res_v14_compatibility_dir, name + | 318 output_v17_dir = os.path.join(res_v14_dir, name + '-v17') |
322 '-v17') | |
323 | 319 |
324 # We only convert layout resources under layout*/, xml*/, | 320 # We only convert layout resources under layout*/, xml*/, |
325 # and style resources under values*/. | 321 # and style resources under values*/. |
326 if resource_type in ('layout', 'xml'): | 322 if resource_type in ('layout', 'xml'): |
327 if not api_level_qualifier: | 323 if not api_level_qualifier: |
328 GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, | 324 GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, |
329 output_v17_dir) | 325 output_v17_dir) |
330 elif resource_type == 'values': | 326 elif resource_type == 'values': |
331 if api_level_qualifier == 'v17': | 327 if api_level_qualifier == 'v17': |
332 output_qualifiers = qualifiers[:] | 328 output_qualifiers = qualifiers[:] |
333 del output_qualifiers[api_level_qualifier_index] | 329 del output_qualifiers[api_level_qualifier_index] |
334 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, | 330 output_v14_dir = os.path.join(res_v14_dir, |
335 '-'.join([resource_type] + | 331 '-'.join([resource_type] + |
336 output_qualifiers)) | 332 output_qualifiers)) |
337 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) | 333 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) |
338 elif not api_level_qualifier: | 334 elif not api_level_qualifier: |
339 ErrorIfStyleResourceExistsInDir(input_dir) | 335 ErrorIfStyleResourceExistsInDir(input_dir) |
340 | 336 |
| 337 def main(): |
| 338 options = ParseArgs() |
| 339 |
| 340 GenerateV14Resources( |
| 341 options.res_dir, options.res_v14_compatibility_dir, options.verify_only) |
| 342 |
341 if options.stamp: | 343 if options.stamp: |
342 build_utils.Touch(options.stamp) | 344 build_utils.Touch(options.stamp) |
343 | 345 |
344 if __name__ == '__main__': | 346 if __name__ == '__main__': |
345 sys.exit(main()) | 347 sys.exit(main()) |
346 | 348 |
OLD | NEW |