Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromecast / tools / build / chromecast_repack_locales.py
blobfec1011a941b4bf49d29ada8c4ad76aa115d0ad6
1 #!/usr/bin/env python
2 # Copyright 2014 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 # Some build paths defined by gyp.
22 GRIT_DIR = None
23 INT_DIR = None
24 CHROMECAST_BRANDING = None
26 class Usage(Exception):
27 def __init__(self, msg):
28 self.msg = msg
31 def calc_output(locale):
32 """Determine the file that will be generated for the given locale."""
33 #e.g. '<(INTERMEDIATE_DIR)/repack/da.pak',
34 # For Fake Bidi, generate it at a fixed path so that tests can safely
35 # reference it.
36 if locale == 'fake-bidi':
37 return '%s/%s.pak' % (INT_DIR, locale)
38 return os.path.join(INT_DIR, locale + '.pak')
41 def calc_inputs(locale):
42 """Determine the files that need processing for the given locale."""
43 inputs = []
44 if CHROMECAST_BRANDING != 'public':
45 inputs.append(os.path.join(GRIT_DIR, 'app_strings_%s.pak' % locale))
46 inputs.append(os.path.join(GRIT_DIR, 'chromecast_settings_%s.pak' % locale))
47 return inputs
50 def list_outputs(locales):
51 """Returns the names of files that will be generated for the given locales.
53 This is to provide gyp the list of output files, so build targets can
54 properly track what needs to be built.
55 """
56 outputs = []
57 for locale in locales:
58 outputs.append(calc_output(locale))
59 # Quote each element so filename spaces don't mess up gyp's attempt to parse
60 # it into a list.
61 return " ".join(['"%s"' % x for x in outputs])
64 def list_inputs(locales):
65 """Returns the names of files that will be processed for the given locales.
67 This is to provide gyp the list of input files, so build targets can properly
68 track their prerequisites.
69 """
70 inputs = []
71 for locale in locales:
72 inputs += calc_inputs(locale)
73 # Quote each element so filename spaces don't mess up gyp's attempt to parse
74 # it into a list.
75 return " ".join(['"%s"' % x for x in inputs])
78 def repack_locales(locales):
79 """ Loop over and repack the given locales."""
80 for locale in locales:
81 inputs = []
82 inputs += calc_inputs(locale)
83 output = calc_output(locale)
84 data_pack.DataPack.RePack(output, inputs)
87 def DoMain(argv):
88 global CHROMECAST_BRANDING
89 global GRIT_DIR
90 global INT_DIR
92 parser = optparse.OptionParser("usage: %prog [options] locales")
93 parser.add_option("-i", action="store_true", dest="inputs", default=False,
94 help="Print the expected input file list, then exit.")
95 parser.add_option("-o", action="store_true", dest="outputs", default=False,
96 help="Print the expected output file list, then exit.")
97 parser.add_option("-g", action="store", dest="grit_dir",
98 help="GRIT build files output directory.")
99 parser.add_option("-x", action="store", dest="int_dir",
100 help="Intermediate build files output directory.")
101 parser.add_option("-b", action="store", dest="chromecast_branding",
102 help="Chromecast branding " +
103 "('public', 'internal' or 'google').")
104 options, locales = parser.parse_args(argv)
106 if not locales:
107 parser.error('Please specificy at least one locale to process.\n')
109 print_inputs = options.inputs
110 print_outputs = options.outputs
111 GRIT_DIR = options.grit_dir
112 INT_DIR = options.int_dir
113 CHROMECAST_BRANDING = options.chromecast_branding
115 if (CHROMECAST_BRANDING != 'public' and
116 CHROMECAST_BRANDING != 'internal' and
117 CHROMECAST_BRANDING != 'google'):
118 parser.error('Chromecast branding (-b) must be ' +
119 '"public", "internal" or "google".\n')
120 if not (GRIT_DIR and INT_DIR):
121 parser.error('Please specify all of "-g" and "-x".\n')
122 if print_inputs and print_outputs:
123 parser.error('Please specify only one of "-i" or "-o".\n')
125 if print_inputs:
126 return list_inputs(locales)
128 if print_outputs:
129 return list_outputs(locales)
131 return repack_locales(locales)
133 if __name__ == '__main__':
134 results = DoMain(sys.argv[1:])
135 if results:
136 print results