Home | History | Annotate | Download | only in tools
      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.
      5 
      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 """
     10 
     11 import optparse
     12 import os
     13 import sys
     14 import zipfile
     15 
     16 
     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.
     20 
     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
     30 
     31 
     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):
     46   """Pack a list of files into a zip archive.
     47 
     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:])
     65 
     66   if (options.basedir[-1] != '/'):
     67     options.basedir += '/'
     68   grit_defines = {}
     69   for define in options.grit_defines:
     70     grit_defines[define] = 1
     71 
     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()
     80 
     81 
     82 if '__main__' == __name__:
     83   sys.exit(main(sys.argv))
     84