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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
69 if name in ATTRIBUTES_TO_MAP_REVERSED: | 69 if name in ATTRIBUTES_TO_MAP_REVERSED: |
70 print >> sys.stderr, ('warning: ' + filename + ' should use ' + | 70 print >> sys.stderr, ('warning: ' + filename + ' should use ' + |
71 ATTRIBUTES_TO_MAP_REVERSED[name] + | 71 ATTRIBUTES_TO_MAP_REVERSED[name] + |
72 ' instead of ' + name) | 72 ' instead of ' + name) |
73 elif name in GRAVITY_ATTRIBUTES and ('left' in value or 'right' in value): | 73 elif name in GRAVITY_ATTRIBUTES and ('left' in value or 'right' in value): |
74 print >> sys.stderr, ('warning: ' + filename + | 74 print >> sys.stderr, ('warning: ' + filename + |
75 ' should use start/end instead of left/right for ' + | 75 ' should use start/end instead of left/right for ' + |
76 name) | 76 name) |
77 | 77 |
78 | 78 |
79 def GenerateV14LayoutResource(dom, filename): | 79 def HasStyleResource(dom): |
80 """Return True if the dom is a style resource, False otherwise.""" | |
81 root_node = IterateXmlElements(dom).next() | |
82 if root_node.nodeName == 'resources': | |
83 for style_element in dom.getElementsByTagName('style'): | |
newt (away)
2013/05/28 21:08:13
make this:
if list(root_node.getElementsByTag
Kibeom Kim (inactive)
2013/05/28 23:15:32
Done.
| |
84 return True | |
85 return False | |
86 | |
87 | |
88 def ErrorIfStyleResourceExistsInDir(input_dir): | |
89 """If a style resource is in input_dir, exist with an error message.""" | |
90 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | |
91 dom = minidom.parse(input_filename) | |
92 if HasStyleResource(dom): | |
93 sys.exit('error: style file ' + input_filename + ' should be under ' + | |
newt (away)
2013/05/28 21:08:13
I'd raise an exception here, instead of exiting di
Kibeom Kim (inactive)
2013/05/28 23:15:32
Done.
| |
94 input_dir + '-v17 directory. Please refer to crbug.com/243952 ' | |
95 'for the details.') | |
96 | |
97 | |
98 def GenerateV14LayoutResourceDom(dom, filename): | |
80 """Convert layout resource to API 14 compatible layout resource. | 99 """Convert layout resource to API 14 compatible layout resource. |
81 | 100 |
82 Args: | 101 Args: |
83 dom: parsed minidom object to be modified. | 102 dom: parsed minidom object to be modified. |
84 filename: file name to display in case we print warnings. | 103 filename: file name to display in case we print warnings. |
85 Returns: | 104 Returns: |
86 True if dom is modified, False otherwise. | 105 True if dom is modified, False otherwise. |
87 """ | 106 """ |
88 is_modified = False | 107 is_modified = False |
89 | 108 |
90 # Iterate all the elements' attributes to find attributes to convert. | 109 # Iterate all the elements' attributes to find attributes to convert. |
91 for element in IterateXmlElements(dom): | 110 for element in IterateXmlElements(dom): |
92 for name, value in list(element.attributes.items()): | 111 for name, value in list(element.attributes.items()): |
93 # Convert any API 17 Start/End attributes to Left/Right attributes. | 112 # Convert any API 17 Start/End attributes to Left/Right attributes. |
94 # For example, from paddingStart="10dp" to paddingLeft="10dp" | 113 # For example, from paddingStart="10dp" to paddingLeft="10dp" |
95 # Note: gravity attributes are not necessary to convert because | 114 # Note: gravity attributes are not necessary to convert because |
96 # start/end values are backward-compatible. Explained at | 115 # start/end values are backward-compatible. Explained at |
97 # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom | 116 # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom |
98 if name in ATTRIBUTES_TO_MAP: | 117 if name in ATTRIBUTES_TO_MAP: |
99 element.setAttribute(ATTRIBUTES_TO_MAP[name], value) | 118 element.setAttribute(ATTRIBUTES_TO_MAP[name], value) |
100 del element.attributes[name] | 119 del element.attributes[name] |
101 is_modified = True | 120 is_modified = True |
102 else: | 121 else: |
103 WarnIfDeprecatedAttribute(name, value, filename) | 122 WarnIfDeprecatedAttribute(name, value, filename) |
104 | 123 |
105 return is_modified | 124 return is_modified |
106 | 125 |
107 | 126 |
108 def GenerateV14StyleResource(dom, filename): | 127 def GenerateV14StyleResourceDom(dom, filename): |
109 """Convert style resource to API 14 compatible style resource. | 128 """Convert style resource to API 14 compatible style resource. |
110 | 129 |
111 Args: | 130 Args: |
112 dom: parsed minidom object to be modified. | 131 dom: parsed minidom object to be modified. |
113 filename: file name to display in case we print warnings. | 132 filename: file name to display in case we print warnings. |
114 Returns: | |
115 True if dom is modified, False otherwise. | |
116 """ | 133 """ |
117 is_modified = False | |
118 | |
119 for style_element in dom.getElementsByTagName('style'): | 134 for style_element in dom.getElementsByTagName('style'): |
120 for item_element in style_element.getElementsByTagName('item'): | 135 for item_element in style_element.getElementsByTagName('item'): |
121 name = item_element.attributes['name'].value | 136 name = item_element.attributes['name'].value |
122 value = item_element.childNodes[0].nodeValue | 137 value = item_element.childNodes[0].nodeValue |
123 if name in ATTRIBUTES_TO_MAP: | 138 if name in ATTRIBUTES_TO_MAP: |
124 item_element.attributes['name'].value = ATTRIBUTES_TO_MAP[name] | 139 item_element.attributes['name'].value = ATTRIBUTES_TO_MAP[name] |
125 is_modified = True | |
126 else: | 140 else: |
127 WarnIfDeprecatedAttribute(name, value, filename) | 141 WarnIfDeprecatedAttribute(name, value, filename) |
128 | 142 |
129 return is_modified | |
130 | 143 |
131 | 144 def GenerateV14LayoutResource(input_filename, |
132 def GenerateV14Resource(input_filename, | |
133 output_v14_filename, | 145 output_v14_filename, |
newt (away)
2013/05/28 21:08:13
align these parameters
Kibeom Kim (inactive)
2013/05/28 23:15:32
Done.
| |
134 output_v17_filename): | 146 output_v17_filename): |
135 """Convert layout/style resource to API 14 compatible layout/style resource. | 147 """Convert layout/style resource to API 14 compatible layout/style resource. |
newt (away)
2013/05/28 21:08:13
"layout/style" -> "layout"
Kibeom Kim (inactive)
2013/05/28 23:15:32
Done.
| |
136 | 148 |
137 It's mostly a simple replacement, s/Start/Left s/End/Right, | 149 It's mostly a simple replacement, s/Start/Left s/End/Right, |
138 on the attribute names. | 150 on the attribute names. |
139 If the generated resource is identical to the original resource, | 151 If the generated resource is identical to the original resource, |
140 don't do anything. If not, write the generated resource to | 152 don't do anything. If not, write the generated resource to |
141 output_v14_filename, and copy the original resource to output_v17_filename. | 153 output_v14_filename, and copy the original resource to output_v17_filename. |
142 """ | 154 """ |
143 dom = minidom.parse(input_filename) | 155 dom = minidom.parse(input_filename) |
144 | 156 is_modified = GenerateV14LayoutResourceDom(dom, input_filename) |
145 root_node = IterateXmlElements(dom).next() | |
146 if root_node.nodeName == 'resources': | |
147 # Style resources are under 'values*/' directory. | |
148 is_modified = GenerateV14StyleResource(dom, input_filename) | |
149 else: | |
150 # Layout resources can be under 'layout*/' or 'xml*/' directory. | |
151 is_modified = GenerateV14LayoutResource(dom, input_filename) | |
152 | 157 |
153 if is_modified: | 158 if is_modified: |
154 # Write the generated resource. | 159 # Write the generated resource. |
155 build_utils.MakeDirectory(os.path.dirname(output_v14_filename)) | 160 build_utils.MakeDirectory(os.path.dirname(output_v14_filename)) |
156 with open(output_v14_filename, 'w') as f: | 161 with open(output_v14_filename, 'w') as f: |
157 dom.writexml(f, '', ' ', '\n', encoding='utf-8') | 162 dom.writexml(f, '', ' ', '\n', encoding='utf-8') |
158 | 163 |
159 # Copy the original resource. | 164 # Copy the original resource. |
160 build_utils.MakeDirectory(os.path.dirname(output_v17_filename)) | 165 build_utils.MakeDirectory(os.path.dirname(output_v17_filename)) |
161 shutil.copy2(input_filename, output_v17_filename) | 166 shutil.copy2(input_filename, output_v17_filename) |
162 | 167 |
163 | 168 |
164 def GenerateV14XmlResourcesInDir(input_dir, output_v14_dir, output_v17_dir): | 169 def GenerateV14StyleResource(input_filename, output_v14_filename): |
165 """Convert resources to API 14 compatible XML resources in the directory.""" | 170 """Convert layout/style resource to API 14 compatible layout/style resource. |
newt (away)
2013/05/28 21:08:13
"Convert API 17 style resources to API 14 compatib
Kibeom Kim (inactive)
2013/05/28 23:15:32
Done.
| |
171 | |
172 Write the generated style resource to output_v14_filename. | |
173 It's mostly a simple replacement, s/Start/Left s/End/Right, | |
174 on the attribute names. | |
175 """ | |
176 dom = minidom.parse(input_filename) | |
177 GenerateV14StyleResourceDom(dom, input_filename) | |
178 | |
179 # Write the generated resource. | |
newt (away)
2013/05/28 21:08:13
I'd pull out lines 179-182 into a separate functio
Kibeom Kim (inactive)
2013/05/28 23:15:32
Done.
| |
180 build_utils.MakeDirectory(os.path.dirname(output_v14_filename)) | |
181 with open(output_v14_filename, 'w') as f: | |
182 dom.writexml(f, '', ' ', '\n', encoding='utf-8') | |
183 | |
184 | |
185 def GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, output_v17_dir): | |
186 """Convert layout resources to API 14 compatible resources in input_dir.""" | |
166 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 187 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
167 rel_filename = os.path.relpath(input_filename, input_dir) | 188 rel_filename = os.path.relpath(input_filename, input_dir) |
168 output_v14_filename = os.path.join(output_v14_dir, rel_filename) | 189 output_v14_filename = os.path.join(output_v14_dir, rel_filename) |
169 output_v17_filename = os.path.join(output_v17_dir, rel_filename) | 190 output_v17_filename = os.path.join(output_v17_dir, rel_filename) |
170 GenerateV14Resource(input_filename, output_v14_filename, | 191 GenerateV14LayoutResource(input_filename, output_v14_filename, |
171 output_v17_filename) | 192 output_v17_filename) |
newt (away)
2013/05/28 21:08:13
align this argument
Kibeom Kim (inactive)
2013/05/28 23:15:32
Done.
| |
172 | 193 |
173 | 194 |
195 def GenerateV14StyleResourcesInDir(input_dir, output_v14_dir): | |
196 """Convert style resources to API 14 compatible resources in input_dir.""" | |
197 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | |
198 rel_filename = os.path.relpath(input_filename, input_dir) | |
199 output_v14_filename = os.path.join(output_v14_dir, rel_filename) | |
200 GenerateV14StyleResource(input_filename, output_v14_filename) | |
201 | |
202 | |
174 def ParseArgs(): | 203 def ParseArgs(): |
175 """Parses command line options. | 204 """Parses command line options. |
176 | 205 |
177 Returns: | 206 Returns: |
178 An options object as from optparse.OptionsParser.parse_args() | 207 An options object as from optparse.OptionsParser.parse_args() |
179 """ | 208 """ |
180 parser = optparse.OptionParser() | 209 parser = optparse.OptionParser() |
181 parser.add_option('--res-dir', | 210 parser.add_option('--res-dir', |
182 help='directory containing resources ' | 211 help='directory containing resources ' |
183 'used to generate v14 compatible resources') | 212 'used to generate v14 compatible resources') |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
215 if 'ldrtl' in qualifiers: | 244 if 'ldrtl' in qualifiers: |
216 continue | 245 continue |
217 | 246 |
218 # We also need to copy the original v17 resource to *-v17 directory | 247 # We also need to copy the original v17 resource to *-v17 directory |
219 # because the generated v14 resource will hide the original resource. | 248 # because the generated v14 resource will hide the original resource. |
220 input_dir = os.path.join(options.res_dir, name) | 249 input_dir = os.path.join(options.res_dir, name) |
221 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, name) | 250 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, name) |
222 output_v17_dir = os.path.join(options.res_v14_compatibility_dir, name + | 251 output_v17_dir = os.path.join(options.res_v14_compatibility_dir, name + |
223 '-v17') | 252 '-v17') |
224 | 253 |
225 # We only convert resources under layout*/, xml*/, | 254 # We only convert layout resources under layout*/, xml*/, |
226 # and style resources under values*/. | 255 # and style resources under values*/. |
227 # TODO(kkimlabs): don't process xml directly once all layouts have | 256 # TODO(kkimlabs): don't process xml directly once all layouts have |
228 # been moved out of XML directory. see http://crbug.com/238458 | 257 # been moved out of XML directory. see http://crbug.com/238458 |
229 if resource_type in ('layout', 'xml', 'values'): | 258 if resource_type in ('layout', 'xml'): |
230 GenerateV14XmlResourcesInDir(input_dir, output_v14_dir, output_v17_dir) | 259 GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, output_v17_dir) |
260 elif resource_type == 'values': | |
261 if len(qualifiers) > 0 and qualifiers[-1] == 'v17': | |
newt (away)
2013/05/28 21:08:13
if 'v17' in qualifiers: ...
also, I wouldn't depe
Kibeom Kim (inactive)
2013/05/28 23:15:32
I assumed v17 be the final because the versioning
| |
262 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, | |
263 '-'.join([resource_type] + | |
264 qualifiers[:-1])) | |
265 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) | |
266 else: | |
267 # TODO(kkimlabs): uncomment the below line and remove 'pass' once the | |
268 # downstream fix (https://gerrit-int.chromium.org/#/c/38786/) is landed. | |
269 # ErrorIfStyleResourceExistsInDir(input_dir) | |
270 pass | |
231 | 271 |
232 if options.stamp: | 272 if options.stamp: |
233 build_utils.Touch(options.stamp) | 273 build_utils.Touch(options.stamp) |
234 | 274 |
235 if __name__ == '__main__': | 275 if __name__ == '__main__': |
236 sys.exit(main(sys.argv)) | 276 sys.exit(main(sys.argv)) |
237 | 277 |
OLD | NEW |