Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 """Convert Android xml resources to API 14 compatible. | |
| 8 | |
| 9 There are two reasons that we cannot just use API attributes, | |
| 10 so we are generating another set of resources by this script. | |
| 11 | |
| 12 1. paddingStart attribute can cause a crash on Galaxy Tab 2. b/8351339 | |
| 13 2. There is a bug that paddingStart does not override paddingLeft on | |
| 14 JB-MR1 b/8654490 . This is fixed on JB-MR2. | |
| 15 | |
| 16 Therefore, this resource generation script can be removed when | |
| 17 we drop the support for JB-MR1. | |
| 18 | |
| 19 Please refer to crbug.com/235118 for the details. | |
|
cjhopman
2013/05/03 21:23:04
Nit: http://
Kibeom Kim (inactive)
2013/05/03 22:09:57
Done.
| |
| 20 """ | |
| 21 | |
| 22 import optparse | |
| 23 import os | |
| 24 import re | |
| 25 import sys | |
| 26 import xml.dom.minidom as minidom | |
| 27 | |
| 28 from util import build_utils | |
| 29 | |
| 30 | |
| 31 ATTRIBUTE_NAMESPACE = 'http://schemas.android.com/apk/res/android' | |
| 32 | |
| 33 # The list of the attributes were taken from go/android-rtl | |
|
cjhopman
2013/05/03 21:23:04
go/android-rtl is an internal link. Is this list a
Kibeom Kim (inactive)
2013/05/03 22:09:57
Done.
| |
| 34 ATTRIBUTES_TO_MAP = {'paddingStart' : 'paddingLeft', | |
| 35 'drawableStart' : 'drawableLeft', | |
| 36 'layout_alignStart' : 'layout_alignLeft', | |
| 37 'layout_marginStart' : 'layout_marginLeft', | |
| 38 'layout_alignParentStart' : 'layout_alignParentLeft', | |
| 39 'layout_toStartOf' : 'layout_toLeftOf', | |
| 40 'paddingEnd' : 'paddingRight', | |
| 41 'drawableEnd' : 'drawableRight', | |
| 42 'layout_alignEnd' : 'layout_alignRight', | |
| 43 'layout_marginEnd' : 'layout_marginRight', | |
| 44 'layout_alignParentEnd' : 'layout_alignParentRight', | |
| 45 'layout_toEndOf' : 'layout_toRightOf'} | |
| 46 | |
| 47 ATTRIBUTES_TO_MAP_NS = {} | |
| 48 | |
| 49 for k, v in ATTRIBUTES_TO_MAP.items(): | |
| 50 ATTRIBUTES_TO_MAP_NS[(ATTRIBUTE_NAMESPACE, k)] = (ATTRIBUTE_NAMESPACE, v) | |
| 51 | |
| 52 ATTRIBUTES_TO_MAP_NS_VALUES = set(ATTRIBUTES_TO_MAP_NS.values()) | |
| 53 | |
| 54 | |
| 55 def IterateXmlElements(node): | |
| 56 """minidom helper function that iterates all the element nodes. | |
| 57 Iteration order is pre-order depth-first.""" | |
| 58 if node.nodeType == node.ELEMENT_NODE: | |
| 59 yield node | |
| 60 for child_node in node.childNodes: | |
| 61 for child_node_element in IterateXmlElements(child_node): | |
| 62 yield child_node_element | |
| 63 | |
| 64 | |
| 65 def GenerateV14Resource(input_filename, output_filename): | |
| 66 """Convert resource to API 14 compatible resource. | |
| 67 | |
| 68 It's mostly a simple replacement, s/Start/Left s/End/Right, | |
| 69 on the attribute names.""" | |
| 70 dom = minidom.parse(input_filename) | |
| 71 | |
| 72 for element in IterateXmlElements(dom): | |
| 73 all_names = element.attributes.keysNS() | |
| 74 | |
| 75 # Iterate all the attributes to find attributes to convert. | |
| 76 # Note that name variable is actually a tuple that has namespace and name. | |
| 77 # For example, | |
| 78 # name == ('http://schemas.android.com/apk/res/android', 'paddingStart') | |
| 79 for name, value in list(element.attributes.itemsNS()): | |
| 80 # Note: gravity attributes are not necessary to convert because | |
| 81 # start/end values are backward-compatible. Explained at | |
| 82 # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom | |
| 83 | |
| 84 # Convert any other API 17 Start/End attributes to Left/Right attributes. | |
| 85 # For example, from paddingStart="10dp" to paddingLeft="10dp" | |
| 86 if name in ATTRIBUTES_TO_MAP_NS: | |
| 87 mapped_name = ATTRIBUTES_TO_MAP_NS[name] | |
| 88 | |
| 89 # Add the new mapped attribute and remove the original attribute. | |
| 90 # For example, add paddingLeft and remove paddingStart. | |
| 91 # Note that instead of element.setAttribute(...), this is more correct. | |
| 92 # element.setAttributeNS(mapped_name[0], mapped_name[1], value) | |
| 93 # However, there is a minidom bug that doesn't print namespace set by | |
|
cjhopman
2013/05/03 21:23:04
Nit: is there some kind of minidom bug report you
Kibeom Kim (inactive)
2013/05/03 22:09:57
Done.
| |
| 94 # setAttributeNS. Hence this workaround. | |
| 95 element.setAttribute('android:' + mapped_name[1], value) | |
| 96 del element.attributes[name] | |
| 97 elif name in ATTRIBUTES_TO_MAP_NS_VALUES: | |
| 98 # TODO(kkimlabs): Enable warning once layouts have been converted | |
| 99 # print >> sys.stderror, 'Warning: layout should use xxx instead of yyy' | |
| 100 pass | |
| 101 | |
| 102 build_utils.MakeDirectory(os.path.dirname(output_filename)) | |
| 103 with open(output_filename, 'w') as f: | |
| 104 dom.writexml(f, ' ', '\n', encoding='utf-8') | |
| 105 | |
| 106 | |
| 107 def GenerateV14ResourcesInDir(input_dir, output_dir): | |
| 108 """Convert resources to API 14 compatible XML resources | |
| 109 in the directory.""" | |
| 110 for input_file in build_utils.FindInDirectory(input_dir, '*.xml'): | |
| 111 output_path = os.path.join(output_dir, | |
| 112 os.path.relpath(input_file, input_dir)) | |
| 113 GenerateV14Resource(input_file, output_path) | |
| 114 | |
| 115 | |
| 116 def ParseArgs(): | |
| 117 """Parses command line options. | |
| 118 | |
| 119 Returns: | |
| 120 An options object as from optparse.OptionsParser.parse_args() | |
| 121 """ | |
| 122 parser = optparse.OptionParser() | |
| 123 parser.add_option('--res-dir', | |
| 124 help='directory containing resources ' | |
| 125 'used to generate v14 resources') | |
| 126 parser.add_option('--res-v14-dir', | |
| 127 help='output directory into which ' | |
| 128 'v14 resources will be generated') | |
| 129 parser.add_option('--stamp', help='File to touch on success') | |
| 130 | |
| 131 options, args = parser.parse_args() | |
| 132 | |
| 133 if args: | |
| 134 parser.error('No positional arguments should be given.') | |
| 135 | |
| 136 # Check that required options have been provided. | |
| 137 required_options = ('res_dir', 'res_v14_dir') | |
| 138 build_utils.CheckOptions(options, parser, required=required_options) | |
| 139 return options | |
| 140 | |
| 141 | |
| 142 def main(argv): | |
| 143 options = ParseArgs() | |
| 144 | |
| 145 build_utils.DeleteDirectory(options.res_v14_dir) | |
| 146 build_utils.MakeDirectory(options.res_v14_dir) | |
| 147 | |
| 148 for name in os.listdir(options.res_dir): | |
| 149 if not os.path.isdir(os.path.join(options.res_dir, name)): | |
| 150 continue | |
| 151 | |
| 152 dir_pieces = name.split('-') | |
| 153 resource_type = dir_pieces[0] | |
| 154 qualifiers = dir_pieces[1:] | |
| 155 | |
| 156 # We only convert resources under layout*/ and xml*/. | |
| 157 if resource_type not in ('layout', 'xml'): | |
| 158 continue | |
| 159 | |
| 160 # Android pre-v17 API doesn't support RTL. Skip. | |
| 161 if 'ldrtl' in qualifiers: | |
| 162 continue | |
| 163 | |
| 164 # Convert all the resource files. | |
| 165 input_path = os.path.join(options.res_dir, name) | |
| 166 output_path = os.path.join(options.res_v14_dir, name) | |
| 167 GenerateV14ResourcesInDir(input_path, output_path) | |
| 168 | |
| 169 if options.stamp: | |
| 170 build_utils.Touch(options.stamp) | |
| 171 | |
| 172 if __name__ == '__main__': | |
| 173 sys.exit(main(sys.argv)) | |
| 174 | |
| OLD | NEW |