OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
cjhopman
2014/05/13 16:41:31
Nit: add a file-level docstring (as long or short
mkosiba (inactive)
2014/06/12 11:11:19
Done.
| |
6 import optparse | |
7 import os | |
8 import shutil | |
9 import sys | |
10 from xml.dom import minidom | |
11 | |
12 from util import build_utils | |
13 | |
bulach
2014/05/13 08:42:39
nit: need another \n, i.e., two between top-levels
mkosiba (inactive)
2014/06/12 11:11:19
Done.
| |
14 class JarJarRules(object): | |
mkosiba (inactive)
2014/05/12 17:35:50
I know this is duplicated from the jni generator.
| |
15 def __init__(self, jarjar_rules): | |
16 self._rules = [] | |
17 for line in jarjar_rules.splitlines(): | |
18 rule = line.split() | |
19 if rule[0] != 'rule': | |
20 continue | |
21 _, src, dest = rule | |
22 assert src.endswith('**') | |
23 src = src[:-2] | |
24 | |
newt (away)
2014/05/12 20:11:23
nit: remove extra empty line
mkosiba (inactive)
2014/06/12 11:11:19
Done.
| |
25 | |
26 if dest.endswith('@0'): | |
27 self._rules.append((src, dest[:-2] + src)) | |
28 else: | |
29 assert dest.endswith('@1') | |
30 self._rules.append((src, dest[:-2])) | |
31 print self._rules | |
bulach
2014/05/13 08:42:39
nit: maybe remove this print?
mkosiba (inactive)
2014/06/12 11:11:19
Done.
| |
32 | |
33 def RenameClass(self, class_name): | |
34 for old, new in self._rules: | |
35 if old in class_name: | |
36 return class_name.replace(old, new, 1) | |
37 return class_name | |
38 | |
39 def RenameNodes(node, rules): | |
40 if node.nodeType == node.ELEMENT_NODE: | |
41 if node.tagName.lower() == 'view' and node.attributes.has_key('class'): | |
42 node.attributes['class'] = rules.RenameClass(node.attributes['class']) | |
43 else: | |
44 node.tagName = rules.RenameClass(node.tagName) | |
45 for child in node.childNodes: | |
46 RenameNodes(child, rules) | |
47 | |
48 def ProcessLayoutFile(path, rules): | |
49 xmldoc = minidom.parse(path) | |
50 RenameNodes(xmldoc.documentElement, rules) | |
51 with open(path, 'w') as f: | |
52 xmldoc.writexml(f) | |
53 | |
54 def LayoutFilesFilter(src, names): | |
55 if os.path.basename(src).lower() != 'layout': | |
56 return set() | |
57 else: | |
58 return {name for name in names if name.endswith('.xml')} | |
bulach
2014/05/13 08:42:39
nit: perhaps:
filter(lambda n: n.endswith('.xml'),
mkosiba (inactive)
2014/06/12 11:11:19
Done.
| |
59 | |
60 def ProcessResources(options): | |
61 with open(options.rules_path) as f: | |
62 rules = JarJarRules(f.read()) | |
63 | |
64 build_utils.DeleteDirectory(options.output_dir) | |
65 for input_dir in options.input_dir: | |
66 shutil.copytree(input_dir, options.output_dir) | |
67 | |
68 for root, _dirnames, filenames in os.walk(options.output_dir): | |
69 layout_files = LayoutFilesFilter(root, filenames) | |
70 for layout_file in layout_files: | |
71 ProcessLayoutFile(os.path.join(root, layout_file), rules) | |
72 | |
73 def ParseArgs(): | |
74 parser = optparse.OptionParser() | |
75 parser.add_option('--input-dir', action="append", | |
bulach
2014/05/13 08:42:39
nit: s/"/'/
mkosiba (inactive)
2014/06/12 11:11:19
Done.
| |
76 help='Path to the resources folder to process.') | |
77 parser.add_option('--output-dir', | |
78 help='Directory to hold processed resources.') | |
bulach
2014/05/13 08:42:39
nit: perhaps add a note saying it'll be clobbered?
mkosiba (inactive)
2014/06/12 11:11:19
Done.
| |
79 parser.add_option('--rules-path', | |
80 help='Path to the jarjar rules file.') | |
81 parser.add_option('--stamp', help='Path to touch on success.') | |
82 | |
83 options, args = parser.parse_args() | |
84 | |
85 if args: | |
86 parser.error('No positional arguments should be given.') | |
87 | |
88 # Check that required options have been provided. | |
89 required_options = ('input_dir', 'output_dir', 'rules_path') | |
90 build_utils.CheckOptions(options, parser, required=required_options) | |
91 | |
92 return options | |
93 | |
94 def main(): | |
95 options = ParseArgs() | |
96 | |
97 ProcessResources(options) | |
98 | |
99 if options.stamp: | |
100 build_utils.Touch(options.stamp) | |
101 | |
102 if __name__ == '__main__': | |
103 sys.exit(main()) | |
OLD | NEW |