Chromium Code Reviews| Index: build/android/gyp/generate_v14_resources.py |
| diff --git a/build/android/gyp/generate_v14_resources.py b/build/android/gyp/generate_v14_resources.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..2936b7e539bd5103e48d59ddaeb70b3e182290ae |
| --- /dev/null |
| +++ b/build/android/gyp/generate_v14_resources.py |
| @@ -0,0 +1,170 @@ |
| +#!/usr/bin/env python |
| +# |
| +# Copyright 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +import optparse |
| +import os |
| +import re |
| +import sys |
| +import xml.dom.minidom as minidom |
| + |
| +from util import build_utils |
| + |
| + |
| +ATTRIBUTE_NAMESPACE = u'http://schemas.android.com/apk/res/android' |
| + |
| +# The list of the attributes were taken from go/android-rtl |
| +ATTRIBUTES_TO_MAP = {'paddingStart' : 'paddingLeft', |
| + 'drawableStart' : 'drawableLeft', |
| + 'layout_alignStart' : 'layout_alignLeft', |
| + 'layout_marginStart' : 'layout_marginLeft', |
| + 'layout_alignParentStart' : 'layout_alignParentLeft', |
| + 'layout_toStartOf' : 'layout_toLeftOf', |
| + 'paddingEnd' : 'paddingRight', |
| + 'drawableEnd' : 'drawableRight', |
| + 'layout_alignEnd' : 'layout_alignRight', |
| + 'layout_marginEnd' : 'layout_marginRight', |
| + 'layout_alignParentEnd' : 'layout_alignParentRight', |
| + 'layout_toEndOf' : 'layout_toRightOf'} |
| + |
| +ATTRIBUTES_TO_MAP_NS = {} |
| + |
| +for k, v in ATTRIBUTES_TO_MAP.items(): |
| + ATTRIBUTES_TO_MAP_NS[(ATTRIBUTE_NAMESPACE, k)] = (ATTRIBUTE_NAMESPACE , v) |
|
newt (away)
2013/04/26 21:22:24
remove space before comma
cjhopman
2013/04/26 21:50:00
Also, remove one space after first comma
Kibeom Kim (inactive)
2013/04/27 00:05:07
Done.
Kibeom Kim (inactive)
2013/04/27 00:05:07
Done.
|
| + |
| + |
| +def iterate_xml_elements(node): |
| + """minidom helper function that iterates all the element nodes. Depth-first. |
| + """ |
| + if node.nodeType == node.ELEMENT_NODE: |
| + yield node |
| + for child_node in node.childNodes: |
| + for child_node_element in iterate_xml_elements(child_node): |
| + yield child_node_element |
| + |
| + |
| +def generate_pre_v17_resource(input_filename, output_filename): |
| + """Convert resource to API 14 compatible resource. |
| + |
| + It's mostly a simple replacement, s/Start/Left s/End/Right, |
| + on the attribute names. |
| + """ |
| + dom = minidom.parse(input_filename) |
| + |
| + for element in iterate_xml_elements(dom): |
| + all_names = element.attributes.keysNS() |
| + |
| + # Iterate all the attributes to find attributes to convert. |
| + # Note that name variable is actually a tuple that has namespace and name. |
| + # For example, |
| + # name == ('http://schemas.android.com/apk/res/android', 'paddingStart') |
| + for name, value in list(element.attributes.itemsNS()): |
|
newt (away)
2013/04/26 21:22:24
you should be able to remove "list"
Kibeom Kim (inactive)
2013/04/27 00:05:07
Is it ok? I have del element.attributes[name] in t
|
| + # Note: gravity attributes are not necessary to convert because |
| + # start/end values are backward-compatible. Explained at |
| + # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom |
| + |
| + # Convert any other API 17 Start/End attributes to Left/Right attributes. |
| + # For example, from paddingStart="10dp" to paddingLeft="10dp" |
| + if name in ATTRIBUTES_TO_MAP_NS: |
| + mapped_name = ATTRIBUTES_TO_MAP_NS[name] |
| + |
| + # Add the new mapped attribute and remove the original attribute. |
| + # For example, add paddingLeft and remove paddingStart. |
| + element.setAttributeNS(mapped_name[0], mapped_name[1], value) |
| + del element.attributes[name] |
| + elif name in ATTRIBUTES_TO_MAP.values(): |
|
newt (away)
2013/04/26 21:22:24
ATTRIBUTES_TO_MAP_NS
also, not sure how long this
Kibeom Kim (inactive)
2013/04/27 00:05:07
Done.
|
| + # TODO(kkimlabs): Enable warning once layouts have been converted |
| + # print >> sys.stderror, 'Warning: layout should use xxx instead of yyy' |
| + pass |
| + |
| + build_utils.MakeDirectory(os.path.dirname(output_filename)) |
| + dom.writexml(open(output_filename,"w"), ' ', '\n') |
|
newt (away)
2013/04/26 21:22:24
be sure to close these files! :)
with open(ou
Kibeom Kim (inactive)
2013/04/27 00:05:07
Done.
|
| + |
| + |
| +def generate_v14_resources_in_dir(input_dir, output_dir): |
| + """Convert resources to API 14 compatible resources in the directory. |
| + """ |
| + |
| + for name in os.listdir(options.res_dir): |
| + if not os.path.isfile(name): |
| + continue |
| + |
| + if not name.lower().endswith('.xml'): |
| + continue |
| + |
| + input_filename = os.path.join(input_dir, name) |
| + output_filename = os.path.join(output_dir, name) |
| + generate_pre_v17_resource(input_filename, output_filename) |
| + |
| + |
| +def ParseArgs(): |
| + """Parses command line options. |
| + |
| + Returns: |
| + An options object as from optparse.OptionsParser.parse_args() |
| + """ |
| + parser = optparse.OptionParser() |
| + parser.add_option('--res-dir', |
| + help='directory containing resources ' |
| + 'be used to generate v14 resources') |
|
newt (away)
2013/04/26 21:22:24
oops.. remove "be"
Kibeom Kim (inactive)
2013/04/27 00:05:07
Done.
|
| + parser.add_option('--res-v14-dir', |
| + help='directory containing v14 resources to be generated') |
|
newt (away)
2013/04/26 21:22:24
"output directory into which v14 resources will be
Kibeom Kim (inactive)
2013/04/27 00:05:07
Done.
|
| + |
| + (options, args) = parser.parse_args() |
| + |
| + if args: |
| + parser.error('No positional arguments should be given.') |
| + |
| + # Check that required options have been provided. |
| + required_options = ('res_dir', 'res_v14_dir') |
| + build_utils.CheckOptions(options, parser, required=required_options) |
| + return options |
| + |
| + |
| +def main(argv): |
|
newt (away)
2013/04/26 21:22:24
we should probably delete the output directory bef
Kibeom Kim (inactive)
2013/04/27 00:05:07
Done.
|
| + """Convert Android xml resources to API 14 compatible. |
| + |
| + There are two reasons that we cannot just use API attributes, |
| + so we are generating another set of resources by this script. |
| + |
| + 1. paddingStart attribute can cause a crash on Galaxy Tab 2. b/8351339 |
| + 2. There is a bug that paddingStart does not override paddingLeft on |
| + JB-MR1 b/8654490 . This is fixed on JB-MR2. |
| + |
| + Therefore, this resource generation script can be removed when |
| + we drop the support for JB-MR1. |
| + |
| + Please refer to crbug.com/235118 for the details. |
| + """ |
| + |
| + options = ParseArgs() |
| + |
| + build_utils.MakeDirectory(options.res_v14_dir) |
| + |
| + for name in os.listdir(options.res_dir): |
| + if not os.path.isdir(name): |
| + continue |
| + |
| + dir_pieces = name.split('-') |
| + resource_type = dir_pieces[0] |
| + qualifiers = dir_pieces[1:] |
| + |
| + # We only convert resources under layout*/ and xml*/. |
| + if resource_type not in ('layout', 'xml'): |
| + continue |
| + |
| + # Android pre-v17 API doesn't support RTL. Skip. |
| + if 'ldrtl' in qualifiers: |
| + continue |
| + |
| + # Convert all the resource files. |
| + input_path = os.path.join(options.res_dir, name) |
| + output_path = os.path.join(options.res_v14_dir, name) |
| + generate_v14_resources_in_dir(input_path, output_path) |
| + |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main(sys.argv)) |
| + |