| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # | |
| 3 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 4 # Use of this source code is governed by a BSD-style license that can be | |
| 5 # found in the LICENSE file. | |
| 6 """Creates a srcjar for locale pak file paths. | |
| 7 | |
| 8 Creates a srcjar with a class containing an array of locale pak files so that | |
| 9 these assets can be enumerated and extracted as necessary. This is much | |
| 10 more efficient than using AssetManager.list(). | |
| 11 | |
| 12 The generated class implements: | |
| 13 //base/android/java/src/org/chromium/base/LocalePakFiles.java | |
| 14 | |
| 15 Providing access to pak file paths via: | |
| 16 public static String[] getFiles() | |
| 17 """ | |
| 18 | |
| 19 import argparse | |
| 20 import collections | |
| 21 import os | |
| 22 import string | |
| 23 import sys | |
| 24 import zipfile | |
| 25 | |
| 26 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir)) | |
| 27 from pylib.constants import host_paths | |
| 28 from util import build_utils | |
| 29 | |
| 30 _CLASSNAME = 'LocalePakFiles' | |
| 31 _PACKAGE = 'org.chromium.ui' | |
| 32 _THIS_FILE = os.path.abspath(__file__) | |
| 33 | |
| 34 | |
| 35 def _CreateLocalePakFilesJava(pakfiles, package, classname, target): | |
| 36 """Generate the java file contents for the locale pak file class.""" | |
| 37 file_tmpl = string.Template(""" | |
| 38 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 39 // Use of this source code is governed by a BSD-style license that can be | |
| 40 // found in the LICENSE file. | |
| 41 | |
| 42 // This file is generated by: | |
| 43 // ${THIS_FILE} | |
| 44 // From target: | |
| 45 // ${TARGET} | |
| 46 | |
| 47 package ${PACKAGE}; | |
| 48 | |
| 49 import org.chromium.base.annotations.SuppressFBWarnings; | |
| 50 | |
| 51 public class LocalePakFiles { | |
| 52 private static String[] pakFiles = {${PAKFILES}}; | |
| 53 | |
| 54 @SuppressFBWarnings("MS_EXPOSE_REP") | |
| 55 public static String[] getFiles() { | |
| 56 return pakFiles; | |
| 57 } | |
| 58 } | |
| 59 """) | |
| 60 | |
| 61 values = { | |
| 62 'TARGET': target, | |
| 63 'PACKAGE': package, | |
| 64 'CLASSNAME': classname, | |
| 65 'THIS_FILE': os.path.relpath(_THIS_FILE, host_paths.DIR_SOURCE_ROOT), | |
| 66 'PAKFILES': ', '.join('"%s"' % p for p in pakfiles), | |
| 67 } | |
| 68 | |
| 69 return file_tmpl.substitute(values) | |
| 70 | |
| 71 | |
| 72 def _WriteJarOutput(output_path, in_zip_path, data): | |
| 73 """Write file data to a srcjar.""" | |
| 74 path = os.path.dirname(output_path) | |
| 75 if path and not os.path.exists(path): | |
| 76 os.makedirs(path) | |
| 77 with zipfile.ZipFile(output_path, 'w') as srcjar: | |
| 78 build_utils.AddToZipHermetic(srcjar, in_zip_path, data=data) | |
| 79 | |
| 80 | |
| 81 def main(): | |
| 82 parser = argparse.ArgumentParser() | |
| 83 build_utils.AddDepfileOption(parser) | |
| 84 parser.add_argument('--locale-paks', required=True, | |
| 85 help='List of pak file paths to be added to srcjar') | |
| 86 parser.add_argument('--srcjar', required=True, help='Path to output srcjar') | |
| 87 parser.add_argument('--target', required=True, help='Target invoking script') | |
| 88 | |
| 89 args = parser.parse_args() | |
| 90 | |
| 91 sources = build_utils.ParseGnList(args.locale_paks) | |
| 92 | |
| 93 if args.depfile: | |
| 94 build_utils.WriteDepfile(args.depfile, args.srcjar) | |
| 95 | |
| 96 pakfiles = [os.path.basename(s) for s in sources] | |
| 97 srcjar_contents = _CreateLocalePakFilesJava( | |
| 98 pakfiles, _PACKAGE, _CLASSNAME, args.target) | |
| 99 in_zip_path = os.path.join(_PACKAGE.replace('.', '/'), _CLASSNAME + '.java') | |
| 100 _WriteJarOutput(args.srcjar, in_zip_path, srcjar_contents) | |
| 101 | |
| 102 | |
| 103 if __name__ == '__main__': | |
| 104 sys.exit(main()) | |
| OLD | NEW |