2 # Copyright 2013 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.
17 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
), '..', '..', '..',
19 from grit
.format
import data_pack
21 # Some build paths defined by gyp.
25 # The target platform. If it is not defined, sys.platform will be used.
29 EXTRA_INPUT_FILES
= []
31 class Usage(Exception):
32 def __init__(self
, msg
):
36 def calc_output(locale
):
37 """Determine the file that will be generated for the given locale."""
38 #e.g. '<(INTERMEDIATE_DIR)/remoting_locales/da.pak',
39 if OS
== 'mac' or OS
== 'ios':
40 # For Cocoa to find the locale at runtime, it needs to use '_' instead
41 # of '-' (http://crbug.com/20441).
42 return os
.path
.join(INT_DIR
, 'remoting', 'resources',
43 '%s.lproj' % locale
.replace('-', '_'), 'locale.pak')
45 return os
.path
.join(INT_DIR
, 'remoting_locales', locale
+ '.pak')
48 def calc_inputs(locale
):
49 """Determine the files that need processing for the given locale."""
52 #e.g. '<(grit_out_dir)/remoting/resources/da.pak'
53 inputs
.append(os
.path
.join(GRIT_DIR
, 'remoting/resources/%s.pak' % locale
))
55 # Add any extra input files.
56 for extra_file
in EXTRA_INPUT_FILES
:
57 inputs
.append('%s_%s.pak' % (extra_file
, locale
))
62 def list_outputs(locales
):
63 """Returns the names of files that will be generated for the given locales.
65 This is to provide gyp the list of output files, so build targets can
66 properly track what needs to be built.
69 for locale
in locales
:
70 outputs
.append(calc_output(locale
))
71 # Quote each element so filename spaces don't mess up gyp's attempt to parse
73 return " ".join(['"%s"' % x
for x
in outputs
])
76 def list_inputs(locales
):
77 """Returns the names of files that will be processed for the given locales.
79 This is to provide gyp the list of input files, so build targets can properly
80 track their prerequisites.
83 for locale
in locales
:
84 inputs
+= calc_inputs(locale
)
85 # Quote each element so filename spaces don't mess up gyp's attempt to parse
87 return " ".join(['"%s"' % x
for x
in inputs
])
90 def repack_locales(locales
):
91 """ Loop over and repack the given locales."""
92 for locale
in locales
:
93 inputs
= calc_inputs(locale
)
94 output
= calc_output(locale
)
95 data_pack
.DataPack
.RePack(output
, inputs
)
102 global EXTRA_INPUT_FILES
104 parser
= optparse
.OptionParser("usage: %prog [options] locales")
105 parser
.add_option("-i", action
="store_true", dest
="inputs", default
=False,
106 help="Print the expected input file list, then exit.")
107 parser
.add_option("-o", action
="store_true", dest
="outputs", default
=False,
108 help="Print the expected output file list, then exit.")
109 parser
.add_option("-g", action
="store", dest
="grit_dir",
110 help="GRIT build files output directory.")
111 parser
.add_option("-x", action
="store", dest
="int_dir",
112 help="Intermediate build files output directory.")
113 parser
.add_option("-e", action
="append", dest
="extra_input", default
=[],
114 help="Full path to an extra input pak file without the\
115 locale suffix and \".pak\" extension.")
116 parser
.add_option("-p", action
="store", dest
="os",
117 help="The target OS. (e.g. mac, linux, win, etc.)")
118 options
, locales
= parser
.parse_args(argv
)
121 parser
.error('Please specificy at least one locale to process.\n')
123 print_inputs
= options
.inputs
124 print_outputs
= options
.outputs
125 GRIT_DIR
= options
.grit_dir
126 INT_DIR
= options
.int_dir
127 EXTRA_INPUT_FILES
= options
.extra_input
131 if sys
.platform
== 'darwin':
133 elif sys
.platform
.startswith('linux'):
135 elif sys
.platform
in ('cygwin', 'win32'):
140 if print_inputs
and print_outputs
:
141 parser
.error('Please specify only one of "-i" or "-o".\n')
142 if print_inputs
and not GRIT_DIR
:
143 parser
.error('Please specify "-g".\n')
144 if print_outputs
and not INT_DIR
:
145 parser
.error('Please specify "-x".\n')
146 if not (print_inputs
or print_outputs
or (GRIT_DIR
and INT_DIR
)):
147 parser
.error('Please specify both "-g" and "-x".\n')
150 return list_inputs(locales
)
153 return list_outputs(locales
)
155 return repack_locales(locales
)
157 if __name__
== '__main__':
158 results
= DoMain(sys
.argv
[1:])