Chromium Code Reviews| 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..1be4f4dd97820c79f4d77d2218096e681dc3606d |
| --- /dev/null |
| +++ b/build/android/gyp/locale_pak_assets.py |
| @@ -0,0 +1,106 @@ |
| +#!/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 an array of locale pak files so that |
| +these assets can be enumerated and extracted as necessary. This is much |
| +more efficient than using AssetManager.list(). |
| + |
| +The generated class implements: |
| + //base/android/java/src/org/chromium/base/LocalePakFiles.java |
| + |
| +Providing access to pak file paths via: |
| + public static String[] getFiles() |
| +""" |
| + |
| +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 |
| + |
| +_CLASSNAME = 'LocalePakFiles' |
| +_PACKAGE_BASE = 'org.chromium.' |
| +_THIS_FILE = os.path.abspath(__file__) |
| + |
| + |
| +def _CreateLocalePakFilesJava(pakfiles, package, classname, target): |
| + """Generate the java file contents for the locale pak file class.""" |
| + 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}; |
| + |
| +public class LocalePakFiles { |
| + private static String[] pakFiles = {${PAKFILES}}; |
| + |
| + public static String[] getFiles() { |
| + return pakFiles; |
| + } |
| +} |
| +""") |
| + |
| + values = { |
| + 'TARGET': target, |
| + 'PACKAGE': package, |
| + 'CLASSNAME': classname, |
| + 'THIS_FILE': os.path.relpath(_THIS_FILE, host_paths.DIR_SOURCE_ROOT), |
| + 'PAKFILES': ', '.join('"%s"' % p for p in pakfiles), |
| + } |
| + |
| + return file_tmpl.substitute(values) |
| + |
| + |
| +def _WriteJarOutput(output_path, in_zip_path, data): |
| + """Write file data to a srcjar.""" |
| + 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') |
| + |
| + args = parser.parse_args() |
| + |
| + sources = build_utils.ParseGnList(args.locale_paks) |
| + |
| + if args.depfile: |
| + build_utils.WriteDepfile(args.depfile, args.srcjar, sources) |
|
agrieve
2016/09/21 20:34:02
nit: don't pass sources into WriteDepfile. GN alre
estevenson
2016/09/21 22:35:18
Done.
|
| + |
| + pakfiles = [os.path.basename(s) for s in sources] |
| + package = _PACKAGE_BASE + args.package_suffix |
| + srcjar_contents = _CreateLocalePakFilesJava( |
| + pakfiles, 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()) |