1 #!/usr/bin/env python 2 # Copyright (C) 2018 The Android Open Source Project 3 # 4 # Licensed under the Apache License, Version 2.0 (the "License"); 5 # you may not use this file except in compliance with the License. 6 # You may obtain a copy of the License at 7 # 8 # http://www.apache.org/licenses/LICENSE-2.0 9 # 10 # Unless required by applicable law or agreed to in writing, software 11 # distributed under the License is distributed on an "AS IS" BASIS, 12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 # See the License for the specific language governing permissions and 14 # limitations under the License. 15 16 """ Script to list all files in a directory filtering by pattern. 17 18 Do NOT use this script to pull in sources for GN targets. Globbing inputs is 19 a bad idea, as it plays very badly with git leaving untracked files around. This 20 script should be used only for cases where false positives won't affect the 21 output of the build but just cause spurious re-runs (e.g. as input section of 22 an "action" target). 23 """ 24 from __future__ import print_function 25 import argparse 26 import fnmatch 27 import os 28 import sys 29 30 def main(): 31 parser = argparse.ArgumentParser() 32 parser.add_argument('--filter', default=[], action='append') 33 parser.add_argument('--exclude', default=[], action='append') 34 parser.add_argument('--deps', default=None) 35 parser.add_argument('--output', default=None) 36 parser.add_argument('--root', required=True) 37 args = parser.parse_args() 38 39 fout = open(args.output, 'w') if args.output else sys.stdout 40 def writepath(path): 41 if args.deps: 42 path = '\t' + path 43 print(path, file=fout) 44 45 root = args.root 46 if not root.endswith('/'): 47 root += '/' 48 if not os.path.exists(root): 49 return 0 50 51 if args.deps: 52 print(args.deps + ':', file=fout) 53 for pardir, dirs, files in os.walk(root, topdown=True): 54 assert pardir.startswith(root) 55 relpar = pardir[len(root):] 56 dirs[:] = [d for d in dirs if os.path.join(relpar, d) not in args.exclude] 57 for fname in files: 58 fpath = os.path.join(pardir, fname) 59 match = len(args.filter) == 0 60 for filter in args.filter: 61 if fnmatch.fnmatch(fpath, filter): 62 match = True 63 break 64 if match: 65 writepath(fpath) 66 67 if __name__ == '__main__': 68 sys.exit(main()) 69