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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 list(root_node.getElementsByTagName('style'))) | 91 list(root_node.getElementsByTagName('style'))) |
| 92 | 92 |
| 93 | 93 |
| 94 def ErrorIfStyleResourceExistsInDir(input_dir): | 94 def ErrorIfStyleResourceExistsInDir(input_dir): |
| 95 """If a style resource is in input_dir, exist with an error message.""" | 95 """If a style resource is in input_dir, exist with an error message.""" |
| 96 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 96 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 97 dom = minidom.parse(input_filename) | 97 dom = minidom.parse(input_filename) |
| 98 if HasStyleResource(dom): | 98 if HasStyleResource(dom): |
| 99 raise Exception('error: style file ' + input_filename + | 99 raise Exception('error: style file ' + input_filename + |
| 100 ' should be under ' + input_dir + | 100 ' should be under ' + input_dir + |
| 101 '-v17 directory. Please refer to crbug.com/243952 ' | 101 '-v17 directory. Please refer to ' |
| 102 'for the details.') | 102 'http://crbug.com/243952 for the details.') |
| 103 | 103 |
| 104 | 104 |
| 105 def GenerateV14LayoutResourceDom(dom, filename): | 105 def GenerateV14LayoutResourceDom(dom, filename_for_warning): |
| 106 """Convert layout resource to API 14 compatible layout resource. | 106 """Convert layout resource to API 14 compatible layout resource. |
| 107 | 107 |
| 108 Args: | 108 Args: |
| 109 dom: parsed minidom object to be modified. | 109 dom: parsed minidom object to be modified. |
| 110 filename: file name to display in case we print warnings. | 110 filename_for_warning: file name to display in case we print warnings. |
| 111 If None, do not print warning. | |
| 111 Returns: | 112 Returns: |
| 112 True if dom is modified, False otherwise. | 113 True if dom is modified, False otherwise. |
| 113 """ | 114 """ |
| 114 is_modified = False | 115 is_modified = False |
| 115 | 116 |
| 116 # Iterate all the elements' attributes to find attributes to convert. | 117 # Iterate all the elements' attributes to find attributes to convert. |
| 117 for element in IterateXmlElements(dom): | 118 for element in IterateXmlElements(dom): |
| 118 for name, value in list(element.attributes.items()): | 119 for name, value in list(element.attributes.items()): |
| 119 # Convert any API 17 Start/End attributes to Left/Right attributes. | 120 # Convert any API 17 Start/End attributes to Left/Right attributes. |
| 120 # For example, from paddingStart="10dp" to paddingLeft="10dp" | 121 # For example, from paddingStart="10dp" to paddingLeft="10dp" |
| 121 # Note: gravity attributes are not necessary to convert because | 122 # Note: gravity attributes are not necessary to convert because |
| 122 # start/end values are backward-compatible. Explained at | 123 # start/end values are backward-compatible. Explained at |
| 123 # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom | 124 # https://plus.sandbox.google.com/+RomanNurik/posts/huuJd8iVVXY?e=Showroom |
| 124 if name in ATTRIBUTES_TO_MAP: | 125 if name in ATTRIBUTES_TO_MAP: |
| 125 element.setAttribute(ATTRIBUTES_TO_MAP[name], value) | 126 element.setAttribute(ATTRIBUTES_TO_MAP[name], value) |
| 126 del element.attributes[name] | 127 del element.attributes[name] |
| 127 is_modified = True | 128 is_modified = True |
| 128 else: | 129 elif filename_for_warning: |
| 129 WarnIfDeprecatedAttribute(name, value, filename) | 130 WarnIfDeprecatedAttribute(name, value, filename_for_warning) |
| 130 | 131 |
| 131 return is_modified | 132 return is_modified |
| 132 | 133 |
| 133 | 134 |
| 134 def GenerateV14StyleResourceDom(dom, filename): | 135 def GenerateV14StyleResourceDom(dom, filename_for_warning): |
| 135 """Convert style resource to API 14 compatible style resource. | 136 """Convert style resource to API 14 compatible style resource. |
| 136 | 137 |
| 137 Args: | 138 Args: |
| 138 dom: parsed minidom object to be modified. | 139 dom: parsed minidom object to be modified. |
| 139 filename: file name to display in case we print warnings. | 140 filename_for_warning: file name to display in case we print warnings. |
| 141 If None, do not print warning. | |
| 142 Returns: | |
| 143 True if dom is modified, False otherwise. | |
| 140 """ | 144 """ |
| 145 is_modified = False | |
| 146 | |
| 141 for style_element in dom.getElementsByTagName('style'): | 147 for style_element in dom.getElementsByTagName('style'): |
| 142 for item_element in style_element.getElementsByTagName('item'): | 148 for item_element in style_element.getElementsByTagName('item'): |
| 143 name = item_element.attributes['name'].value | 149 name = item_element.attributes['name'].value |
| 144 value = item_element.childNodes[0].nodeValue | 150 value = item_element.childNodes[0].nodeValue |
| 145 if name in ATTRIBUTES_TO_MAP: | 151 if name in ATTRIBUTES_TO_MAP: |
| 146 item_element.attributes['name'].value = ATTRIBUTES_TO_MAP[name] | 152 item_element.attributes['name'].value = ATTRIBUTES_TO_MAP[name] |
| 147 else: | 153 is_modified = True |
| 148 WarnIfDeprecatedAttribute(name, value, filename) | 154 elif filename_for_warning: |
| 155 WarnIfDeprecatedAttribute(name, value, filename_for_warning) | |
| 149 | 156 |
| 157 return is_modified | |
| 150 | 158 |
| 151 def GenerateV14LayoutResource(input_filename, output_v14_filename, | 159 def GenerateV14LayoutResource(input_filename, output_v14_filename, |
| 152 output_v17_filename): | 160 output_v17_filename): |
| 153 """Convert API 17 layout resource to API 14 compatible layout resource. | 161 """Convert API 17 layout resource to API 14 compatible layout resource. |
| 154 | 162 |
| 155 It's mostly a simple replacement, s/Start/Left s/End/Right, | 163 It's mostly a simple replacement, s/Start/Left s/End/Right, |
| 156 on the attribute names. | 164 on the attribute names. |
| 157 If the generated resource is identical to the original resource, | 165 If the generated resource is identical to the original resource, |
| 158 don't do anything. If not, write the generated resource to | 166 don't do anything. If not, write the generated resource to |
| 159 output_v14_filename, and copy the original resource to output_v17_filename. | 167 output_v14_filename, and copy the original resource to output_v17_filename. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 185 | 193 |
| 186 | 194 |
| 187 def GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, output_v17_dir): | 195 def GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, output_v17_dir): |
| 188 """Convert layout resources to API 14 compatible resources in input_dir.""" | 196 """Convert layout resources to API 14 compatible resources in input_dir.""" |
| 189 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 197 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 190 rel_filename = os.path.relpath(input_filename, input_dir) | 198 rel_filename = os.path.relpath(input_filename, input_dir) |
| 191 output_v14_filename = os.path.join(output_v14_dir, rel_filename) | 199 output_v14_filename = os.path.join(output_v14_dir, rel_filename) |
| 192 output_v17_filename = os.path.join(output_v17_dir, rel_filename) | 200 output_v17_filename = os.path.join(output_v17_dir, rel_filename) |
| 193 GenerateV14LayoutResource(input_filename, output_v14_filename, | 201 GenerateV14LayoutResource(input_filename, output_v14_filename, |
| 194 output_v17_filename) | 202 output_v17_filename) |
| 195 | 203 |
|
newt (away)
2013/07/09 00:52:49
python style is two newlines between top-level met
Kibeom Kim (inactive)
2013/07/09 02:10:21
Done.
| |
| 196 | |
| 197 def GenerateV14StyleResourcesInDir(input_dir, output_v14_dir): | 204 def GenerateV14StyleResourcesInDir(input_dir, output_v14_dir): |
| 198 """Convert style resources to API 14 compatible resources in input_dir.""" | 205 """Convert style resources to API 14 compatible resources in input_dir.""" |
| 199 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 206 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 200 rel_filename = os.path.relpath(input_filename, input_dir) | 207 rel_filename = os.path.relpath(input_filename, input_dir) |
| 201 output_v14_filename = os.path.join(output_v14_dir, rel_filename) | 208 output_v14_filename = os.path.join(output_v14_dir, rel_filename) |
| 202 GenerateV14StyleResource(input_filename, output_v14_filename) | 209 GenerateV14StyleResource(input_filename, output_v14_filename) |
| 203 | 210 |
| 211 def VerifyV14ResourcesInDir(input_dir, dom_handler): | |
| 212 """Verify that the resources in input_dir is compatible with v14, i.e., they | |
| 213 don't use attributes that cause crashes on certain devices. Print an error if | |
| 214 they have. | |
| 215 | |
| 216 Args: | |
| 217 input_dir: directory to inspect. | |
| 218 dom_handler: either GenerateV14LayoutResourcesInDir or | |
| 219 GenerateV14StyleResourcesInDir, depending on the resource | |
| 220 type in input_dir. | |
| 221 """ | |
| 222 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | |
| 223 dom = minidom.parse(input_filename) | |
| 224 if (dom_handler(dom, None)): | |
| 225 raise Exception('error : ' + input_filename + | |
| 226 ' has an RTL attribute, i.e., attribute that has "start"' | |
| 227 'or "end" in its name. Pre-v17 resources should not ' | |
| 228 'include it because it can cause crashes on certain ' | |
| 229 'devices. Please refer to http://crbug.com/243952 ' | |
| 230 'for the details.') | |
| 231 | |
| 232 def WarnIfDeprecatedAttributeInDir(input_dir, dom_handler): | |
| 233 """Print warning if resources in input_dir have deprecated attributes, e.g., | |
| 234 paddingLeft, PaddingRight | |
| 235 | |
| 236 Args: | |
| 237 input_dir: directory to inspect. | |
| 238 dom_handler: either GenerateV14LayoutResourcesInDir or | |
| 239 GenerateV14StyleResourcesInDir, depending on the resource | |
| 240 type in input_dir. | |
| 241 """ | |
| 242 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | |
| 243 dom = minidom.parse(input_filename) | |
| 244 dom_handler(dom, input_filename) | |
| 204 | 245 |
| 205 def ParseArgs(): | 246 def ParseArgs(): |
| 206 """Parses command line options. | 247 """Parses command line options. |
| 207 | 248 |
| 208 Returns: | 249 Returns: |
| 209 An options object as from optparse.OptionsParser.parse_args() | 250 An options object as from optparse.OptionsParser.parse_args() |
| 210 """ | 251 """ |
| 211 parser = optparse.OptionParser() | 252 parser = optparse.OptionParser() |
| 212 parser.add_option('--res-dir', | 253 parser.add_option('--res-dir', |
| 213 help='directory containing resources ' | 254 help='directory containing resources ' |
| 214 'used to generate v14 compatible resources') | 255 'used to generate v14 compatible resources') |
| 215 parser.add_option('--res-v14-compatibility-dir', | 256 parser.add_option('--res-v14-compatibility-dir', |
| 216 help='output directory into which ' | 257 help='output directory into which ' |
| 217 'v14 compatible resources will be generated') | 258 'v14 compatible resources will be generated') |
| 218 parser.add_option('--stamp', help='File to touch on success') | 259 parser.add_option('--stamp', help='File to touch on success') |
| 260 parser.add_option('--verify-only', action="store_true", help='Do not generate' | |
| 261 ' v14 resources. Instead, just verify that the resources are already ' | |
| 262 "compatible with v14, i.e. they don't use attributes that cause crashes " | |
| 263 'on certain devices.') | |
| 219 | 264 |
| 220 options, args = parser.parse_args() | 265 options, args = parser.parse_args() |
| 221 | 266 |
| 222 if args: | 267 if args: |
| 223 parser.error('No positional arguments should be given.') | 268 parser.error('No positional arguments should be given.') |
| 224 | 269 |
| 225 # Check that required options have been provided. | 270 # Check that required options have been provided. |
| 226 required_options = ('res_dir', 'res_v14_compatibility_dir') | 271 required_options = ('res_dir', 'res_v14_compatibility_dir') |
| 227 build_utils.CheckOptions(options, parser, required=required_options) | 272 build_utils.CheckOptions(options, parser, required=required_options) |
| 228 return options | 273 return options |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 247 for index, qualifier in enumerate(qualifiers): | 292 for index, qualifier in enumerate(qualifiers): |
| 248 if re.match('v[0-9]+$', qualifier): | 293 if re.match('v[0-9]+$', qualifier): |
| 249 api_level_qualifier_index = index | 294 api_level_qualifier_index = index |
| 250 api_level_qualifier = qualifier | 295 api_level_qualifier = qualifier |
| 251 break | 296 break |
| 252 | 297 |
| 253 # Android pre-v17 API doesn't support RTL. Skip. | 298 # Android pre-v17 API doesn't support RTL. Skip. |
| 254 if 'ldrtl' in qualifiers: | 299 if 'ldrtl' in qualifiers: |
| 255 continue | 300 continue |
| 256 | 301 |
| 257 # We also need to copy the original v17 resource to *-v17 directory | 302 input_dir = os.path.abspath(os.path.join(options.res_dir, name)) |
| 258 # because the generated v14 resource will hide the original resource. | |
| 259 input_dir = os.path.join(options.res_dir, name) | |
| 260 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, name) | |
| 261 output_v17_dir = os.path.join(options.res_v14_compatibility_dir, name + | |
| 262 '-v17') | |
| 263 | 303 |
| 264 # We only convert layout resources under layout*/, xml*/, | 304 if options.verify_only: |
| 265 # and style resources under values*/. | 305 if resource_type in ('layout', 'xml'): |
|
newt (away)
2013/07/09 00:52:49
this logic (lines 305-310) probably belongs inside
Kibeom Kim (inactive)
2013/07/09 02:10:21
Done.
| |
| 266 if resource_type in ('layout', 'xml'): | 306 dom_parser = GenerateV14LayoutResourceDom |
| 267 if not api_level_qualifier: | 307 elif resource_type == 'values': |
| 268 GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, | 308 dom_parser = GenerateV14StyleResourceDom |
| 269 output_v17_dir) | 309 else: |
| 270 elif resource_type == 'values': | 310 continue |
| 271 if api_level_qualifier == 'v17': | 311 |
| 272 output_qualifiers = qualifiers[:] | 312 if not api_level_qualifier or int(api_level_qualifier[1:]) < 17: |
| 273 del output_qualifiers[api_level_qualifier_index] | 313 VerifyV14ResourcesInDir(input_dir, dom_parser) |
| 274 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, | 314 else: |
| 275 '-'.join([resource_type] + | 315 WarnIfDeprecatedAttributeInDir(input_dir, dom_parser) |
| 276 output_qualifiers)) | 316 else: |
| 277 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) | 317 # We also need to copy the original v17 resource to *-v17 directory |
| 278 elif not api_level_qualifier: | 318 # because the generated v14 resource will hide the original resource. |
| 279 ErrorIfStyleResourceExistsInDir(input_dir) | 319 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, name) |
| 320 output_v17_dir = os.path.join(options.res_v14_compatibility_dir, name + | |
| 321 '-v17') | |
| 322 | |
| 323 # We only convert layout resources under layout*/, xml*/, | |
| 324 # and style resources under values*/. | |
| 325 if resource_type in ('layout', 'xml'): | |
| 326 if not api_level_qualifier: | |
| 327 GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, | |
| 328 output_v17_dir) | |
| 329 elif resource_type == 'values': | |
| 330 if api_level_qualifier == 'v17': | |
| 331 output_qualifiers = qualifiers[:] | |
| 332 del output_qualifiers[api_level_qualifier_index] | |
| 333 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, | |
| 334 '-'.join([resource_type] + | |
| 335 output_qualifiers)) | |
| 336 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) | |
| 337 elif not api_level_qualifier: | |
| 338 ErrorIfStyleResourceExistsInDir(input_dir) | |
| 280 | 339 |
| 281 if options.stamp: | 340 if options.stamp: |
| 282 build_utils.Touch(options.stamp) | 341 build_utils.Touch(options.stamp) |
| 283 | 342 |
| 284 if __name__ == '__main__': | 343 if __name__ == '__main__': |
| 285 sys.exit(main(sys.argv)) | 344 sys.exit(main(sys.argv)) |
| 286 | 345 |
| OLD | NEW |