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

Unified Diff: build/android/gyp/mirror_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: Fixed an error caused by ">(res_dir)" Created 7 years, 8 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | build/android/gyp/process_resources.py » ('j') | build/android/gyp/process_resources.py » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: build/android/gyp/mirror_resources.py
diff --git a/build/android/gyp/mirror_resources.py b/build/android/gyp/mirror_resources.py
new file mode 100755
index 0000000000000000000000000000000000000000..b06f746bdf9fbfd7e3dc48dd0bc9986a86d99ae2
--- /dev/null
+++ b/build/android/gyp/mirror_resources.py
@@ -0,0 +1,139 @@
+#!/usr/bin/env python
+#
+# Copyright 2013 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.
+
+import optparse
+import os
+import re
+import sys
+import xml.etree.cElementTree as ET
+
+from util import build_utils
+
+
+ATTRIBUTE_PREFIX = '{http://schemas.android.com/apk/res/android}'
+
+ATTRIBUTES_TO_MIRROR = ['paddingLeft',
+ 'drawableLeft',
+ 'layout_alignLeft',
+ 'layout_marginLeft',
+ 'layout_alignParentLeft',
+ 'layout_toLeftOf',
+ 'paddingRight',
+ 'drawableRight',
+ 'layout_alignRight',
+ 'layout_marginRight',
+ 'layout_alignParentRight',
+ 'layout_toRightOf',
+ 'gravity',
+ 'layout_gravity']
+
+ATTRIBUTES_TO_MIRROR = set([ATTRIBUTE_PREFIX + i for i in ATTRIBUTES_TO_MIRROR])
+
+
+def mirror_resource(input_filename, output_filename):
+ """Convert LTR resource to RTL android resource. (or the other way around)
+
+ For example,
+ from paddingLeft="10dp" to paddingRight="10dp"
+ from gravity="right|center_vertical" to gravity="left|center_vertical"
+ """
+
+ tree = ET.parse(input_filename)
+ for element in tree.iter():
+ attribute_names = ATTRIBUTES_TO_MIRROR.intersection(element.keys())
newt (away) 2013/04/26 18:46:26 if this file changes to convert RTL-friendly layou
+ mirrored_attributes = {}
+ for attribute_name in attribute_names:
+ if 'Left' in attribute_name:
+ mirrored_attribute_name = attribute_name.replace('Left', 'Right')
+ mirrored_attribute_value = element.attrib[attribute_name]
+ elif 'Right' in attribute_name:
+ mirrored_attribute_name = attribute_name.replace('Right', 'Left')
+ mirrored_attribute_value = element.attrib[attribute_name]
+ else:
+ # This is either gravity or layout_gravity
+ mirrored_attribute_name = attribute_name
+ mirrored_attribute_value = re.sub('left|right',
+ lambda x: 'right' if x.group(0) == 'left' else 'left',
+ element.attrib[attribute_name])
+
+ mirrored_attributes[mirrored_attribute_name] = mirrored_attribute_value
+ # Delete the attribute that we are going to mirror.
+ del element.attrib[attribute_name]
+
+ # Update the parsed xml with the mirrored attributes.
+ element.attrib.update(mirrored_attributes)
+
+ build_utils.MakeDirectory(os.path.dirname(output_filename))
+ tree.write(output_filename, encoding='utf-8', xml_declaration=True)
+
+
+def mirror_resources_in_dir(input_dir, output_dir):
+ for root, dirs, files in os.walk(input_dir, followlinks=True):
+ relative_dir = os.path.relpath(root, input_dir)
+ for file_name in files:
+ if file_name.lower().endswith('.xml'):
+ input_filename = os.path.join(input_dir, relative_dir, file_name)
+ output_filename = os.path.join(output_dir, relative_dir, file_name)
+ mirror_resource(input_filename, output_filename)
+
+
+def ParseArgs():
+ """Parses command line options.
+
+ Returns:
+ An options object as from optparse.OptionsParser.parse_args()
+ """
+ parser = optparse.OptionParser()
+ parser.add_option('--res-dir',
+ help='directory containing resources to be mirrored to RTL')
+ parser.add_option('--res-mirrored-dir',
+ help='directory containing RTL resources to be generated')
+
+ (options, args) = parser.parse_args()
+
+ if args:
+ parser.error('No positional arguments should be given.')
+
+ # Check that required options have been provided.
+ required_options = ('res_dir', 'res_mirrored_dir')
+ build_utils.CheckOptions(options, parser, required=required_options)
+ return options
+
+
+def main(argv):
+ """Convert Android LTR xml resources to RTL and save it to -ldrtl directories.
+
+ Please refer to the function mirror_resource's comment on how the files
+ are mirrored.
+
+ ldrtl directory is explained at
+ http://developer.android.com/guide/topics/resources/providing-resources.html
+ """
+ options = ParseArgs()
+
+ res_sub_dirs = os.walk(options.res_dir).next()[1]
+
+ for res_sub_dir in res_sub_dirs:
+ # If the directory has already RTL resources, we don't need to mirror. Skip.
+ if '-ldrtl' in res_sub_dir:
+ continue
+
+ # We only mirror resources under layout*/ or xml*/.
+ if (res_sub_dir.startswith('layout') or res_sub_dir.startswith('xml')):
+ # Insert ldrtl to the dir name.
+ # For example, from layout-hdpi to layout-ldrtl-hdpi.
+ res_sub_dir_split = res_sub_dir.split('-')
+ res_sub_dir_split.insert(1, 'ldrtl')
+ res_mirrored_sub_dir = '-'.join(res_sub_dir_split)
+ # Mirror all the resource files.
+ mirror_resources_in_dir(
+ os.path.join(options.res_dir, res_sub_dir),
+ os.path.join(options.res_mirrored_dir, res_mirrored_sub_dir))
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))
+
« no previous file with comments | « no previous file | build/android/gyp/process_resources.py » ('j') | build/android/gyp/process_resources.py » ('J')

Powered by Google App Engine
This is Rietveld 408576698