| 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) |
| 156 |
| 157 return is_modified |
| 149 | 158 |
| 150 | 159 |
| 151 def GenerateV14LayoutResource(input_filename, output_v14_filename, | 160 def GenerateV14LayoutResource(input_filename, output_v14_filename, |
| 152 output_v17_filename): | 161 output_v17_filename): |
| 153 """Convert API 17 layout resource to API 14 compatible layout resource. | 162 """Convert API 17 layout resource to API 14 compatible layout resource. |
| 154 | 163 |
| 155 It's mostly a simple replacement, s/Start/Left s/End/Right, | 164 It's mostly a simple replacement, s/Start/Left s/End/Right, |
| 156 on the attribute names. | 165 on the attribute names. |
| 157 If the generated resource is identical to the original resource, | 166 If the generated resource is identical to the original resource, |
| 158 don't do anything. If not, write the generated resource to | 167 don't do anything. If not, write the generated resource to |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 204 |
| 196 | 205 |
| 197 def GenerateV14StyleResourcesInDir(input_dir, output_v14_dir): | 206 def GenerateV14StyleResourcesInDir(input_dir, output_v14_dir): |
| 198 """Convert style resources to API 14 compatible resources in input_dir.""" | 207 """Convert style resources to API 14 compatible resources in input_dir.""" |
| 199 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): | 208 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 200 rel_filename = os.path.relpath(input_filename, input_dir) | 209 rel_filename = os.path.relpath(input_filename, input_dir) |
| 201 output_v14_filename = os.path.join(output_v14_dir, rel_filename) | 210 output_v14_filename = os.path.join(output_v14_dir, rel_filename) |
| 202 GenerateV14StyleResource(input_filename, output_v14_filename) | 211 GenerateV14StyleResource(input_filename, output_v14_filename) |
| 203 | 212 |
| 204 | 213 |
| 214 def VerifyV14ResourcesInDir(input_dir, resource_type): |
| 215 """Verify that the resources in input_dir is compatible with v14, i.e., they |
| 216 don't use attributes that cause crashes on certain devices. Print an error if |
| 217 they have.""" |
| 218 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 219 exception_message = ('error : ' + input_filename + ' has an RTL attribute, ' |
| 220 'i.e., attribute that has "start" or "end" in its name.' |
| 221 ' Pre-v17 resources should not include it because it ' |
| 222 'can cause crashes on certain devices. Please refer to ' |
| 223 'http://crbug.com/243952 for the details.') |
| 224 dom = minidom.parse(input_filename) |
| 225 if resource_type in ('layout', 'xml'): |
| 226 if GenerateV14LayoutResourceDom(dom, None): |
| 227 raise Exception(exception_message) |
| 228 elif resource_type == 'values': |
| 229 if GenerateV14StyleResourceDom(dom, None): |
| 230 raise Exception(exception_message) |
| 231 |
| 232 |
| 233 def WarnIfDeprecatedAttributeInDir(input_dir, resource_type): |
| 234 """Print warning if resources in input_dir have deprecated attributes, e.g., |
| 235 paddingLeft, PaddingRight""" |
| 236 for input_filename in build_utils.FindInDirectory(input_dir, '*.xml'): |
| 237 dom = minidom.parse(input_filename) |
| 238 if resource_type in ('layout', 'xml'): |
| 239 GenerateV14LayoutResourceDom(dom, input_filename) |
| 240 elif resource_type == 'values': |
| 241 GenerateV14StyleResourceDom(dom, input_filename) |
| 242 |
| 243 |
| 205 def ParseArgs(): | 244 def ParseArgs(): |
| 206 """Parses command line options. | 245 """Parses command line options. |
| 207 | 246 |
| 208 Returns: | 247 Returns: |
| 209 An options object as from optparse.OptionsParser.parse_args() | 248 An options object as from optparse.OptionsParser.parse_args() |
| 210 """ | 249 """ |
| 211 parser = optparse.OptionParser() | 250 parser = optparse.OptionParser() |
| 212 parser.add_option('--res-dir', | 251 parser.add_option('--res-dir', |
| 213 help='directory containing resources ' | 252 help='directory containing resources ' |
| 214 'used to generate v14 compatible resources') | 253 'used to generate v14 compatible resources') |
| 215 parser.add_option('--res-v14-compatibility-dir', | 254 parser.add_option('--res-v14-compatibility-dir', |
| 216 help='output directory into which ' | 255 help='output directory into which ' |
| 217 'v14 compatible resources will be generated') | 256 'v14 compatible resources will be generated') |
| 218 parser.add_option('--stamp', help='File to touch on success') | 257 parser.add_option('--stamp', help='File to touch on success') |
| 258 parser.add_option('--verify-only', action="store_true", help='Do not generate' |
| 259 ' v14 resources. Instead, just verify that the resources are already ' |
| 260 "compatible with v14, i.e. they don't use attributes that cause crashes " |
| 261 'on certain devices.') |
| 219 | 262 |
| 220 options, args = parser.parse_args() | 263 options, args = parser.parse_args() |
| 221 | 264 |
| 222 if args: | 265 if args: |
| 223 parser.error('No positional arguments should be given.') | 266 parser.error('No positional arguments should be given.') |
| 224 | 267 |
| 225 # Check that required options have been provided. | 268 # Check that required options have been provided. |
| 226 required_options = ('res_dir', 'res_v14_compatibility_dir') | 269 required_options = ('res_dir', 'res_v14_compatibility_dir') |
| 227 build_utils.CheckOptions(options, parser, required=required_options) | 270 build_utils.CheckOptions(options, parser, required=required_options) |
| 228 return options | 271 return options |
| (...skipping 18 matching lines...) Expand all Loading... |
| 247 for index, qualifier in enumerate(qualifiers): | 290 for index, qualifier in enumerate(qualifiers): |
| 248 if re.match('v[0-9]+$', qualifier): | 291 if re.match('v[0-9]+$', qualifier): |
| 249 api_level_qualifier_index = index | 292 api_level_qualifier_index = index |
| 250 api_level_qualifier = qualifier | 293 api_level_qualifier = qualifier |
| 251 break | 294 break |
| 252 | 295 |
| 253 # Android pre-v17 API doesn't support RTL. Skip. | 296 # Android pre-v17 API doesn't support RTL. Skip. |
| 254 if 'ldrtl' in qualifiers: | 297 if 'ldrtl' in qualifiers: |
| 255 continue | 298 continue |
| 256 | 299 |
| 257 # We also need to copy the original v17 resource to *-v17 directory | 300 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 | 301 |
| 264 # We only convert layout resources under layout*/, xml*/, | 302 if options.verify_only: |
| 265 # and style resources under values*/. | 303 if not api_level_qualifier or int(api_level_qualifier[1:]) < 17: |
| 266 if resource_type in ('layout', 'xml'): | 304 VerifyV14ResourcesInDir(input_dir, resource_type) |
| 267 if not api_level_qualifier: | 305 else: |
| 268 GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, | 306 WarnIfDeprecatedAttributeInDir(input_dir, resource_type) |
| 269 output_v17_dir) | 307 else: |
| 270 elif resource_type == 'values': | 308 # We also need to copy the original v17 resource to *-v17 directory |
| 271 if api_level_qualifier == 'v17': | 309 # because the generated v14 resource will hide the original resource. |
| 272 output_qualifiers = qualifiers[:] | 310 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, name) |
| 273 del output_qualifiers[api_level_qualifier_index] | 311 output_v17_dir = os.path.join(options.res_v14_compatibility_dir, name + |
| 274 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, | 312 '-v17') |
| 275 '-'.join([resource_type] + | 313 |
| 276 output_qualifiers)) | 314 # We only convert layout resources under layout*/, xml*/, |
| 277 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) | 315 # and style resources under values*/. |
| 278 elif not api_level_qualifier: | 316 if resource_type in ('layout', 'xml'): |
| 279 ErrorIfStyleResourceExistsInDir(input_dir) | 317 if not api_level_qualifier: |
| 318 GenerateV14LayoutResourcesInDir(input_dir, output_v14_dir, |
| 319 output_v17_dir) |
| 320 elif resource_type == 'values': |
| 321 if api_level_qualifier == 'v17': |
| 322 output_qualifiers = qualifiers[:] |
| 323 del output_qualifiers[api_level_qualifier_index] |
| 324 output_v14_dir = os.path.join(options.res_v14_compatibility_dir, |
| 325 '-'.join([resource_type] + |
| 326 output_qualifiers)) |
| 327 GenerateV14StyleResourcesInDir(input_dir, output_v14_dir) |
| 328 elif not api_level_qualifier: |
| 329 ErrorIfStyleResourceExistsInDir(input_dir) |
| 280 | 330 |
| 281 if options.stamp: | 331 if options.stamp: |
| 282 build_utils.Touch(options.stamp) | 332 build_utils.Touch(options.stamp) |
| 283 | 333 |
| 284 if __name__ == '__main__': | 334 if __name__ == '__main__': |
| 285 sys.exit(main(sys.argv)) | 335 sys.exit(main(sys.argv)) |
| 286 | 336 |
| OLD | NEW |