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