OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
2 # Copyright 2015 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 for today extension. | |
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 script_dir = os.path.dirname(__file__) | |
18 src_dir = os.path.join(script_dir, os.pardir, os.pardir, os.pardir, os.pardir) | |
19 sys.path.append(os.path.join(src_dir, 'tools', 'grit')) | |
20 | |
21 from grit.format import data_pack | |
22 | |
23 | |
24 def calc_output(options, locale): | |
25 """Determine the file that will be generated for the given locale.""" | |
26 #e.g. '<(INTERMEDIATE_DIR)/repack/da.pak', | |
27 # For Fake Bidi, generate it at a fixed path so that tests can safely | |
28 # reference it. | |
29 if locale == 'fake-bidi': | |
30 return os.path.join(options.share_int_dir, locale + '.pak') | |
31 # For Cocoa to find the locale at runtime, it needs to use '_' instead | |
32 # of '-' (http://crbug.com/20441). Also, 'en-US' should be represented | |
33 # simply as 'en' (http://crbug.com/19165, http://crbug.com/25578). | |
34 if locale == 'en-US': | |
35 locale = 'en' | |
36 else: | |
37 locale = locale.replace('-', '_') | |
38 return os.path.join(options.out_dir, locale + '.lproj', 'locale.pak') | |
39 | |
40 | |
41 def calc_inputs(options, locale): | |
42 """Determine the files that need processing for the given locale.""" | |
43 inputs = [] | |
44 | |
45 #e.g. 'out/Debug/gen/ios/today_extension/ios_today_extension_strings_da.pak' | |
46 inputs.append(os.path.join(options.share_int_dir, 'ios', 'today_extension', | |
47 'ios_today_extension_strings_%s.pak' % (locale))) | |
48 | |
49 # Add any extra input files. | |
50 for extra_file in options.extra_input: | |
51 inputs.append('%s_%s.pak' % (extra_file, locale)) | |
52 | |
53 return inputs | |
54 | |
55 | |
56 def list_outputs(options, locales): | |
57 """Returns the names of files that will be generated for the given locales. | |
58 | |
59 This is to provide gyp the list of output files, so build targets can | |
60 properly track what needs to be built. | |
61 """ | |
62 outputs = [] | |
63 for locale in locales: | |
64 outputs.append(calc_output(options, locale)) | |
65 return outputs | |
66 | |
67 | |
68 def list_inputs(options, locales): | |
69 """Returns the names of files that will be processed for the given locales. | |
70 | |
71 This is to provide gyp the list of input files, so build targets can properly | |
72 track their prerequisites. | |
73 """ | |
74 inputs = [] | |
75 for locale in locales: | |
76 inputs.extend(calc_inputs(options, locale)) | |
77 return inputs | |
78 | |
79 | |
80 def quote_filenames(filenames): | |
81 """Quote each elements so filename spaces don't mess up gyp's attempt to parse | |
82 it into a list.""" | |
83 return " ".join(['"%s"' % x for x in filenames]) | |
84 | |
85 | |
86 def repack_locales(options, locales): | |
87 """ Loop over and repack the given locales.""" | |
88 for locale in locales: | |
89 inputs = calc_inputs(options, locale) | |
90 output = calc_output(options, locale) | |
91 data_pack.DataPack.RePack(output, inputs, whitelist_file=options.whitelist) | |
92 | |
93 | |
94 def DoMain(argv): | |
95 parser = optparse.OptionParser("usage: %prog [options] locales") | |
96 parser.add_option( | |
97 "-i", action="store_true", dest="print_inputs", default=False, | |
98 help="Print the expected input file list, then exit.") | |
99 parser.add_option( | |
100 "-o", action="store_true", dest="print_outputs", default=False, | |
101 help="Print the expected output file list, then exit.") | |
102 parser.add_option( | |
103 "-x", action="store", dest="out_dir", | |
104 help="Intermediate build files output directory.") | |
105 parser.add_option( | |
106 "-s", action="store", dest="share_int_dir", | |
107 help="Shared intermediate build files output directory.") | |
108 parser.add_option( | |
109 "-b", action="store", dest="branding", | |
110 help="Branding type of this build.") | |
111 parser.add_option( | |
112 "-e", action="append", dest="extra_input", default=[], | |
113 help="Full path to an extra input pak file without the " | |
114 "locale suffix and \".pak\" extension.") | |
115 parser.add_option( | |
116 "--whitelist", action="store", help="Full path to the " | |
117 "whitelist used to filter output pak file resource IDs") | |
118 options, locales = parser.parse_args(argv) | |
119 | |
120 if not locales: | |
121 parser.error('Please specificy at least one locale to process.\n') | |
122 | |
123 if not (options.out_dir and options.share_int_dir): | |
124 parser.error('Please specify all of "-x" and "-s".\n') | |
125 if options.print_inputs and options.print_outputs: | |
126 parser.error('Please specify only one of "-i" or "-o".\n') | |
127 # Need to know the branding, unless we're just listing the outputs. | |
128 if not options.print_outputs and not options.branding: | |
129 parser.error('Please specify "-b" to determine the input files.\n') | |
130 | |
131 if options.print_inputs: | |
132 return quote_filenames(list_inputs(options, locales)) | |
133 | |
134 if options.print_outputs: | |
135 return quote_filenames(list_outputs(options, locales)) | |
136 | |
137 return repack_locales(options, locales) | |
138 | |
139 if __name__ == '__main__': | |
140 results = DoMain(sys.argv[1:]) | |
141 if results: | |
142 print results | |
OLD | NEW |