1#!/usr/bin/env python
2#
3# Copyright 2018 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import argparse
8import sys
9
10from util import build_utils
11
12
13_RESOURCE_CLASSES = [
14    "R.class",
15    "R##*.class",
16    "Manifest.class",
17    "Manifest##*.class",
18]
19
20
21def CreatePathTransform(exclude_globs, include_globs,
22                        strip_resource_classes_for):
23  exclude_globs = list(exclude_globs or [])
24  if strip_resource_classes_for:
25    exclude_globs.extend(p.replace('.', '/') + '/' + f
26                         for p in strip_resource_classes_for
27                         for f in _RESOURCE_CLASSES)
28  def path_transform(path):
29    # Exclude filters take precidence over include filters.
30    if build_utils.MatchesGlob(path, exclude_globs):
31      return None
32    if include_globs and not build_utils.MatchesGlob(path, include_globs):
33      return None
34    return path
35
36  return path_transform
37
38
39def main():
40  parser = argparse.ArgumentParser()
41  parser.add_argument('--input', required=True,
42      help='Input zip file.')
43  parser.add_argument('--output', required=True,
44      help='Output zip file')
45  parser.add_argument('--exclude-globs',
46      help='GN list of exclude globs')
47  parser.add_argument('--include-globs',
48      help='GN list of include globs')
49  parser.add_argument('--strip-resource-classes-for',
50      help='GN list of java package names exclude R.class files in.')
51
52  argv = build_utils.ExpandFileArgs(sys.argv[1:])
53  args = parser.parse_args(argv)
54
55  if args.exclude_globs:
56    args.exclude_globs = build_utils.ParseGnList(args.exclude_globs)
57  if args.include_globs:
58    args.include_globs= build_utils.ParseGnList(args.include_globs)
59  if args.strip_resource_classes_for:
60    args.strip_resource_classes_for = build_utils.ParseGnList(
61        args.strip_resource_classes_for)
62
63  path_transform = CreatePathTransform(args.exclude_globs, args.include_globs,
64                                       args.strip_resource_classes_for)
65  with build_utils.AtomicOutput(args.output) as f:
66    build_utils.MergeZips(
67        f.name, [args.input], path_transform=path_transform)
68
69
70if __name__ == '__main__':
71  main()
72