Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / tools / build / repack_locales.py
blob96f47eb7df93110591e9871636151788020c6862
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.
6 """Helper script to repack paks for a list of locales.
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 """
13 import optparse
14 import os
15 import sys
17 sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..',
18 'tools', 'grit'))
19 from grit.format import data_pack
21 # The gyp "branding" variable.
22 BRANDING = None
24 # Some build paths defined by gyp.
25 GRIT_DIR = None
26 SHARE_INT_DIR = None
27 INT_DIR = None
29 # The target platform. If it is not defined, sys.platform will be used.
30 OS = None
32 USE_ASH = False
34 # Extra input files.
35 EXTRA_INPUT_FILES = []
37 class Usage(Exception):
38 def __init__(self, msg):
39 self.msg = msg
42 def calc_output(locale):
43 """Determine the file that will be generated for the given locale."""
44 #e.g. '<(INTERMEDIATE_DIR)/repack/da.pak',
45 # For Fake Bidi, generate it at a fixed path so that tests can safely
46 # reference it.
47 if locale == 'fake-bidi':
48 return '%s/%s.pak' % (INT_DIR, locale)
49 if OS == 'mac' or OS == 'ios':
50 # For Cocoa to find the locale at runtime, it needs to use '_' instead
51 # of '-' (http://crbug.com/20441). Also, 'en-US' should be represented
52 # simply as 'en' (http://crbug.com/19165, http://crbug.com/25578).
53 if locale == 'en-US':
54 locale = 'en'
55 return '%s/repack/%s.lproj/locale.pak' % (INT_DIR, locale.replace('-', '_'))
56 else:
57 return os.path.join(INT_DIR, 'repack', locale + '.pak')
60 def calc_inputs(locale):
61 """Determine the files that need processing for the given locale."""
62 inputs = []
64 #e.g. '<(grit_out_dir)/generated_resources_da.pak'
65 inputs.append(os.path.join(GRIT_DIR, 'generated_resources_%s.pak' % locale))
67 #e.g. '<(grit_out_dir)/locale_settings_da.pak'
68 inputs.append(os.path.join(GRIT_DIR, 'locale_settings_%s.pak' % locale))
70 #e.g. '<(grit_out_dir)/platform_locale_settings_da.pak'
71 inputs.append(os.path.join(GRIT_DIR,
72 'platform_locale_settings_%s.pak' % locale))
74 #e.g. '<(SHARED_INTERMEDIATE_DIR)/components/strings/
75 # component_strings_da.pak',
76 inputs.append(os.path.join(SHARE_INT_DIR, 'components', 'strings',
77 'component_strings_%s.pak' % locale))
79 if USE_ASH:
80 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ash_strings/ash_strings_da.pak',
81 inputs.append(os.path.join(SHARE_INT_DIR, 'ash_strings',
82 'ash_strings_%s.pak' % locale))
84 if OS != 'ios':
85 #e.g. '<(SHARED_INTERMEDIATE_DIR)/webkit/webkit_strings_da.pak'
86 inputs.append(os.path.join(SHARE_INT_DIR, 'webkit',
87 'webkit_strings_%s.pak' % locale))
89 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ui/ui_strings_da.pak',
90 inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'ui_strings',
91 'ui_strings_%s.pak' % locale))
93 #e.g. '<(SHARED_INTERMEDIATE_DIR)/device/bluetooth/strings/
94 # device_bluetooth_strings_da.pak',
95 inputs.append(os.path.join(SHARE_INT_DIR, 'device', 'bluetooth', 'strings',
96 'device_bluetooth_strings_%s.pak' % locale))
98 #e.g. '<(SHARED_INTERMEDIATE_DIR)/ui/app_locale_settings_da.pak',
99 inputs.append(os.path.join(SHARE_INT_DIR, 'ui', 'app_locale_settings',
100 'app_locale_settings_%s.pak' % locale))
103 if OS != 'ios' and OS != 'android':
104 #e.g. '<(SHARED_INTERMEDIATE_DIR)/third_party/libaddressinput/
105 # libaddressinput_strings_da.pak',
106 inputs.append(os.path.join(SHARE_INT_DIR, 'third_party', 'libaddressinput',
107 'libaddressinput_strings_%s.pak' % locale))
109 #e.g. '<(grit_out_dir)/google_chrome_strings_da.pak'
110 # or
111 # '<(grit_out_dir)/chromium_strings_da.pak'
112 inputs.append(os.path.join(
113 GRIT_DIR, '%s_strings_%s.pak' % (BRANDING, locale)))
115 # Add any extra input files.
116 for extra_file in EXTRA_INPUT_FILES:
117 inputs.append('%s_%s.pak' % (extra_file, locale))
119 return inputs
122 def list_outputs(locales):
123 """Returns the names of files that will be generated for the given locales.
125 This is to provide gyp the list of output files, so build targets can
126 properly track what needs to be built.
128 outputs = []
129 for locale in locales:
130 outputs.append(calc_output(locale))
131 # Quote each element so filename spaces don't mess up gyp's attempt to parse
132 # it into a list.
133 return " ".join(['"%s"' % x for x in outputs])
136 def list_inputs(locales):
137 """Returns the names of files that will be processed for the given locales.
139 This is to provide gyp the list of input files, so build targets can properly
140 track their prerequisites.
142 inputs = []
143 for locale in locales:
144 inputs += calc_inputs(locale)
145 # Quote each element so filename spaces don't mess up gyp's attempt to parse
146 # it into a list.
147 return " ".join(['"%s"' % x for x in inputs])
150 def repack_locales(locales):
151 """ Loop over and repack the given locales."""
152 for locale in locales:
153 inputs = []
154 inputs += calc_inputs(locale)
155 output = calc_output(locale)
156 data_pack.DataPack.RePack(output, inputs)
159 def DoMain(argv):
160 global BRANDING
161 global GRIT_DIR
162 global SHARE_INT_DIR
163 global INT_DIR
164 global OS
165 global USE_ASH
166 global EXTRA_INPUT_FILES
168 parser = optparse.OptionParser("usage: %prog [options] locales")
169 parser.add_option("-i", action="store_true", dest="inputs", default=False,
170 help="Print the expected input file list, then exit.")
171 parser.add_option("-o", action="store_true", dest="outputs", default=False,
172 help="Print the expected output file list, then exit.")
173 parser.add_option("-g", action="store", dest="grit_dir",
174 help="GRIT build files output directory.")
175 parser.add_option("-x", action="store", dest="int_dir",
176 help="Intermediate build files output directory.")
177 parser.add_option("-s", action="store", dest="share_int_dir",
178 help="Shared intermediate build files output directory.")
179 parser.add_option("-b", action="store", dest="branding",
180 help="Branding type of this build.")
181 parser.add_option("-e", action="append", dest="extra_input", default=[],
182 help="Full path to an extra input pak file without the\
183 locale suffix and \".pak\" extension.")
184 parser.add_option("-p", action="store", dest="os",
185 help="The target OS. (e.g. mac, linux, win, etc.)")
186 parser.add_option("--use-ash", action="store", dest="use_ash",
187 help="Whether to include ash strings")
188 options, locales = parser.parse_args(argv)
190 if not locales:
191 parser.error('Please specificy at least one locale to process.\n')
193 print_inputs = options.inputs
194 print_outputs = options.outputs
195 GRIT_DIR = options.grit_dir
196 INT_DIR = options.int_dir
197 SHARE_INT_DIR = options.share_int_dir
198 BRANDING = options.branding
199 EXTRA_INPUT_FILES = options.extra_input
200 OS = options.os
201 USE_ASH = options.use_ash == '1'
203 if not OS:
204 if sys.platform == 'darwin':
205 OS = 'mac'
206 elif sys.platform.startswith('linux'):
207 OS = 'linux'
208 elif sys.platform in ('cygwin', 'win32'):
209 OS = 'win'
210 else:
211 OS = sys.platform
213 if not (GRIT_DIR and INT_DIR and SHARE_INT_DIR):
214 parser.error('Please specify all of "-g" and "-x" and "-s".\n')
215 if print_inputs and print_outputs:
216 parser.error('Please specify only one of "-i" or "-o".\n')
217 # Need to know the branding, unless we're just listing the outputs.
218 if not print_outputs and not BRANDING:
219 parser.error('Please specify "-b" to determine the input files.\n')
221 if print_inputs:
222 return list_inputs(locales)
224 if print_outputs:
225 return list_outputs(locales)
227 return repack_locales(locales)
229 if __name__ == '__main__':
230 results = DoMain(sys.argv[1:])
231 if results:
232 print results