blob: 86f3f3a82f363f1c66d400a0b1c088750c769e58 [file] [log] [blame]
Cole Faust407d2142022-10-24 17:57:39 -07001#!/usr/bin/env python3
Joe Onoratodc1a7282009-08-04 15:58:26 -04002#
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
23import os
24import sys
25
Colin Cross98086452016-05-27 15:20:31 -070026def perform_find(mindepth, prune, dirlist, filenames):
Joe Onoratodc1a7282009-08-04 15:58:26 -040027 result = []
28 pruneleaves = set(map(lambda x: os.path.split(x)[1], prune))
Colin Cross7829ebf2016-11-03 13:51:10 -070029 seen = set()
Joe Onoratodc1a7282009-08-04 15:58:26 -040030 for rootdir in dirlist:
31 rootdepth = rootdir.count("/")
Conley Owensd8a32852012-08-24 16:24:45 -070032 for root, dirs, files in os.walk(rootdir, followlinks=True):
Joe Onoratodc1a7282009-08-04 15:58:26 -040033 # prune
34 check_prune = False
35 for d in dirs:
36 if d in pruneleaves:
37 check_prune = True
38 break
39 if check_prune:
40 i = 0
41 while i < len(dirs):
Andrew Stadlerfbe107a2009-08-11 15:05:59 -070042 if dirs[i] in prune:
Joe Onoratodc1a7282009-08-04 15:58:26 -040043 del dirs[i]
44 else:
45 i += 1
46 # mindepth
47 if mindepth > 0:
48 depth = 1 + root.count("/") - rootdepth
49 if depth < mindepth:
50 continue
51 # match
Colin Cross98086452016-05-27 15:20:31 -070052 for filename in filenames:
53 if filename in files:
54 result.append(os.path.join(root, filename))
55 del dirs[:]
Colin Cross7829ebf2016-11-03 13:51:10 -070056
57 # filter out inodes that have already been seen due to symlink loops
58 i = 0
59 while i < len(dirs):
60 st = os.stat(os.path.join(root, dirs[i]))
61 key = (st.st_dev, st.st_ino)
62 if key in seen:
63 del dirs[i]
64 else:
65 i += 1
66 seen.add(key)
67
Joe Onoratodc1a7282009-08-04 15:58:26 -040068 return result
69
70def usage():
Colin Cross98086452016-05-27 15:20:31 -070071 sys.stderr.write("""Usage: %(progName)s [<options>] [--dir=<dir>] <filenames>
Joe Onoratodc1a7282009-08-04 15:58:26 -040072Options:
73 --mindepth=<mindepth>
74 Both behave in the same way as their find(1) equivalents.
75 --prune=<dirname>
76 Avoids returning results from inside any directory called <dirname>
77 (e.g., "*/out/*"). May be used multiple times.
Colin Cross98086452016-05-27 15:20:31 -070078 --dir=<dir>
79 Add a directory to search. May be repeated multiple times. For backwards
80 compatibility, if no --dir argument is provided then all but the last entry
81 in <filenames> are treated as directories.
Joe Onoratodc1a7282009-08-04 15:58:26 -040082""" % {
83 "progName": os.path.split(sys.argv[0])[1],
84 })
85 sys.exit(1)
86
87def main(argv):
88 mindepth = -1
89 prune = []
Colin Cross98086452016-05-27 15:20:31 -070090 dirlist = []
Joe Onoratodc1a7282009-08-04 15:58:26 -040091 i=1
92 while i<len(argv) and len(argv[i])>2 and argv[i][0:2] == "--":
93 arg = argv[i]
94 if arg.startswith("--mindepth="):
95 try:
96 mindepth = int(arg[len("--mindepth="):])
97 except ValueError:
98 usage()
99 elif arg.startswith("--prune="):
100 p = arg[len("--prune="):]
101 if len(p) == 0:
102 usage()
103 prune.append(p)
Colin Cross98086452016-05-27 15:20:31 -0700104 elif arg.startswith("--dir="):
105 d = arg[len("--dir="):]
Paul Keith8368d232018-08-16 02:28:39 +0200106 if len(d) == 0:
Colin Cross98086452016-05-27 15:20:31 -0700107 usage()
108 dirlist.append(d)
Joe Onoratodc1a7282009-08-04 15:58:26 -0400109 else:
110 usage()
111 i += 1
Colin Cross98086452016-05-27 15:20:31 -0700112 if len(dirlist) == 0: # backwards compatibility
113 if len(argv)-i < 2: # need both <dirlist> and <filename>
114 usage()
115 dirlist = argv[i:-1]
116 filenames = [argv[-1]]
117 else:
118 if len(argv)-i < 1: # need <filename>
119 usage()
120 filenames = argv[i:]
121 results = list(set(perform_find(mindepth, prune, dirlist, filenames)))
Joe Onoratod36e9452009-08-06 16:05:02 -0700122 results.sort()
Ishida, Haruyasu58e52b42010-12-20 15:37:54 +0100123 for r in results:
Cole Faust407d2142022-10-24 17:57:39 -0700124 print(r)
Joe Onoratodc1a7282009-08-04 15:58:26 -0400125
126if __name__ == "__main__":
127 main(sys.argv)