Merge "Add --dir option to findleaves.py"

am: 40623e2a99

* commit '40623e2a995247907028b12538a2732ac107a057':
  Add --dir option to findleaves.py

Change-Id: I6708e21ca14a5d4e7218c792b6bc6ee0fd6818e8
This commit is contained in:
Colin Cross
2016-05-28 01:01:43 +00:00
committed by android-build-merger

View File

@@ -23,7 +23,7 @@
import os import os
import sys import sys
def perform_find(mindepth, prune, dirlist, filename): def perform_find(mindepth, prune, dirlist, filenames):
result = [] result = []
pruneleaves = set(map(lambda x: os.path.split(x)[1], prune)) pruneleaves = set(map(lambda x: os.path.split(x)[1], prune))
for rootdir in dirlist: for rootdir in dirlist:
@@ -48,19 +48,24 @@ def perform_find(mindepth, prune, dirlist, filename):
if depth < mindepth: if depth < mindepth:
continue continue
# match # match
for filename in filenames:
if filename in files: if filename in files:
result.append(os.path.join(root, filename)) result.append(os.path.join(root, filename))
del dirs[:] del dirs[:]
return result return result
def usage(): def usage():
sys.stderr.write("""Usage: %(progName)s [<options>] <dirlist> <filename> sys.stderr.write("""Usage: %(progName)s [<options>] [--dir=<dir>] <filenames>
Options: Options:
--mindepth=<mindepth> --mindepth=<mindepth>
Both behave in the same way as their find(1) equivalents. Both behave in the same way as their find(1) equivalents.
--prune=<dirname> --prune=<dirname>
Avoids returning results from inside any directory called <dirname> Avoids returning results from inside any directory called <dirname>
(e.g., "*/out/*"). May be used multiple times. (e.g., "*/out/*"). May be used multiple times.
--dir=<dir>
Add a directory to search. May be repeated multiple times. For backwards
compatibility, if no --dir argument is provided then all but the last entry
in <filenames> are treated as directories.
""" % { """ % {
"progName": os.path.split(sys.argv[0])[1], "progName": os.path.split(sys.argv[0])[1],
}) })
@@ -69,6 +74,7 @@ Options:
def main(argv): def main(argv):
mindepth = -1 mindepth = -1
prune = [] prune = []
dirlist = []
i=1 i=1
while i<len(argv) and len(argv[i])>2 and argv[i][0:2] == "--": while i<len(argv) and len(argv[i])>2 and argv[i][0:2] == "--":
arg = argv[i] arg = argv[i]
@@ -82,14 +88,24 @@ def main(argv):
if len(p) == 0: if len(p) == 0:
usage() usage()
prune.append(p) prune.append(p)
elif arg.startswith("--dir="):
d = arg[len("--dir="):]
if len(p) == 0:
usage()
dirlist.append(d)
else: else:
usage() usage()
i += 1 i += 1
if len(dirlist) == 0: # backwards compatibility
if len(argv)-i < 2: # need both <dirlist> and <filename> if len(argv)-i < 2: # need both <dirlist> and <filename>
usage() usage()
dirlist = argv[i:-1] dirlist = argv[i:-1]
filename = argv[-1] filenames = [argv[-1]]
results = list(set(perform_find(mindepth, prune, dirlist, filename))) else:
if len(argv)-i < 1: # need <filename>
usage()
filenames = argv[i:]
results = list(set(perform_find(mindepth, prune, dirlist, filenames)))
results.sort() results.sort()
for r in results: for r in results:
print r print r