+#!/usr/bin/env python
+
+"""
+Find the location of one or more filenames, and output them to
+stdout. The search can be anchored within a directory using the --root
+parameter. By default, all matching paths (for all arguments) will be
+output. However, if the --single flag is set, only the first path
+found will be output.
+
+Examples:
+
+$ ./find_file_paths --root /bin -s mv
+
+ /bin/mv
+
+
+$ ./find_file_paths --root /bin mv ls
+
+ /bin/mv
+ /bin/ls
+
+"""
+
+from optparse import OptionParser
+import os
+import site
+import sys
+
+# Basically, add '../src' to our path.
+# Needed for the imports that follow.
+site.addsitedir(os.path.dirname(os.path.abspath(sys.argv[0])) + '/../src')
+
+import CLI
+import ExitCodes
+import FileUtils
+
+
+usage = '%prog [options] file1 [file2, file3...]'
+fmtr = CLI.VerbatimHelpFormatter()
+parser = OptionParser(usage, formatter=fmtr)
+
+parser.add_option('-r',
+ '--root',
+ help='The folder under which to search. Defaults to /.',
+ default='/')
+
+parser.add_option('-s',
+ '--single',
+ action='store_true',
+ help='Return only the first matching path.',
+ default=False)
+
+# Use this module's docstring as the description.
+parser.description = __doc__
+
+(options, args) = parser.parse_args()
+
+
+if len(args) < 1:
+ print "\nERROR: You must supply at least one filename.\n"
+ parser.print_help()
+ print '' # Print a newline.
+ raise SystemExit(ExitCodes.NOT_ENOUGH_ARGS)
+
+
+found_paths = []
+
+# Build a list of found paths, in case there are / we want more than
+# one.
+for filename in args:
+ # If --single was passed, we relay it to the find_file_paths
+ # function so that it completes sooner (it doesn't have to keep
+ # looking after the first match).
+ found_paths += FileUtils.find_file_paths(options.root,
+ filename,
+ options.single)
+
+ if options.single and (len(found_paths) > 0):
+ # If we found anything and we only want the first match, just
+ # print it and quit.
+ print found_paths[0]
+ raise SystemExit(ExitCodes.EXIT_SUCCESS)
+
+
+# If we weren't passed --single, spit out all matches.
+for fp in found_paths:
+ print fp