Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / tools / build / repack_locales.py
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 """Helper script to repack paks for a list of locales.
7
8 Gyp doesn't have any built-in looping capability, so this just provides a way to
9 loop over a list of locales when repacking pak files, thus avoiding a
10 proliferation of mostly duplicate, cut-n-paste gyp actions.
11 """
12
13 import optparse
14 import os
15 import sys
16
17 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..',
18                              'tools', 'grit'))
19 from grit.format import data_pack
20
21 # The gyp "branding" variable.
22 BRANDING = None
23
24 # Some build paths defined by gyp.
25 GRIT_DIR = None
26 SHARE_INT_DIR = None
27 INT_DIR = None
28
29 # The target platform. If it is not defined, sys.platform will be used.
30 OS = None
31
32 # Note that OS is normally set to 'linux' when building for chromeos.
33 CHROMEOS = False
34
35 USE_ASH = False
36 USE_ATHENA = False
37 ENABLE_AUTOFILL_DIALOG = False
38 ENABLE_EXTENSIONS = False
39
40 WHITELIST = None
41
42 # Extra input files.
43 EXTRA_INPUT_FILES = []
44
45 class Usage(Exception):
46   def __init__(self, msg):
47     self.msg = msg
48
49
50 def calc_output(locale):
51   """Determine the file that will be generated for the given locale."""
52   #e.g. '<(INTERMEDIATE_DIR)/repack/da.pak',
53   # For Fake Bidi, generate it at a fixed path so that tests can safely
54   # reference it.
55   if locale == 'fake-bidi':
56     return '%s/%s.pak' % (INT_DIR, locale)
57   if OS == 'mac' or OS == 'ios':
58     # For Cocoa to find the locale at runtime, it needs to use '_' instead
59     # of '-' (http://crbug.com/20441).  Also, 'en-US' should be represented
60     # simply as 'en' (http://crbug.com/19165, http://crbug.com/25578).
61     if locale == 'en-US':
62       locale = 'en'
63     return '%s/repack/%s.lproj/locale.pak' % (INT_DIR, locale.replace('-', '_'))
64   else:
65     return os.path.join(INT_DIR, 'repack', locale + '.pak')
66
67
68 def calc_inputs(locale):
69   """Determine the files that need processing for the given locale."""
70   inputs = []
71
72   #e.g. '<(grit_out_dir)/generated_resources_da.pak'
73   inputs.append(os.path.join(GRIT_DIR, 'generated_resources_%s.pak' % locale))
74
75   #e.g. '<(grit_out_dir)/locale_settings_da.pak'
76   inputs.append(os.path.join(GRIT_DIR, 'locale_settings_%s.pak' % locale))
77
78   #e.g. '<(grit_out_dir)/platform_locale_settings_da.pak'
79   inputs.append(os.path.join(GRIT_DIR,
80                 'platform_locale_settings_%s.pak' % locale))
81
82   #e.g. '<(SHARED_INTERMEDIATE_DIR)/components/strings/
83   # components_strings_da.pak',
84   inputs.append(os.path.join(SHARE_INT_DIR, 'components', 'strings',
85                 'components_strings_%s.pak' % locale))
86
87   if USE_ASH:
88     #e.g. '<(SHARED_INTERMEDIATE_DIR)/ash/strings/ash_strings_da.pak',
89     inputs.append(os.path.join(SHARE_INT_DIR, 'ash', 'strings',
90                   'ash_strings_%s.pak' % locale))
91
92   if USE_ATHENA:
93     #e.g. '<(SHARED_INTERMEDIATE_DIR)/athena/strings/athena_strings_da.pak',
94     inputs.append(os.path.join(SHARE_INT_DIR, 'athena', 'strings',
95                   'athena_strings_%s.pak' % locale))
96
97   if CHROMEOS:
98     inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'chromeos', 'strings',
99                   'ui_chromeos_strings_%s.pak' % locale))
100     inputs.append(os.path.join(SHARE_INT_DIR, 'remoting', 'resources',
101                   '%s.pak' % locale))
102
103   if OS != 'ios':
104     #e.g.
105     # '<(SHARED_INTERMEDIATE_DIR)/content/app/strings/content_strings_da.pak'
106     inputs.append(os.path.join(SHARE_INT_DIR, 'content', 'app', 'strings',
107                   'content_strings_%s.pak' % locale))
108
109     #e.g. '<(SHARED_INTERMEDIATE_DIR)/ui/strings/ui_strings_da.pak',
110     inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'strings',
111                   'ui_strings_%s.pak' % locale))
112
113     #e.g. '<(SHARED_INTERMEDIATE_DIR)/ui/strings/app_locale_settings_da.pak',
114     inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'strings',
115                   'app_locale_settings_%s.pak' % locale))
116
117   if ENABLE_AUTOFILL_DIALOG and OS != 'ios' and OS != 'android':
118     #e.g. '<(SHARED_INTERMEDIATE_DIR)/third_party/libaddressinput/
119     # address_input_strings_da.pak',
120     inputs.append(os.path.join(SHARE_INT_DIR, 'third_party', 'libaddressinput',
121                                'address_input_strings_%s.pak' % locale))
122
123   if ENABLE_EXTENSIONS:
124     #e.g. '<(SHARED_INTERMEDIATE_DIR)/device/bluetooth/strings/
125     # device_bluetooth_strings_da.pak',
126     inputs.append(os.path.join(SHARE_INT_DIR, 'device', 'bluetooth', 'strings',
127                   'device_bluetooth_strings_%s.pak' % locale))
128
129     # For example:
130     # '<(SHARED_INTERMEDIATE_DIR)/extensions/strings/extensions_strings_da.pak
131     # TODO(jamescook): When Android stops building extensions code move this
132     # to the OS != 'ios' and OS != 'android' section below.
133     inputs.append(os.path.join(SHARE_INT_DIR, 'extensions', 'strings',
134                   'extensions_strings_%s.pak' % locale))
135
136   #e.g. '<(grit_out_dir)/google_chrome_strings_da.pak'
137   #     or
138   #     '<(grit_out_dir)/chromium_strings_da.pak'
139   inputs.append(os.path.join(
140       GRIT_DIR, '%s_strings_%s.pak' % (BRANDING, locale)))
141
142   # Add any extra input files.
143   for extra_file in EXTRA_INPUT_FILES:
144     inputs.append('%s_%s.pak' % (extra_file, locale))
145
146   return inputs
147
148
149 def list_outputs(locales):
150   """Returns the names of files that will be generated for the given locales.
151
152   This is to provide gyp the list of output files, so build targets can
153   properly track what needs to be built.
154   """
155   outputs = []
156   for locale in locales:
157     outputs.append(calc_output(locale))
158   # Quote each element so filename spaces don't mess up gyp's attempt to parse
159   # it into a list.
160   return " ".join(['"%s"' % x for x in outputs])
161
162
163 def list_inputs(locales):
164   """Returns the names of files that will be processed for the given locales.
165
166   This is to provide gyp the list of input files, so build targets can properly
167   track their prerequisites.
168   """
169   inputs = []
170   for locale in locales:
171     inputs += calc_inputs(locale)
172   # Quote each element so filename spaces don't mess up gyp's attempt to parse
173   # it into a list.
174   return " ".join(['"%s"' % x for x in inputs])
175
176
177 def repack_locales(locales):
178   """ Loop over and repack the given locales."""
179   for locale in locales:
180     inputs = []
181     inputs += calc_inputs(locale)
182     output = calc_output(locale)
183     data_pack.DataPack.RePack(output, inputs, whitelist_file=WHITELIST)
184
185
186 def DoMain(argv):
187   global BRANDING
188   global GRIT_DIR
189   global SHARE_INT_DIR
190   global INT_DIR
191   global OS
192   global CHROMEOS
193   global USE_ASH
194   global USE_ATHENA
195   global WHITELIST
196   global ENABLE_AUTOFILL_DIALOG
197   global ENABLE_EXTENSIONS
198   global EXTRA_INPUT_FILES
199
200   parser = optparse.OptionParser("usage: %prog [options] locales")
201   parser.add_option("-i", action="store_true", dest="inputs", default=False,
202                     help="Print the expected input file list, then exit.")
203   parser.add_option("-o", action="store_true", dest="outputs", default=False,
204                     help="Print the expected output file list, then exit.")
205   parser.add_option("-g", action="store", dest="grit_dir",
206                     help="GRIT build files output directory.")
207   parser.add_option("-x", action="store", dest="int_dir",
208                     help="Intermediate build files output directory.")
209   parser.add_option("-s", action="store", dest="share_int_dir",
210                     help="Shared intermediate build files output directory.")
211   parser.add_option("-b", action="store", dest="branding",
212                     help="Branding type of this build.")
213   parser.add_option("-e", action="append", dest="extra_input", default=[],
214                     help="Full path to an extra input pak file without the\
215                          locale suffix and \".pak\" extension.")
216   parser.add_option("-p", action="store", dest="os",
217                     help="The target OS. (e.g. mac, linux, win, etc.)")
218   parser.add_option("--use-ash", action="store", dest="use_ash",
219                     help="Whether to include ash strings")
220   parser.add_option("--use-athena", action="store", dest="use_athena",
221                     help="Whether to include athena strings")
222   parser.add_option("--chromeos", action="store",
223                     help="Whether building for Chrome OS")
224   parser.add_option("--whitelist", action="store", help="Full path to the "
225                     "whitelist used to filter output pak file resource IDs")
226   parser.add_option("--enable-autofill-dialog", action="store",
227                     dest="enable_autofill_dialog",
228                     help="Whether to include strings for autofill dialog")
229   parser.add_option("--enable-extensions", action="store",
230                     dest="enable_extensions",
231                     help="Whether to include strings for extensions")
232   options, locales = parser.parse_args(argv)
233
234   if not locales:
235     parser.error('Please specificy at least one locale to process.\n')
236
237   print_inputs = options.inputs
238   print_outputs = options.outputs
239   GRIT_DIR = options.grit_dir
240   INT_DIR = options.int_dir
241   SHARE_INT_DIR = options.share_int_dir
242   BRANDING = options.branding
243   EXTRA_INPUT_FILES = options.extra_input
244   OS = options.os
245   CHROMEOS = options.chromeos == '1'
246   USE_ASH = options.use_ash == '1'
247   USE_ATHENA = options.use_athena == '1'
248   WHITELIST = options.whitelist
249   ENABLE_AUTOFILL_DIALOG = options.enable_autofill_dialog == '1'
250   ENABLE_EXTENSIONS = options.enable_extensions == '1'
251
252   if not OS:
253     if sys.platform == 'darwin':
254       OS = 'mac'
255     elif sys.platform.startswith('linux'):
256       OS = 'linux'
257     elif sys.platform in ('cygwin', 'win32'):
258       OS = 'win'
259     else:
260       OS = sys.platform
261
262   if not (GRIT_DIR and INT_DIR and SHARE_INT_DIR):
263     parser.error('Please specify all of "-g" and "-x" and "-s".\n')
264   if print_inputs and print_outputs:
265     parser.error('Please specify only one of "-i" or "-o".\n')
266   # Need to know the branding, unless we're just listing the outputs.
267   if not print_outputs and not BRANDING:
268     parser.error('Please specify "-b" to determine the input files.\n')
269
270   if print_inputs:
271     return list_inputs(locales)
272
273   if print_outputs:
274     return list_outputs(locales)
275
276   return repack_locales(locales)
277
278 if __name__ == '__main__':
279   results = DoMain(sys.argv[1:])
280   if results:
281     print results