OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Creates a zip archive with policy template files. The list of input files is |
| 6 extracted from a grd file with grit. This is to keep the length of input |
| 7 arguments below the limit on Windows. |
| 8 """ |
| 9 |
| 10 import optparse |
| 11 import os |
| 12 import sys |
| 13 import zipfile |
| 14 |
| 15 def add_files_to_zip(zip_file, base_dir, file_list): |
| 16 """Pack a list of files into a zip archive, that is already |
| 17 opened for writing. |
| 18 |
| 19 Args: |
| 20 zip_file: An object representing the zip archive. |
| 21 base_dir: Base path of all the files in the real file system. |
| 22 files: List of file paths to add, all relative to base_dir. |
| 23 The zip entries will only contain this componenet of the path. |
| 24 """ |
| 25 for file_path in file_list: |
| 26 zip_file.write(base_dir + file_path, file_path) |
| 27 return 0 |
| 28 |
| 29 def get_grd_outputs(grit_cmd, grit_defines, grd_file, grd_strip_path_prefix): |
| 30 grit_path = os.path.join(os.getcwd(), os.path.dirname(grit_cmd)) |
| 31 sys.path.append(grit_path) |
| 32 import grit_info |
| 33 outputs = grit_info.Outputs(grd_file, grit_defines) |
| 34 result = [] |
| 35 for item in outputs: |
| 36 assert item.startswith(grd_strip_path_prefix) |
| 37 result.append(item[len(grd_strip_path_prefix):]) |
| 38 return result |
| 39 |
| 40 def main(argv): |
| 41 """Pack a list of files into a zip archive. |
| 42 |
| 43 Args: |
| 44 zip_path: The file name of the zip archive. |
| 45 base_dir: Base path of input files. |
| 46 locales: The list of locales that are used to generate the list of file |
| 47 names using INPUT_FILES. |
| 48 """ |
| 49 parser = optparse.OptionParser() |
| 50 parser.add_option("--output", dest="output") |
| 51 parser.add_option("--basedir", dest="basedir") |
| 52 parser.add_option("--grit_info", dest="grit_info") |
| 53 parser.add_option("--grd_input", dest="grd_input") |
| 54 parser.add_option("--grd_strip_path_prefix", dest="grd_strip_path_prefix") |
| 55 parser.add_option("--extra_input", action="append", dest="extra_input", |
| 56 default=[]) |
| 57 parser.add_option("-D", action="append", dest="grit_defines", default=[]) |
| 58 parser.add_option("-E", action="append", dest="grit_build_env", default=[]) |
| 59 options, args = parser.parse_args(argv[1:]) |
| 60 |
| 61 if (options.basedir[-1] != '/'): |
| 62 options.basedir += '/' |
| 63 grit_defines = {} |
| 64 for define in options.grit_defines: |
| 65 grit_defines[define] = 1 |
| 66 |
| 67 file_list = options.extra_input |
| 68 file_list += get_grd_outputs(options.grit_info, grit_defines, |
| 69 options.grd_input, options.grd_strip_path_prefix) |
| 70 zip_file = zipfile.ZipFile(options.output, 'w', zipfile.ZIP_DEFLATED) |
| 71 try: |
| 72 return add_files_to_zip(zip_file, options.basedir, file_list) |
| 73 finally: |
| 74 zip_file.close() |
| 75 |
| 76 if '__main__' == __name__: |
| 77 sys.exit(main(sys.argv)) |
| 78 |
OLD | NEW |