• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
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.
5
6"""Helper script to repack paks for a list of locales.
7
8Gyp doesn't have any built-in looping capability, so this just provides a way to
9loop over a list of locales when repacking pak files, thus avoiding a
10proliferation of mostly duplicate, cut-n-paste gyp actions.
11"""
12
13import optparse
14import os
15import sys
16
17sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..',
18                             'tools', 'grit'))
19from grit.format import data_pack
20
21# Some build paths defined by gyp.
22GRIT_DIR = None
23INT_DIR = None
24
25# The target platform. If it is not defined, sys.platform will be used.
26OS = None
27
28# Extra input files.
29EXTRA_INPUT_FILES = []
30
31class Usage(Exception):
32  def __init__(self, msg):
33    self.msg = msg
34
35
36def 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')
44  else:
45    return os.path.join(INT_DIR, 'remoting_locales', locale + '.pak')
46
47
48def calc_inputs(locale):
49  """Determine the files that need processing for the given locale."""
50  inputs = []
51
52  #e.g. '<(grit_out_dir)/remoting/resources/da.pak'
53  inputs.append(os.path.join(GRIT_DIR, 'remoting/resources/%s.pak' % locale))
54
55  # Add any extra input files.
56  for extra_file in EXTRA_INPUT_FILES:
57    inputs.append('%s_%s.pak' % (extra_file, locale))
58
59  return inputs
60
61
62def list_outputs(locales):
63  """Returns the names of files that will be generated for the given locales.
64
65  This is to provide gyp the list of output files, so build targets can
66  properly track what needs to be built.
67  """
68  outputs = []
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
72  # it into a list.
73  return " ".join(['"%s"' % x for x in outputs])
74
75
76def list_inputs(locales):
77  """Returns the names of files that will be processed for the given locales.
78
79  This is to provide gyp the list of input files, so build targets can properly
80  track their prerequisites.
81  """
82  inputs = []
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
86  # it into a list.
87  return " ".join(['"%s"' % x for x in inputs])
88
89
90def 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)
96
97
98def DoMain(argv):
99  global GRIT_DIR
100  global INT_DIR
101  global OS
102  global EXTRA_INPUT_FILES
103
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)
119
120  if not locales:
121    parser.error('Please specificy at least one locale to process.\n')
122
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
128  OS = options.os
129
130  if not OS:
131    if sys.platform == 'darwin':
132      OS = 'mac'
133    elif sys.platform.startswith('linux'):
134      OS = 'linux'
135    elif sys.platform in ('cygwin', 'win32'):
136      OS = 'win'
137    else:
138      OS = sys.platform
139
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')
148
149  if print_inputs:
150    return list_inputs(locales)
151
152  if print_outputs:
153    return list_outputs(locales)
154
155  return repack_locales(locales)
156
157if __name__ == '__main__':
158  results = DoMain(sys.argv[1:])
159  if results:
160    print results
161