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 shutil 9import sys 10 11from util import build_utils 12 13 14_RESOURCE_CLASSES = [ 15 "R.class", 16 "R##*.class", 17 "Manifest.class", 18 "Manifest##*.class", 19] 20 21 22def CreatePathTransform(exclude_globs, include_globs, 23 strip_resource_classes_for): 24 """Returns a function to strip paths for the given patterns. 25 26 Args: 27 exclude_globs: List of globs that if matched should be excluded. 28 include_globs: List of globs that if not matched should be excluded. 29 strip_resource_classes_for: List of Java packages for which to strip 30 R.java classes from. 31 32 Returns: 33 * None if no filters are needed. 34 * A function "(path) -> path" that returns None when |path| should be 35 stripped, or |path| otherwise. 36 """ 37 if not (exclude_globs or include_globs or strip_resource_classes_for): 38 return None 39 exclude_globs = list(exclude_globs or []) 40 if strip_resource_classes_for: 41 exclude_globs.extend(p.replace('.', '/') + '/' + f 42 for p in strip_resource_classes_for 43 for f in _RESOURCE_CLASSES) 44 def path_transform(path): 45 # Exclude filters take precidence over include filters. 46 if build_utils.MatchesGlob(path, exclude_globs): 47 return None 48 if include_globs and not build_utils.MatchesGlob(path, include_globs): 49 return None 50 return path 51 52 return path_transform 53 54 55def main(): 56 parser = argparse.ArgumentParser() 57 parser.add_argument('--input', required=True, 58 help='Input zip file.') 59 parser.add_argument('--output', required=True, 60 help='Output zip file') 61 parser.add_argument('--exclude-globs', 62 help='GN list of exclude globs') 63 parser.add_argument('--include-globs', 64 help='GN list of include globs') 65 parser.add_argument('--strip-resource-classes-for', 66 help='GN list of java package names exclude R.class files in.') 67 68 argv = build_utils.ExpandFileArgs(sys.argv[1:]) 69 args = parser.parse_args(argv) 70 71 args.exclude_globs = build_utils.ParseGnList(args.exclude_globs) 72 args.include_globs = build_utils.ParseGnList(args.include_globs) 73 args.strip_resource_classes_for = build_utils.ParseGnList( 74 args.strip_resource_classes_for) 75 76 path_transform = CreatePathTransform(args.exclude_globs, args.include_globs, 77 args.strip_resource_classes_for) 78 with build_utils.AtomicOutput(args.output) as f: 79 if path_transform: 80 build_utils.MergeZips(f.name, [args.input], path_transform=path_transform) 81 else: 82 shutil.copy(args.input, f.name) 83 84 85if __name__ == '__main__': 86 main() 87