Re: New python function

Lars Ivar Igesund <[email protected]> Thu, 22 Apr 2004 17:20:08 +0100
Newsgroups gmane.comp.tools.aap.devel
Message-ID <[email protected]>
Sorry, missed a vital line in my previous patch. This one is tested
a little bit better.

Lars Ivar Igesund

Lars Ivar Igesund wrote:

> Hi
> 
> I've made a new python function by modifying program_path.
> My version is a bit simpler and is called file_path. It can
> find any kind of file, whether it is executable or not.
> 
> I don't know if it should be added as-is, but the new function
> is very useful in conf operations where you're looking for
> libraries, etc.
> 
> Lars Ivar Igesund
recpython.diff (text/plain, 1.6 KB)
Index: RecPython.py
===================================================================
RCS file: /cvsroot/a-a-p/Exec/RecPython.py,v
retrieving revision 1.38
diff -u -r1.38 RecPython.py
--- RecPython.py	8 Mar 2004 20:55:52 -0000	1.38
+++ RecPython.py	22 Apr 2004 15:17:18 -0000
@@ -363,6 +363,36 @@
     return action
 
 
+def file_path(name, path = None, skip = None):
+    """Return the full name of a file "name" if it can be found in the
+       search path. None otherwise.
+       If "path" is given, use it as the search path (string of items
+       separated with os.pathsep).
+       If "skip" is given, skip this directory."""
+    # Decide on the list of directories to examine.
+    if not path is None:
+        envpath = path
+    elif os.environ.has_key('PATH'):
+        envpath = os.environ['PATH']
+    else:
+        envpath = os.defpath
+    if not type(envpath) is types.ListType:
+        dirlist = string.split(envpath, os.pathsep)
+    # When using a default path also include our own directory and its "bin"
+    # subdirectory.
+    if path is None and Global.aap_rootdir:
+        dirlist = [ Global.aap_rootdir, Global.aap_bindir ] + dirlist
+
+    for dir in dirlist:
+        if skip and os.path.abspath(dir) == os.path.abspath(skip):
+            continue
+        fullname = os.path.join(dir, name)
+        if os.path.isfile(fullname):
+            return fullname
+
+    return None
+
+
 def program_path(name, path = None, pathext = None, skip = None):
     """Return the full name of a program "name" if it can be found in the
        search path.  None otherwise.