Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(943)

Side by Side Diff: build/android/gyp/generate_v14_resources.py

Issue 14476011: [Android] Auto-generate API 14 resources from the existing API 17 resources. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added copying script action. Pretty much complete I think Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.etree.cElementTree as ET
12 import xml.dom.minidom as minidom
13
14 from util import build_utils
15
16
17 ATTRIBUTE_NAMESPACE = u'http://schemas.android.com/apk/res/android'
18
19 # The list of the attributes were taken from go/android-rtl
20 ATTRIBUTES_TO_MAP = {'paddingStart' : 'paddingLeft',
21 'drawableStart' : 'drawableLeft',
22 'layout_alignStart' : 'layout_alignLeft',
23 'layout_marginStart' : 'layout_marginLeft',
24 'layout_alignParentStart' : 'layout_alignParentLeft',
25 'layout_toStartOf' : 'layout_toLeftOf',
26 'paddingEnd' : 'paddingRight',
27 'drawableEnd' : 'drawableRight',
28 'layout_alignEnd' : 'layout_alignRight',
29 'layout_marginEnd' : 'layout_marginRight',
30 'layout_alignParentEnd' : 'layout_alignParentRight',
31 'layout_toEndOf' : 'layout_toRightOf'}
32
33 ATTRIBUTES_TO_MAP = {(ATTRIBUTE_NAMESPACE, k) : (ATTRIBUTE_NAMESPACE , v)
34 for k, v in ATTRIBUTES_TO_MAP.items()}
35
36 GRAVITY_ATTRIBUTES = ('gravity', 'layout_gravity')
37
38 GRAVITY_ATTRIBUTES = [(ATTRIBUTE_NAMESPACE, i) for i in GRAVITY_ATTRIBUTES]
39
40
41 def iterate_xml_elements(node):
42 """minidom helper function that iterates all the element nodes. Depth-first.
43 """
44 if node.nodeType == node.ELEMENT_NODE:
45 yield node
46 for child_node in node.childNodes:
47 for child_node_element in iterate_xml_elements(child_node):
48 yield child_node_element
49
50
51 def generate_pre_v17_resource(input_filename, output_filename):
52 """Convert resource to API 14 compatible resource.
53
54 It's mostly a simple replacement, s/Start/Left s/End/Right,
55 on the attribute names.
56 """
57 dom = minidom.parse(input_filename)
58
59 for element in iterate_xml_elements(dom):
60 all_names = element.attributes.keysNS()
61
62 # Iterate all the attributes to find attributes to convert.
63 # Note that name variable is actually a tuple that has namespace and name.
64 # For example,
65 # name == ('http://schemas.android.com/apk/res/android', 'paddingStart')
66 for name, value in list(element.attributes.itemsNS()):
67 # For gravity attributes, replace start/end to left/right.
68 # For example, gravity="start" to gravity="end"
69 # This is actually not necessary because start/end is backward-compatible.
70 if name in GRAVITY_ATTRIBUTES:
71 replaced_value = re.sub('start|end',
72 lambda x: 'left' if x.group(0) == 'start' else 'right', value)
73 element.setAttributeNS(name[0], name[1], replaced_value)
74 # Convert any other API 17 Start/End attributes to Left/Right attributes.
75 # For example, from paddingStart="10dp" to paddingLeft="10dp"
76 elif name in ATTRIBUTES_TO_MAP.keys():
77 mapped_name = ATTRIBUTES_TO_MAP[name]
78
79 # If, for example, paddingLeft and paddingStart are used
80 # but have different values, then it is definitely not right.
81 if mapped_name in all_names:
82 if value != element.getAttributeNS(*mapped_name):
83 error_message = '[ERROR]' + input_filename + '\n'
84 error_message += ('These Attributes are defined'
85 'but the values are different.\n')
86 error_message += name[1] + ' = '
87 error_message += element.getAttributeNS(*name) + '\n'
88 error_message += mapped_name[1] + ' = '
89 error_message += element.getAttributeNS(*mapped_name) + '\n'
90 print >> sys.stderr, error_message
91 sys.exit(1)
92
93 # Add the new mapped attribute and remove the original attribute.
94 # For example, add paddingLeft and remove paddingStart.
95 element.setAttributeNS(mapped_name[0], mapped_name[1], value)
96 del element.attributes[name]
97
98 build_utils.MakeDirectory(os.path.dirname(output_filename))
99 dom.writexml(open(output_filename,"w"), ' ', '\n')
100
101
102 def generate_v14_resources_in_dir(input_dir, output_dir):
103 """Convert resources to API 14 compatible resources in the directory
104 recursively.
105 """
106 for root, dirs, files in os.walk(input_dir, followlinks=True):
107 relative_dir = os.path.relpath(root, input_dir)
108 for file_name in files:
109 if file_name.lower().endswith('.xml'):
110 input_filename = os.path.join(input_dir, relative_dir, file_name)
111 output_filename = os.path.join(output_dir, relative_dir, file_name)
112 generate_pre_v17_resource(input_filename, output_filename)
113
114
115 def ParseArgs():
116 """Parses command line options.
117
118 Returns:
119 An options object as from optparse.OptionsParser.parse_args()
120 """
121 parser = optparse.OptionParser()
122 parser.add_option('--res-dir',
123 help='directory containing v17 resources '
124 'be used to generate v14 resources')
125 parser.add_option('--res-v14-dir',
126 help='directory containing v14 resources to be generated')
127
128 (options, args) = parser.parse_args()
129
130 if args:
131 parser.error('No positional arguments should be given.')
132
133 # Check that required options have been provided.
134 required_options = ('res_dir', 'res_v14_dir')
135 build_utils.CheckOptions(options, parser, required=required_options)
136 return options
137
138
139 def main(argv):
140 """Convert Android xml resources to API 14 compatible.
cjhopman 2013/04/26 20:57:31 I'd prefer file-level docstring
Kibeom Kim (inactive) 2013/04/27 00:05:07 Done.
141
142 """
143
144 options = ParseArgs()
145
146 res_sub_dir_names = os.walk(options.res_dir).next()[1]
147 build_utils.MakeDirectory(options.res_v14_dir)
148
149 for res_sub_dir_name in res_sub_dir_names:
150 dir_pieces = res_sub_dir_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 generate_v14_resources_in_dir(os.path.join(options.res_dir,
164 res_sub_dir_name),
165 os.path.join(options.res_v14_dir,
166 res_sub_dir_name))
167
168
169 if __name__ == '__main__':
170 sys.exit(main(sys.argv))
171
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698