Chromium Code Reviews| 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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 def IterateXmlElements(node): | 58 def IterateXmlElements(node): |
| 59 """minidom helper function that iterates all the element nodes. | 59 """minidom helper function that iterates all the element nodes. |
| 60 Iteration order is pre-order depth-first.""" | 60 Iteration order is pre-order depth-first.""" |
| 61 if node.nodeType == node.ELEMENT_NODE: | 61 if node.nodeType == node.ELEMENT_NODE: |
| 62 yield node | 62 yield node |
| 63 for child_node in node.childNodes: | 63 for child_node in node.childNodes: |
| 64 for child_node_element in IterateXmlElements(child_node): | 64 for child_node_element in IterateXmlElements(child_node): |
| 65 yield child_node_element | 65 yield child_node_element |
| 66 | 66 |
| 67 | 67 |
| 68 def ParseAndReportErrors(filename): | |
| 69 try: | |
| 70 return minidom.parse(filename) | |
| 71 except Exception: | |
| 72 import traceback | |
| 73 traceback.print_exc() | |
|
Kibeom Kim (inactive)
2014/10/30 07:07:39
Just a question: it looks this is a common practic
Kibeom Kim (inactive)
2014/10/30 07:10:44
Though, having the most important message at the l
| |
| 74 sys.stderr.write('Failed to parse XML file: %s\n' % filename) | |
| 75 sys.exit(1) | |
| 76 | |
| 77 | |
| 68 def AssertNotDeprecatedAttribute(name, value, filename): | 78 def AssertNotDeprecatedAttribute(name, value, filename): |
| 69 """Raises an exception if the given attribute is deprecated.""" | 79 """Raises an exception if the given attribute is deprecated.""" |
| 70 msg = None | 80 msg = None |
| 71 if name in ATTRIBUTES_TO_MAP_REVERSED: | 81 if name in ATTRIBUTES_TO_MAP_REVERSED: |
| 72 msg = '{0} should use {1} instead of {2}'.format(filename, | 82 msg = '{0} should use {1} instead of {2}'.format(filename, |
| 73 ATTRIBUTES_TO_MAP_REVERSED[name], name) | 83 ATTRIBUTES_TO_MAP_REVERSED[name], name) |
| 74 elif name in GRAVITY_ATTRIBUTES and ('left' in value or 'right' in value): | 84 elif name in GRAVITY_ATTRIBUTES and ('left' in value or 'right' in value): |
| 75 msg = '{0} should use start/end instead of left/right for {1}'.format( | 85 msg = '{0} should use start/end instead of left/right for {1}'.format( |
| 76 filename, name) | 86 filename, name) |
| 77 | 87 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 93 def HasStyleResource(dom): | 103 def HasStyleResource(dom): |
| 94 """Return True if the dom is a style resource, False otherwise.""" | 104 """Return True if the dom is a style resource, False otherwise.""" |
| 95 root_node = IterateXmlElements(dom).next() | 105 root_node = IterateXmlElements(dom).next() |
| 96 return bool(root_node.nodeName == 'resources' and | 106 return bool(root_node.nodeName == 'resources' and |
| 97 list(root_node.getElementsByTagName('style'))) | 107 list(root_node.getElementsByTagName('style'))) |
| 98 | 108 |
| 99 | 109 |
| 100 def ErrorIfStyleResourceExistsInDir(input_dir): | 110 def ErrorIfStyleResourceExistsInDir(input_dir): |
| 101 """If a style resource is in input_dir, raises an exception.""" | 111 """If a style resource is in input_dir, raises an exception.""" |
| 102 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 112 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 103 dom = minidom.parse(input_filename) | 113 dom = ParseAndReportErrors(input_filename) |
| 104 if HasStyleResource(dom): | 114 if HasStyleResource(dom): |
| 105 raise Exception('error: style file ' + input_filename + | 115 raise Exception('error: style file ' + input_filename + |
| 106 ' should be under ' + input_dir + | 116 ' should be under ' + input_dir + |
| 107 '-v17 directory. Please refer to ' | 117 '-v17 directory. Please refer to ' |
| 108 'http://crbug.com/243952 for the details.') | 118 'http://crbug.com/243952 for the details.') |
| 109 | 119 |
| 110 | 120 |
| 111 def GenerateV14LayoutResourceDom(dom, filename, assert_not_deprecated=True): | 121 def GenerateV14LayoutResourceDom(dom, filename, assert_not_deprecated=True): |
| 112 """Convert layout resource to API 14 compatible layout resource. | 122 """Convert layout resource to API 14 compatible layout resource. |
| 113 | 123 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 170 def GenerateV14LayoutResource(input_filename, output_v14_filename, | 180 def GenerateV14LayoutResource(input_filename, output_v14_filename, |
| 171 output_v17_filename): | 181 output_v17_filename): |
| 172 """Convert API 17 layout resource to API 14 compatible layout resource. | 182 """Convert API 17 layout resource to API 14 compatible layout resource. |
| 173 | 183 |
| 174 It's mostly a simple replacement, s/Start/Left s/End/Right, | 184 It's mostly a simple replacement, s/Start/Left s/End/Right, |
| 175 on the attribute names. | 185 on the attribute names. |
| 176 If the generated resource is identical to the original resource, | 186 If the generated resource is identical to the original resource, |
| 177 don't do anything. If not, write the generated resource to | 187 don't do anything. If not, write the generated resource to |
| 178 output_v14_filename, and copy the original resource to output_v17_filename. | 188 output_v14_filename, and copy the original resource to output_v17_filename. |
| 179 """ | 189 """ |
| 180 dom = minidom.parse(input_filename) | 190 dom = ParseAndReportErrors(input_filename) |
| 181 is_modified = GenerateV14LayoutResourceDom(dom, input_filename) | 191 is_modified = GenerateV14LayoutResourceDom(dom, input_filename) |
| 182 | 192 |
| 183 if is_modified: | 193 if is_modified: |
| 184 # Write the generated resource. | 194 # Write the generated resource. |
| 185 WriteDomToFile(dom, output_v14_filename) | 195 WriteDomToFile(dom, output_v14_filename) |
| 186 | 196 |
| 187 # Copy the original resource. | 197 # Copy the original resource. |
| 188 build_utils.MakeDirectory(os.path.dirname(output_v17_filename)) | 198 build_utils.MakeDirectory(os.path.dirname(output_v17_filename)) |
| 189 shutil.copy2(input_filename, output_v17_filename) | 199 shutil.copy2(input_filename, output_v17_filename) |
| 190 | 200 |
| 191 | 201 |
| 192 def GenerateV14StyleResource(input_filename, output_v14_filename): | 202 def GenerateV14StyleResource(input_filename, output_v14_filename): |
| 193 """Convert API 17 style resources to API 14 compatible style resource. | 203 """Convert API 17 style resources to API 14 compatible style resource. |
| 194 | 204 |
| 195 Write the generated style resource to output_v14_filename. | 205 Write the generated style resource to output_v14_filename. |
| 196 It's mostly a simple replacement, s/Start/Left s/End/Right, | 206 It's mostly a simple replacement, s/Start/Left s/End/Right, |
| 197 on the attribute names. | 207 on the attribute names. |
| 198 """ | 208 """ |
| 199 dom = minidom.parse(input_filename) | 209 dom = ParseAndReportErrors(input_filename) |
| 200 GenerateV14StyleResourceDom(dom, input_filename) | 210 GenerateV14StyleResourceDom(dom, input_filename) |
| 201 | 211 |
| 202 # Write the generated resource. | 212 # Write the generated resource. |
| 203 WriteDomToFile(dom, output_v14_filename) | 213 WriteDomToFile(dom, output_v14_filename) |
| 204 | 214 |
| 205 | 215 |
| 206 def GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, output_v17_dir): | 216 def GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, output_v17_dir): |
| 207 """Convert layout resources to API 14 compatible resources in input_dir.""" | 217 """Convert layout resources to API 14 compatible resources in input_dir.""" |
| 208 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 218 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 209 rel_filename = os.path.relpath(input_filename, input_dir) | 219 rel_filename = os.path.relpath(input_filename, input_dir) |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 224 def VerifyV14ResourcesInDir(input_dir, resource_type): | 234 def VerifyV14ResourcesInDir(input_dir, resource_type): |
| 225 """Verify that the resources in input_dir is compatible with v14, i.e., they | 235 """Verify that the resources in input_dir is compatible with v14, i.e., they |
| 226 don't use attributes that cause crashes on certain devices. Print an error if | 236 don't use attributes that cause crashes on certain devices. Print an error if |
| 227 they have.""" | 237 they have.""" |
| 228 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 238 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 229 exception_message = ('error : ' + input_filename + ' has an RTL attribute, ' | 239 exception_message = ('error : ' + input_filename + ' has an RTL attribute, ' |
| 230 'i.e., attribute that has "start" or "end" in its name.' | 240 'i.e., attribute that has "start" or "end" in its name.' |
| 231 ' Pre-v17 resources should not include it because it ' | 241 ' Pre-v17 resources should not include it because it ' |
| 232 'can cause crashes on certain devices. Please refer to ' | 242 'can cause crashes on certain devices. Please refer to ' |
| 233 'http://crbug.com/243952 for the details.') | 243 'http://crbug.com/243952 for the details.') |
| 234 dom = minidom.parse(input_filename) | 244 dom = ParseAndReportErrors(input_filename) |
| 235 if resource_type in ('layout', 'xml'): | 245 if resource_type in ('layout', 'xml'): |
| 236 if GenerateV14LayoutResourceDom(dom, input_filename, False): | 246 if GenerateV14LayoutResourceDom(dom, input_filename, False): |
| 237 raise Exception(exception_message) | 247 raise Exception(exception_message) |
| 238 elif resource_type == 'values': | 248 elif resource_type == 'values': |
| 239 if GenerateV14StyleResourceDom(dom, input_filename, False): | 249 if GenerateV14StyleResourceDom(dom, input_filename, False): |
| 240 raise Exception(exception_message) | 250 raise Exception(exception_message) |
| 241 | 251 |
| 242 | 252 |
| 243 def AssertNoDeprecatedAttributesInDir(input_dir, resource_type): | 253 def AssertNoDeprecatedAttributesInDir(input_dir, resource_type): |
| 244 """Raises an exception if resources in input_dir have deprecated attributes, | 254 """Raises an exception if resources in input_dir have deprecated attributes, |
| 245 e.g., paddingLeft, paddingRight""" | 255 e.g., paddingLeft, paddingRight""" |
| 246 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 256 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 247 dom = minidom.parse(input_filename) | 257 dom = ParseAndReportErrors(input_filename) |
| 248 if resource_type in ('layout', 'xml'): | 258 if resource_type in ('layout', 'xml'): |
| 249 GenerateV14LayoutResourceDom(dom, input_filename) | 259 GenerateV14LayoutResourceDom(dom, input_filename) |
| 250 elif resource_type == 'values': | 260 elif resource_type == 'values': |
| 251 GenerateV14StyleResourceDom(dom, input_filename) | 261 GenerateV14StyleResourceDom(dom, input_filename) |
| 252 | 262 |
| 253 | 263 |
| 254 def ParseArgs(): | 264 def ParseArgs(): |
| 255 """Parses command line options. | 265 """Parses command line options. |
| 256 | 266 |
| 257 Returns: | 267 Returns: |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 340 build_utils.MakeDirectory(res_v14_dir) | 350 build_utils.MakeDirectory(res_v14_dir) |
| 341 | 351 |
| 342 GenerateV14Resources(options.res_dir, res_v14_dir, options.verify_only) | 352 GenerateV14Resources(options.res_dir, res_v14_dir, options.verify_only) |
| 343 | 353 |
| 344 if options.stamp: | 354 if options.stamp: |
| 345 build_utils.Touch(options.stamp) | 355 build_utils.Touch(options.stamp) |
| 346 | 356 |
| 347 if __name__ == '__main__': | 357 if __name__ == '__main__': |
| 348 sys.exit(main()) | 358 sys.exit(main()) |
| 349 | 359 |
| OLD | NEW |