]>
gitweb.michael.orlitzky.com - dead/census-tools.git/blob - bin/find_file_paths
4 Find the location of one or more filenames, and output them to
5 stdout. The search can be anchored within a directory using the --root
6 parameter. By default, all matching paths (for all arguments) will be
7 output. However, if the --single flag is set, only the first path
12 $ ./find_file_paths --root /bin -s mv
17 $ ./find_file_paths --root /bin mv ls
24 from optparse
import OptionParser
29 # Basically, add '../src' to our path.
30 # Needed for the imports that follow.
31 site
.addsitedir(os
.path
.dirname(os
.path
.abspath(sys
.argv
[0])) + '/../src')
38 usage
= '%prog [options] file1 [file2, file3...]'
39 fmtr
= CLI
.VerbatimHelpFormatter()
40 parser
= OptionParser(usage
, formatter
=fmtr
)
42 parser
.add_option('-r',
44 help='The folder under which to search. Defaults to /.',
47 parser
.add_option('-s',
50 help='Return only the first matching path.',
53 # Use this module's docstring as the description.
54 parser
.description
= __doc__
56 (options
, args
) = parser
.parse_args()
60 print "\nERROR: You must supply at least one filename.\n"
62 print '' # Print a newline.
63 raise SystemExit(ExitCodes
.NOT_ENOUGH_ARGS
)
68 # Build a list of found paths, in case there are / we want more than
71 # If --single was passed, we relay it to the find_file_paths
72 # function so that it completes sooner (it doesn't have to keep
73 # looking after the first match).
74 found_paths
+= FileUtils
.find_file_paths(options
.root
,
78 if options
.single
and (len(found_paths
) > 0):
79 # If we found anything and we only want the first match, just
82 raise SystemExit(ExitCodes
.EXIT_SUCCESS
)
85 # If we weren't passed --single, spit out all matches.
86 for fp
in found_paths
: