roll libyuv to r1450 from r1444. Adds direct YUY2ToNV12 and UYVYToNV12 conversions...
[chromium-blink-merge.git] / build / android / gyp / locale_pak_resources.py
blob84c4a37ba9254fbd1d325f0a0e116fdbb08e0f9e
1 #!/usr/bin/env python
3 # Copyright 2015 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 resources.zip for locale .pak files.
8 Places the locale.pak files into appropriate resource configs
9 (e.g. en-GB.pak -> res/raw-en/en_gb.lpak). Also generates a locale_paks
10 TypedArray so that resource files can be enumerated at runtime.
11 """
13 import collections
14 import optparse
15 import os
16 import sys
17 import zipfile
19 from util import build_utils
22 # This should stay in sync with:
23 # base/android/java/src/org/chromium/base/LocaleUtils.java
24 _CHROME_TO_ANDROID_LOCALE_MAP = {
25 'he': 'iw',
26 'id': 'in',
27 'fil': 'tl',
31 def ToResourceFileName(name):
32 """Returns the resource-compatible file name for the given file."""
33 # Resources file names must consist of [a-z0-9_.].
34 # Changes extension to .lpak so that compression can be toggled separately for
35 # locale pak files vs other pak files.
36 return name.replace('-', '_').replace('.pak', '.lpak').lower()
39 def CreateLocalePaksXml(names):
40 """Creates the contents for the locale-paks.xml files."""
41 VALUES_FILE_TEMPLATE = '''<?xml version="1.0" encoding="utf-8"?>
42 <resources>
43 <array name="locale_paks">%s
44 </array>
45 </resources>
46 '''
47 VALUES_ITEM_TEMPLATE = '''
48 <item>@raw/%s</item>'''
50 res_names = (os.path.splitext(name)[0] for name in names)
51 items = ''.join((VALUES_ITEM_TEMPLATE % name for name in res_names))
52 return VALUES_FILE_TEMPLATE % items
55 def ComputeMappings(sources):
56 """Computes the mappings of sources -> resources.
58 Returns a tuple of:
59 - mappings: List of (src, dest) paths
60 - lang_to_locale_map: Map of language -> list of resource names
61 e.g. "en" -> ["en_gb.lpak"]
62 """
63 lang_to_locale_map = collections.defaultdict(list)
64 mappings = []
65 for src_path in sources:
66 basename = os.path.basename(src_path)
67 name = os.path.splitext(basename)[0]
68 res_name = ToResourceFileName(basename)
69 if name == 'en-US':
70 dest_dir = 'raw'
71 else:
72 # Chrome's uses different region mapping logic from Android, so include
73 # all regions for each language.
74 android_locale = _CHROME_TO_ANDROID_LOCALE_MAP.get(name, name)
75 lang = android_locale[0:2]
76 dest_dir = 'raw-' + lang
77 lang_to_locale_map[lang].append(res_name)
78 mappings.append((src_path, os.path.join(dest_dir, res_name)))
79 return mappings, lang_to_locale_map
82 def main():
83 parser = optparse.OptionParser()
84 build_utils.AddDepfileOption(parser)
85 parser.add_option('--locale-paks', help='List of files for res/raw-LOCALE')
86 parser.add_option('--resources-zip', help='Path to output resources.zip')
87 parser.add_option('--print-languages',
88 action='store_true',
89 help='Print out the list of languages that cover the given locale paks '
90 '(using Android\'s language codes)')
92 options, _ = parser.parse_args()
93 build_utils.CheckOptions(options, parser,
94 required=['locale_paks'])
96 sources = build_utils.ParseGypList(options.locale_paks)
98 if options.depfile:
99 deps = sources + build_utils.GetPythonDependencies()
100 build_utils.WriteDepfile(options.depfile, deps)
102 mappings, lang_to_locale_map = ComputeMappings(sources)
103 if options.print_languages:
104 print '\n'.join(sorted(lang_to_locale_map))
106 if options.resources_zip:
107 with zipfile.ZipFile(options.resources_zip, 'w', zipfile.ZIP_STORED) as out:
108 for mapping in mappings:
109 out.write(mapping[0], mapping[1])
111 # Create TypedArray resources so ResourceExtractor can enumerate files.
112 def WriteValuesFile(lang, names):
113 dest_dir = 'values'
114 if lang:
115 dest_dir += '-' + lang
116 # Always extract en-US.lpak since it's the fallback.
117 xml = CreateLocalePaksXml(names + ['en_us.lpak'])
118 out.writestr(os.path.join(dest_dir, 'locale-paks.xml'), xml)
120 for lang, names in lang_to_locale_map.iteritems():
121 WriteValuesFile(lang, names)
122 WriteValuesFile(None, [])
125 if __name__ == '__main__':
126 sys.exit(main())