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

Unified Diff: build/android/gyp/locale_pak_assets.py

Issue 2345143002: Move language pak files to assets. (Closed)
Patch Set: Move language pak files back to assets rather than res. Created 4 years, 3 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
Index: build/android/gyp/locale_pak_assets.py
diff --git a/build/android/gyp/locale_pak_assets.py b/build/android/gyp/locale_pak_assets.py
new file mode 100755
index 0000000000000000000000000000000000000000..e837825b476566522b40bd77d3807219caee7105
--- /dev/null
+++ b/build/android/gyp/locale_pak_assets.py
@@ -0,0 +1,192 @@
+#!/usr/bin/env python
+#
+# Copyright 2016 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.
+"""Creates a srcjar for locale pak file paths.
+
+Creates a srcjar with a class containing a mapping of supported languages to
+corresponding locale pak files for each language so that these assets can be
+enumerated and extracted as necessary.
agrieve 2016/09/21 01:06:42 nit: can you mention the motivation is that AssetM
estevenson 2016/09/21 19:50:55 Done.
+
+The generated class implements:
+ //base/android/java/src/org/chromium/base/LocalePakFiles.java
+
+Providing access to pak file paths via:
+ static String[] getPakFilesForLocale(String lang)
+"""
+
+import argparse
+import collections
+import os
+import string
+import sys
+import zipfile
+
+sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))
+from pylib.constants import host_paths
+from util import build_utils
+
+_APK_ASSET_PATH = 'assets'
+_CLASSNAME = 'LocalePakFilesImpl'
+_PAK_US = 'en-US.pak'
+_PACKAGE_BASE = 'org.chromium.'
+_THIS_FILE = os.path.abspath(__file__)
+
+# This should stay in sync with:
+# base/android/java/src/org/chromium/base/LocaleUtils.java
+_CHROME_TO_ANDROID_LOCALE_MAP = {
+ 'he': 'iw',
+ 'id': 'in',
+ 'fil': 'tl',
+}
+
+def _ToAsset(filename):
+ """Add asset base folder as parent to |filename|.
+
+ Args:
+ filename: File name to be changed.
+
+ Returns:
+ Path to |filename| with the asset folder as base."""
+ return os.path.join(_APK_ASSET_PATH, filename)
+
+
+def _CreateLocalePakFilesJava(lang_to_locale_map, package, classname, target):
+ """Generate the java file contents for the locale pak file class.
+
+ Args:
+ lang_to_locale_map: Dict mapping language codes to pak files
+ e.g. {'am': ['assets/am.pak'], ...}.
+ package: Java package string.
+ classname: Java class name.
+ target: Target that ran the script.
+ """
+ file_tmpl = string.Template("""
+// Copyright 2016 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.
+
+// This file is generated by:
+// ${THIS_FILE}
+// From target:
+// ${TARGET}
+
+package ${PACKAGE};
+
+import org.chromium.base.LocalePakFiles;
+
+import java.util.HashMap;
+
+public class ${CLASSNAME} implements LocalePakFiles {
+ private static final HashMap<String, String[]> paks;
+ static {
+ paks = new HashMap<String, String[]>();
+ ${INIT_CODE}
+ }
+
+ public String[] getPakFilesForLocale(String lang) {
+ return paks.get(lang);
+ }
+}
+""")
+
+ add_locale_tmpl = string.Template("""
+ paks.put("${LANG}", new String[${NUM_PAKFILES}]);""")
+
+ add_pakfile_tmpl = string.Template("""
+ paks.get("${LANG}")[${IDX}] = "${PATH}";""")
+
+ init_lines = []
+ for lang, pakfiles in lang_to_locale_map.iteritems():
+ init_lines.append(
+ add_locale_tmpl.substitute(LANG=lang, NUM_PAKFILES=len(pakfiles) + 1))
+ init_lines.extend([
+ add_pakfile_tmpl.substitute(LANG=lang, IDX=idx, PATH=path)
+ for idx, path in enumerate(pakfiles)])
+
+ # Add US pak file since english is the fallback locale.
+ init_lines.append(add_pakfile_tmpl.substitute(
+ LANG=lang, IDX=len(pakfiles), PATH=_ToAsset(_PAK_US)))
+
+ file_tmpl_vals = {
+ 'TARGET': target,
+ 'PACKAGE': package,
+ 'CLASSNAME': classname,
+ 'INIT_CODE': ''.join(init_lines),
+ 'THIS_FILE': os.path.relpath(_THIS_FILE, host_paths.DIR_SOURCE_ROOT)
+ }
+
+ return file_tmpl.substitute(file_tmpl_vals)
+
+
+def _ComputeLangToLocaleMappings(sources):
+ """Create mappings from lang -> pak file.
+
+ Args:
+ sources: list of locale paths e.g. ['locales/am.pak', ...]
+
+ Returns:
+ Dict of language codes to pak files e.g. {'am': ['assets/am.pak'], ...}
+ """
+ lang_to_locale_map = collections.defaultdict(list)
+ for src_path in sources:
+ basename = os.path.basename(src_path)
+ name = os.path.splitext(basename)[0]
+ lang = _CHROME_TO_ANDROID_LOCALE_MAP.get(name, name)[0:2]
+ lang_to_locale_map[lang].append(_ToAsset(basename))
+
+ return lang_to_locale_map
+
+
+def _WriteJarOutput(output_path, in_zip_path, data):
+ """Write file data to a srcjar.
+
+ Args:
+ output_path: The output zip path.
+ in_zip_path: Destination path within the zip file.
+ contents: File data as a string.
+ """
+ path = os.path.dirname(output_path)
+ if path and not os.path.exists(path):
+ os.makedirs(path)
+ with zipfile.ZipFile(output_path, 'w') as srcjar:
+ build_utils.AddToZipHermetic(srcjar, in_zip_path, data=data)
+
+
+def main():
+ parser = argparse.ArgumentParser()
+ build_utils.AddDepfileOption(parser)
+ parser.add_argument('--locale-paks', required=True,
+ help='List of pak file paths to be added to srcjar')
+ parser.add_argument('--srcjar', required=True, help='Path to output srcjar')
+ parser.add_argument('--target', required=True, help='Target invoking script')
+ parser.add_argument('--package-suffix', required=True,
+ help='Specifies package suffix for generated Java file. The generated '
+ 'file will reside in org.chromium.PACKAGE_SUFFIX')
+ parser.add_argument('--print-languages',
+ action='store_true',
+ help='Print out the list of languages that cover the given locale paks '
+ '(using Android\'s language codes)')
+
+ args = parser.parse_args()
+
+ sources = build_utils.ParseGnList(args.locale_paks)
+
+ if args.depfile:
+ build_utils.WriteDepfile(args.depfile, args.srcjar, sources)
+
+ lang_to_locale_map = _ComputeLangToLocaleMappings(sources)
+ if args.print_languages:
+ print '\n'.join(sorted(lang_to_locale_map))
+
+ package = _PACKAGE_BASE + args.package_suffix
+ srcjar_contents = _CreateLocalePakFilesJava(
+ lang_to_locale_map, package, _CLASSNAME, args.target)
+ in_zip_path = os.path.join(
+ package.replace('.', '/'), _CLASSNAME + '.java')
+ _WriteJarOutput(args.srcjar, in_zip_path, srcjar_contents)
+
+
+if __name__ == '__main__':
+ sys.exit(main())

Powered by Google App Engine
This is Rietveld 408576698