remoting_copy_locales.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. """Helper script to repack paks for a list of locales.
  6. Gyp doesn't have any built-in looping capability, so this just provides a way to
  7. loop over a list of locales when repacking pak files, thus avoiding a
  8. proliferation of mostly duplicate, cut-n-paste gyp actions.
  9. """
  10. from __future__ import print_function
  11. import optparse
  12. import os
  13. import sys
  14. # Prepend the grit module from the source tree so it takes precedence over other
  15. # grit versions that might present in the search path.
  16. sys.path.insert(1, os.path.join(os.path.dirname(__file__), '..', '..', '..',
  17. 'tools', 'grit'))
  18. from grit.format import data_pack
  19. # Some build paths defined by gyp.
  20. GRIT_DIR = None
  21. INT_DIR = None
  22. # The target platform. If it is not defined, sys.platform will be used.
  23. OS = None
  24. # Extra input files.
  25. EXTRA_INPUT_FILES = []
  26. class Usage(Exception):
  27. def __init__(self, msg):
  28. self.msg = msg
  29. def calc_output(locale):
  30. """Determine the file that will be generated for the given locale."""
  31. #e.g. '<(INTERMEDIATE_DIR)/remoting_locales/da.pak',
  32. if OS == 'mac' or OS == 'ios':
  33. # For Cocoa to find the locale at runtime, it needs to use '_' instead
  34. # of '-' (http://crbug.com/20441).
  35. return os.path.join(INT_DIR, 'remoting', 'resources',
  36. '%s.lproj' % locale.replace('-', '_'), 'locale.pak')
  37. else:
  38. return os.path.join(INT_DIR, 'remoting_locales', locale + '.pak')
  39. def calc_inputs(locale):
  40. """Determine the files that need processing for the given locale."""
  41. inputs = []
  42. #e.g. '<(grit_out_dir)/remoting/resources/da.pak'
  43. inputs.append(os.path.join(GRIT_DIR, 'remoting/resources/%s.pak' % locale))
  44. # Add any extra input files.
  45. for extra_file in EXTRA_INPUT_FILES:
  46. inputs.append('%s_%s.pak' % (extra_file, locale))
  47. return inputs
  48. def list_outputs(locales):
  49. """Returns the names of files that will be generated for the given locales.
  50. This is to provide gyp the list of output files, so build targets can
  51. properly track what needs to be built.
  52. """
  53. outputs = []
  54. for locale in locales:
  55. outputs.append(calc_output(locale))
  56. # Quote each element so filename spaces don't mess up gyp's attempt to parse
  57. # it into a list.
  58. return " ".join(['"%s"' % x for x in outputs])
  59. def list_inputs(locales):
  60. """Returns the names of files that will be processed for the given locales.
  61. This is to provide gyp the list of input files, so build targets can properly
  62. track their prerequisites.
  63. """
  64. inputs = []
  65. for locale in locales:
  66. inputs += calc_inputs(locale)
  67. # Quote each element so filename spaces don't mess up gyp's attempt to parse
  68. # it into a list.
  69. return " ".join(['"%s"' % x for x in inputs])
  70. def repack_locales(locales):
  71. """ Loop over and repack the given locales."""
  72. for locale in locales:
  73. inputs = calc_inputs(locale)
  74. output = calc_output(locale)
  75. data_pack.RePack(output, inputs)
  76. def DoMain(argv):
  77. global GRIT_DIR
  78. global INT_DIR
  79. global OS
  80. global EXTRA_INPUT_FILES
  81. parser = optparse.OptionParser("usage: %prog [options] locales")
  82. parser.add_option("-i", action="store_true", dest="inputs", default=False,
  83. help="Print the expected input file list, then exit.")
  84. parser.add_option("-o", action="store_true", dest="outputs", default=False,
  85. help="Print the expected output file list, then exit.")
  86. parser.add_option("-g", action="store", dest="grit_dir",
  87. help="GRIT build files output directory.")
  88. parser.add_option("-x", action="store", dest="int_dir",
  89. help="Intermediate build files output directory.")
  90. parser.add_option("-e", action="append", dest="extra_input", default=[],
  91. help="Full path to an extra input pak file without the\
  92. locale suffix and \".pak\" extension.")
  93. parser.add_option("-p", action="store", dest="os",
  94. help="The target OS. (e.g. mac, linux, win, etc.)")
  95. options, locales = parser.parse_args(argv)
  96. if not locales:
  97. parser.error('Please specificy at least one locale to process.\n')
  98. print_inputs = options.inputs
  99. print_outputs = options.outputs
  100. GRIT_DIR = options.grit_dir
  101. INT_DIR = options.int_dir
  102. EXTRA_INPUT_FILES = options.extra_input
  103. OS = options.os
  104. if not OS:
  105. if sys.platform == 'darwin':
  106. OS = 'mac'
  107. elif sys.platform.startswith('linux'):
  108. OS = 'linux'
  109. elif sys.platform in ('cygwin', 'win32'):
  110. OS = 'win'
  111. else:
  112. OS = sys.platform
  113. if print_inputs and print_outputs:
  114. parser.error('Please specify only one of "-i" or "-o".\n')
  115. if print_inputs and not GRIT_DIR:
  116. parser.error('Please specify "-g".\n')
  117. if print_outputs and not INT_DIR:
  118. parser.error('Please specify "-x".\n')
  119. if not (print_inputs or print_outputs or (GRIT_DIR and INT_DIR)):
  120. parser.error('Please specify both "-g" and "-x".\n')
  121. if print_inputs:
  122. return list_inputs(locales)
  123. if print_outputs:
  124. return list_outputs(locales)
  125. return repack_locales(locales)
  126. if __name__ == '__main__':
  127. results = DoMain(sys.argv[1:])
  128. if results:
  129. print(results)