blob: d97ed744d2980ad3c2a95230e02f85c95f23ad09 [file] [log] [blame]
Joe Onoratodc1a7282009-08-04 15:58:26 -04001#!/usr/bin/env python
2#
3# Copyright (C) 2009 The Android Open Source Project
4#
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
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
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.
16#
17
18#
19# Finds files with the specified name under a particular directory, stopping
20# the search in a given subdirectory when the file is found.
21#
22
Anthony Kingc713d762015-11-03 00:23:11 +000023from __future__ import print_function
24
Joe Onoratodc1a7282009-08-04 15:58:26 -040025import os
26import sys
27
28def perform_find(mindepth, prune, dirlist, filename):
29 result = []
Anthony Kingc713d762015-11-03 00:23:11 +000030 pruneleaves = set([os.path.split(x)[1] for x in prune])
Joe Onoratodc1a7282009-08-04 15:58:26 -040031 for rootdir in dirlist:
32 rootdepth = rootdir.count("/")
Conley Owensd8a32852012-08-24 16:24:45 -070033 for root, dirs, files in os.walk(rootdir, followlinks=True):
Joe Onoratodc1a7282009-08-04 15:58:26 -040034 # prune
35 check_prune = False
36 for d in dirs:
37 if d in pruneleaves:
38 check_prune = True
39 break
40 if check_prune:
41 i = 0
42 while i < len(dirs):
Andrew Stadlerfbe107a2009-08-11 15:05:59 -070043 if dirs[i] in prune:
Joe Onoratodc1a7282009-08-04 15:58:26 -040044 del dirs[i]
45 else:
46 i += 1
47 # mindepth
48 if mindepth > 0:
49 depth = 1 + root.count("/") - rootdepth
50 if depth < mindepth:
51 continue
52 # match
53 if filename in files:
54 result.append(os.path.join(root, filename))
55 del dirs[:]
56 return result
57
58def usage():
59 sys.stderr.write("""Usage: %(progName)s [<options>] <dirlist> <filename>
60Options:
61 --mindepth=<mindepth>
62 Both behave in the same way as their find(1) equivalents.
63 --prune=<dirname>
64 Avoids returning results from inside any directory called <dirname>
65 (e.g., "*/out/*"). May be used multiple times.
66""" % {
67 "progName": os.path.split(sys.argv[0])[1],
68 })
69 sys.exit(1)
70
71def main(argv):
72 mindepth = -1
73 prune = []
74 i=1
75 while i<len(argv) and len(argv[i])>2 and argv[i][0:2] == "--":
76 arg = argv[i]
77 if arg.startswith("--mindepth="):
78 try:
79 mindepth = int(arg[len("--mindepth="):])
80 except ValueError:
81 usage()
82 elif arg.startswith("--prune="):
83 p = arg[len("--prune="):]
84 if len(p) == 0:
85 usage()
86 prune.append(p)
87 else:
88 usage()
89 i += 1
90 if len(argv)-i < 2: # need both <dirlist> and <filename>
91 usage()
92 dirlist = argv[i:-1]
93 filename = argv[-1]
Ishida, Haruyasu58e52b42010-12-20 15:37:54 +010094 results = list(set(perform_find(mindepth, prune, dirlist, filename)))
Joe Onoratod36e9452009-08-06 16:05:02 -070095 results.sort()
Ishida, Haruyasu58e52b42010-12-20 15:37:54 +010096 for r in results:
Anthony Kingc713d762015-11-03 00:23:11 +000097 print(r)
Joe Onoratodc1a7282009-08-04 15:58:26 -040098
99if __name__ == "__main__":
100 main(sys.argv)