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