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