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

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: 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
« no previous file with comments | « build/android/gyp/copy_v17_resources.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 attributes, 9 There are two reasons that we cannot just use API 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 def IterateXmlElements(node): 56 def IterateXmlElements(node):
57 """minidom helper function that iterates all the element nodes. 57 """minidom helper function that iterates all the element nodes.
58 Iteration order is pre-order depth-first.""" 58 Iteration order is pre-order depth-first."""
59 if node.nodeType == node.ELEMENT_NODE: 59 if node.nodeType == node.ELEMENT_NODE:
60 yield node 60 yield node
61 for child_node in node.childNodes: 61 for child_node in node.childNodes:
62 for child_node_element in IterateXmlElements(child_node): 62 for child_node_element in IterateXmlElements(child_node):
63 yield child_node_element 63 yield child_node_element
64 64
65 65
66 def GenerateV14Resource(input_filename, output_filename): 66 def GenerateV14StyleXmlResource(dom, output_file):
67 """Convert resource to API 14 compatible resource. 67 """Convert style resource to API 14 compatible style resource.
68
69 It's mostly a simple replacement, s/Start/Left s/End/Right,
70 on the attribute names specified by <item> element.
71 """
72 for style_element in dom.getElementsByTagName('style'):
73 for item_element in style_element.getElementsByTagName('item'):
74 namespace, name = item_element.attributes['name'].value.split(':')
75 if name in ATTRIBUTES_TO_MAP:
76 mapped_name = ATTRIBUTES_TO_MAP[name]
77 item_element.attributes['name'] = namespace + ':' + mapped_name
78
79 build_utils.MakeDirectory(os.path.dirname(output_file))
80 with open(output_file, 'w') as f:
81 dom.writexml(f, '', ' ', '\n', encoding='utf-8')
82
83
84 def GenerateV14LayoutXmlResource(dom, output_file):
85 """Convert layout resource to API 14 compatible layout resource.
68 86
69 It's mostly a simple replacement, s/Start/Left s/End/Right, 87 It's mostly a simple replacement, s/Start/Left s/End/Right,
70 on the attribute names. 88 on the attribute names.
71 """ 89 """
72 dom = minidom.parse(input_filename)
73
74 for element in IterateXmlElements(dom): 90 for element in IterateXmlElements(dom):
75 all_names = element.attributes.keysNS() 91 all_names = element.attributes.keysNS()
76 92
77 # Iterate all the attributes to find attributes to convert. 93 # Iterate all the attributes to find attributes to convert.
78 # Note that name variable is actually a tuple that has namespace and name. 94 # Note that name variable is actually a tuple that has namespace and name.
79 # For example, 95 # For example,
80 # name == ('http://schemas.android.com/apk/res/android', 'paddingStart') 96 # name == ('http://schemas.android.com/apk/res/android', 'paddingStart')
81 for name, value in list(element.attributes.itemsNS()): 97 for name, value in list(element.attributes.itemsNS()):
82 # Note: gravity attributes are not necessary to convert because 98 # Note: gravity attributes are not necessary to convert because
83 # start/end values are backward-compatible. Explained at 99 # start/end values are backward-compatible. Explained at
(...skipping 12 matching lines...) Expand all
96 # setAttributeNS. Hence this workaround. 112 # setAttributeNS. Hence this workaround.
97 # This is a similar bug discussion about minidom namespace normalizing. 113 # This is a similar bug discussion about minidom namespace normalizing.
98 # http://stackoverflow.com/questions/863774/how-to-generate-xml-document s-with-namespaces-in-python 114 # http://stackoverflow.com/questions/863774/how-to-generate-xml-document s-with-namespaces-in-python
99 element.setAttribute('android:' + mapped_name[1], value) 115 element.setAttribute('android:' + mapped_name[1], value)
100 del element.attributes[name] 116 del element.attributes[name]
101 elif name in ATTRIBUTES_TO_MAP_NS_VALUES: 117 elif name in ATTRIBUTES_TO_MAP_NS_VALUES:
102 # TODO(kkimlabs): Enable warning once layouts have been converted 118 # TODO(kkimlabs): Enable warning once layouts have been converted
103 # print >> sys.stderror, 'Warning: layout should use xxx instead of yyy' 119 # print >> sys.stderror, 'Warning: layout should use xxx instead of yyy'
104 pass 120 pass
105 121
106 build_utils.MakeDirectory(os.path.dirname(output_filename)) 122 build_utils.MakeDirectory(os.path.dirname(output_file))
107 with open(output_filename, 'w') as f: 123 with open(output_file, 'w') as f:
108 dom.writexml(f, ' ', '\n', encoding='utf-8') 124 dom.writexml(f, '', ' ', '\n', encoding='utf-8')
109 125
110 126
111 def GenerateV14ResourcesInDir(input_dir, output_dir): 127 def GenerateV14XmlResourcesInDir(input_dir, output_dir):
112 """Convert resources to API 14 compatible XML resources in the directory.""" 128 """Convert resources to API 14 compatible XML resources in the directory."""
113 for input_file in build_utils.FindInDirectory(input_dir, '*.xml'): 129 for input_file in build_utils.FindInDirectory(input_dir, '*.xml'):
114 output_path = os.path.join(output_dir, 130 output_path = os.path.join(output_dir,
115 os.path.relpath(input_file, input_dir)) 131 os.path.relpath(input_file, input_dir))
116 GenerateV14Resource(input_file, output_path) 132 dom = minidom.parse(input_file)
133 if dom.getElementsByTagName('resources'):
134 if dom.getElementsByTagName('style'):
135 GenerateV14StyleXmlResource(dom, output_path)
136 else:
137 GenerateV14LayoutXmlResource(dom, output_path)
117 138
118 139
119 def ParseArgs(): 140 def ParseArgs():
120 """Parses command line options. 141 """Parses command line options.
121 142
122 Returns: 143 Returns:
123 An options object as from optparse.OptionsParser.parse_args() 144 An options object as from optparse.OptionsParser.parse_args()
124 """ 145 """
125 parser = optparse.OptionParser() 146 parser = optparse.OptionParser()
126 parser.add_option('--res-dir', 147 parser.add_option('--res-dir',
(...skipping 22 matching lines...) Expand all
149 build_utils.MakeDirectory(options.res_v14_dir) 170 build_utils.MakeDirectory(options.res_v14_dir)
150 171
151 for name in os.listdir(options.res_dir): 172 for name in os.listdir(options.res_dir):
152 if not os.path.isdir(os.path.join(options.res_dir, name)): 173 if not os.path.isdir(os.path.join(options.res_dir, name)):
153 continue 174 continue
154 175
155 dir_pieces = name.split('-') 176 dir_pieces = name.split('-')
156 resource_type = dir_pieces[0] 177 resource_type = dir_pieces[0]
157 qualifiers = dir_pieces[1:] 178 qualifiers = dir_pieces[1:]
158 179
159 # We only convert resources under layout*/ and xml*/.
160 if resource_type not in ('layout', 'xml'):
161 continue
162
163 # Android pre-v17 API doesn't support RTL. Skip. 180 # Android pre-v17 API doesn't support RTL. Skip.
164 if 'ldrtl' in qualifiers: 181 if 'ldrtl' in qualifiers:
165 continue 182 continue
166 183
167 # Convert all the resource files. 184 input_dir = os.path.join(options.res_dir, name)
168 input_path = os.path.join(options.res_dir, name) 185 output_dir = os.path.join(options.res_v14_dir, name)
169 output_path = os.path.join(options.res_v14_dir, name) 186
170 GenerateV14ResourcesInDir(input_path, output_path) 187 # We only convert resources under layout*/, xml*/,
188 # and style resources under values*/.
189 if resource_type in ('layout', 'xml', 'values'):
190 GenerateV14XmlResourcesInDir(input_dir, output_dir)
171 191
172 if options.stamp: 192 if options.stamp:
173 build_utils.Touch(options.stamp) 193 build_utils.Touch(options.stamp)
174 194
175 if __name__ == '__main__': 195 if __name__ == '__main__':
176 sys.exit(main(sys.argv)) 196 sys.exit(main(sys.argv))
177 197
OLDNEW
« no previous file with comments | « build/android/gyp/copy_v17_resources.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698