3 # Copyright (C) 2009 The Android Open Source Project
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
19 # Finds files with the specified name under a particular directory, stopping
20 # the search in a given subdirectory when the file is found.
26 def perform_find(mindepth, prune, dirlist, filename):
28 pruneleaves = set(map(lambda x: os.path.split(x)[1], prune))
29 for rootdir in dirlist:
30 rootdepth = rootdir.count("/")
31 for root, dirs, files in os.walk(rootdir, followlinks=True):
47 depth = 1 + root.count("/") - rootdepth
52 result.append(os.path.join(root, filename))
57 sys.stderr.write("""Usage: %(progName)s [<options>] <dirlist> <filename>
60 Both behave in the same way as their find(1) equivalents.
62 Avoids returning results from inside any directory called <dirname>
63 (e.g., "*/out/*"). May be used multiple times.
65 "progName": os.path.split(sys.argv[0])[1],
73 while i<len(argv) and len(argv[i])>2 and argv[i][0:2] == "--":
75 if arg.startswith("--mindepth="):
77 mindepth = int(arg[len("--mindepth="):])
80 elif arg.startswith("--prune="):
81 p = arg[len("--prune="):]
88 if len(argv)-i < 2: # need both <dirlist> and <filename>
92 results = list(set(perform_find(mindepth, prune, dirlist, filename)))
97 if __name__ == "__main__":