diff --git a/tsearchpath.py b/tsearchpath.py index 4a428fd..1122c78 100755 --- a/tsearchpath.py +++ b/tsearchpath.py @@ -5,7 +5,7 @@ # Program Information -RCSID = "$Id: tsearchpath.py,v 1.103 2010/06/24 20:28:30 tundra Exp $" +RCSID = "$Id: tsearchpath.py,v 1.104 2010/06/25 02:46:50 tundra Exp $" #----------------------------------------------------------# @@ -20,6 +20,7 @@ #----------------------------------------------------------# DEFAULTDELIM = ":" +PATHSEP=os.path.sep @@ -30,60 +31,69 @@ def tsearchpath(filename, pathlist, - PATHDELIM=DEFAULTDELIM, - PATHSEP=os.path.sep + PATHDELIM=DEFAULTDELIM ): - """ Search 'pathlist' looking for 'filename' returning on first - (leftmost in 'pathlist') match. The function is insensitive - to whether 'filename' is actually a file. It merely looks for - some filesystem entity having the given name. + """ + Find all instances of a filename in a list of paths. - If found, return the fully qualified filename. - If not found, return None. + Inputs: - 'pathlist' is a list of paths to search separated by - PATHDELIM characters. + filename - The name of the filesystem entity (file, + directory, ...) to look for. - If the 'filename' passed contains 'PATHSEP' characters, - presume it to be a relative/absolute path specified by the - caller. In this case, ignore 'pathlist' and just return the - fully qualified filename. + pathlist - A list of paths to search separated by the + PATHDELIM character (Default: ":") - Caller can override the delimiter used to separate path - entries and the path separator character by passing explicit - instances of PATHDELIM= and PATHSEP= respectively. + Output: + + Returns a list of all instances of 'filename' found on + the paths specified by 'pathlist'. These appear in + order of path appearance in 'pathlist' and are returned + as fully qualified real path names. + + + Options: + + PATHDELIM=char + + Can be used to specify the separator character + used in 'pathlist'. + + Side Effects: + + Module does not check for duplicates in the the + 'pathlist'. If multiple components there map to the + same real path and 'filename' exists there, the returned + list will have duplicate entries. + + There is no stripping of spaces around PATHDELIM + characters. Spaces are treated as part of the path + component. This is almost always a mistake and will + cause nothing to match: + + "path1 :path2 :path3" + """ # What we'll return if we find nothing - retval = None + retval = [] - # Handle explicit/relative filename path specifications - - if filename.find(PATHSEP) > -1: - retval = filename + # Find all instances of filename in specified paths + paths = pathlist.split(PATHDELIM) - # Find first matching filename on specified path + for path in paths: - else: - - paths = pathlist.split(PATHDELIM) + if path[-1] != PATHSEP: + path += PATHSEP - for path in paths: + path += filename - if path[-1] != PATHSEP: - path += PATHSEP - - path += filename - - if os.path.exists(path): - retval = path - break - if retval: - retval = os.path.abspath(retval) + if os.path.exists(path): + retval.append(os.path.realpath(path)) return retval