Chromium Code Reviews| Index: build/android/gyp/jarjar_resources.py |
| diff --git a/build/android/gyp/jarjar_resources.py b/build/android/gyp/jarjar_resources.py |
| new file mode 100755 |
| index 0000000000000000000000000000000000000000..2d31908080aa92495e2533b734eda0ff3ba8da2e |
| --- /dev/null |
| +++ b/build/android/gyp/jarjar_resources.py |
| @@ -0,0 +1,103 @@ |
| +#!/usr/bin/env python |
| +# Copyright 2014 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
|
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.
|
| +import optparse |
| +import os |
| +import shutil |
| +import sys |
| +from xml.dom import minidom |
| + |
| +from util import build_utils |
| + |
|
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.
|
| +class JarJarRules(object): |
|
mkosiba (inactive)
2014/05/12 17:35:50
I know this is duplicated from the jni generator.
|
| + def __init__(self, jarjar_rules): |
| + self._rules = [] |
| + for line in jarjar_rules.splitlines(): |
| + rule = line.split() |
| + if rule[0] != 'rule': |
| + continue |
| + _, src, dest = rule |
| + assert src.endswith('**') |
| + src = src[:-2] |
| + |
|
newt (away)
2014/05/12 20:11:23
nit: remove extra empty line
mkosiba (inactive)
2014/06/12 11:11:19
Done.
|
| + |
| + if dest.endswith('@0'): |
| + self._rules.append((src, dest[:-2] + src)) |
| + else: |
| + assert dest.endswith('@1') |
| + self._rules.append((src, dest[:-2])) |
| + print self._rules |
|
bulach
2014/05/13 08:42:39
nit: maybe remove this print?
mkosiba (inactive)
2014/06/12 11:11:19
Done.
|
| + |
| + def RenameClass(self, class_name): |
| + for old, new in self._rules: |
| + if old in class_name: |
| + return class_name.replace(old, new, 1) |
| + return class_name |
| + |
| +def RenameNodes(node, rules): |
| + if node.nodeType == node.ELEMENT_NODE: |
| + if node.tagName.lower() == 'view' and node.attributes.has_key('class'): |
| + node.attributes['class'] = rules.RenameClass(node.attributes['class']) |
| + else: |
| + node.tagName = rules.RenameClass(node.tagName) |
| + for child in node.childNodes: |
| + RenameNodes(child, rules) |
| + |
| +def ProcessLayoutFile(path, rules): |
| + xmldoc = minidom.parse(path) |
| + RenameNodes(xmldoc.documentElement, rules) |
| + with open(path, 'w') as f: |
| + xmldoc.writexml(f) |
| + |
| +def LayoutFilesFilter(src, names): |
| + if os.path.basename(src).lower() != 'layout': |
| + return set() |
| + else: |
| + 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.
|
| + |
| +def ProcessResources(options): |
| + with open(options.rules_path) as f: |
| + rules = JarJarRules(f.read()) |
| + |
| + build_utils.DeleteDirectory(options.output_dir) |
| + for input_dir in options.input_dir: |
| + shutil.copytree(input_dir, options.output_dir) |
| + |
| + for root, _dirnames, filenames in os.walk(options.output_dir): |
| + layout_files = LayoutFilesFilter(root, filenames) |
| + for layout_file in layout_files: |
| + ProcessLayoutFile(os.path.join(root, layout_file), rules) |
| + |
| +def ParseArgs(): |
| + parser = optparse.OptionParser() |
| + 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.
|
| + help='Path to the resources folder to process.') |
| + parser.add_option('--output-dir', |
| + 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.
|
| + parser.add_option('--rules-path', |
| + help='Path to the jarjar rules file.') |
| + parser.add_option('--stamp', help='Path to touch on success.') |
| + |
| + options, args = parser.parse_args() |
| + |
| + if args: |
| + parser.error('No positional arguments should be given.') |
| + |
| + # Check that required options have been provided. |
| + required_options = ('input_dir', 'output_dir', 'rules_path') |
| + build_utils.CheckOptions(options, parser, required=required_options) |
| + |
| + return options |
| + |
| +def main(): |
| + options = ParseArgs() |
| + |
| + ProcessResources(options) |
| + |
| + if options.stamp: |
| + build_utils.Touch(options.stamp) |
| + |
| +if __name__ == '__main__': |
| + sys.exit(main()) |