Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / policy / tools / make_policy_zip.py
blob88fdd26372e20831ebed527027b40d14e64906c3
1 #!/usr/bin/env python
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
4 # found in the LICENSE file.
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
8 arguments below the limit on Windows.
9 """
11 import optparse
12 import os
13 import sys
14 import zipfile
17 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 opened for writing.
21 Args:
22 zip_file: An object representing the zip archive.
23 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 The zip entries will only contain this componenet of the path.
26 """
27 for file_path in file_list:
28 zip_file.write(base_dir + file_path, file_path)
29 return 0
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
45 def main(argv):
46 """Pack a list of files into a zip archive.
48 Args:
49 zip_path: The file name of the zip archive.
50 base_dir: Base path of input files.
51 locales: The list of locales that are used to generate the list of file
52 names using INPUT_FILES.
53 """
54 parser = optparse.OptionParser()
55 parser.add_option("--output", dest="output")
56 parser.add_option("--basedir", dest="basedir")
57 parser.add_option("--grit_info", dest="grit_info")
58 parser.add_option("--grd_input", dest="grd_input")
59 parser.add_option("--grd_strip_path_prefix", dest="grd_strip_path_prefix")
60 parser.add_option("--extra_input", action="append", dest="extra_input",
61 default=[])
62 parser.add_option("-D", action="append", dest="grit_defines", default=[])
63 parser.add_option("-E", action="append", dest="grit_build_env", default=[])
64 options, args = parser.parse_args(argv[1:])
66 if (options.basedir[-1] != '/'):
67 options.basedir += '/'
68 grit_defines = {}
69 for define in options.grit_defines:
70 grit_defines[define] = 1
72 file_list = options.extra_input
73 file_list += get_grd_outputs(options.grit_info, grit_defines,
74 options.grd_input, options.grd_strip_path_prefix)
75 zip_file = zipfile.ZipFile(options.output, 'w', zipfile.ZIP_DEFLATED)
76 try:
77 return add_files_to_zip(zip_file, options.basedir, file_list)
78 finally:
79 zip_file.close()
82 if '__main__' == __name__:
83 sys.exit(main(sys.argv))