1#!/usr/bin/env python3 2# 3# Copyright 2014 The Chromium Authors 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6"""Archives a set of files.""" 7 8import argparse 9import json 10import os 11import sys 12import zipfile 13 14from util import build_utils 15import action_helpers # build_utils adds //build to sys.path. 16import zip_helpers 17 18 19def main(args): 20 args = build_utils.ExpandFileArgs(args) 21 parser = argparse.ArgumentParser(args) 22 parser.add_argument('--input-files', help='GN-list of files to zip.') 23 parser.add_argument( 24 '--input-files-base-dir', 25 help='Paths in the archive will be relative to this directory') 26 parser.add_argument('--input-zips', help='GN-list of zips to merge.') 27 parser.add_argument( 28 '--input-zips-excluded-globs', 29 help='GN-list of globs for paths to exclude.') 30 parser.add_argument('--output', required=True, help='Path to output archive.') 31 compress_group = parser.add_mutually_exclusive_group() 32 compress_group.add_argument( 33 '--compress', action='store_true', help='Compress entries') 34 compress_group.add_argument( 35 '--no-compress', 36 action='store_false', 37 dest='compress', 38 help='Do not compress entries') 39 parser.add_argument('--comment-json', 40 action='append', 41 metavar='KEY=VALUE', 42 type=lambda x: x.split('=', 1), 43 help='Entry to store in JSON-encoded archive comment.') 44 action_helpers.add_depfile_arg(parser) 45 options = parser.parse_args(args) 46 47 with action_helpers.atomic_output(options.output) as f: 48 with zipfile.ZipFile(f.name, 'w') as out_zip: 49 depfile_deps = None 50 if options.input_files: 51 files = action_helpers.parse_gn_list(options.input_files) 52 zip_helpers.add_files_to_zip(files, 53 out_zip, 54 base_dir=options.input_files_base_dir, 55 compress=options.compress) 56 57 if options.input_zips: 58 files = action_helpers.parse_gn_list(options.input_zips) 59 depfile_deps = files 60 path_transform = None 61 if options.input_zips_excluded_globs: 62 globs = action_helpers.parse_gn_list( 63 options.input_zips_excluded_globs) 64 path_transform = ( 65 lambda p: None if build_utils.MatchesGlob(p, globs) else p) 66 zip_helpers.merge_zips(out_zip, 67 files, 68 path_transform=path_transform, 69 compress=options.compress) 70 71 if options.comment_json: 72 out_zip.comment = json.dumps(dict(options.comment_json), 73 sort_keys=True).encode('utf-8') 74 75 # Depfile used only by dist_jar(). 76 if options.depfile: 77 action_helpers.write_depfile(options.depfile, 78 options.output, 79 inputs=depfile_deps) 80 81 82if __name__ == '__main__': 83 main(sys.argv[1:]) 84