]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
authorGreg Ward <gward@python.net>
Wed, 2 Aug 2000 01:08:02 +0000 (01:08 +0000)
committerGreg Ward <gward@python.net>
Wed, 2 Aug 2000 01:08:02 +0000 (01:08 +0000)
Lib/distutils/spawn.py

index 86ea3dfb501a0d744d0c2d05e76cf2103ed8983a..651124d6825a26c7f544209208d3bc745addfd0a 100644 (file)
@@ -1,7 +1,9 @@
 """distutils.spawn
 
 Provides the 'spawn()' function, a front-end to various platform-
-specific functions for launching another program in a sub-process."""
+specific functions for launching another program in a sub-process.
+Also provides the 'find_executable()' to search the path for a given
+executable name. """
 
 # created 1999/07/24, Greg Ward
 
@@ -65,17 +67,8 @@ def _spawn_nt (cmd,
     executable = cmd[0]
     cmd = _nt_quote_args (cmd)
     if search_path:
-        paths = string.split( os.environ['PATH'], os.pathsep)
-        base,ext = os.path.splitext(executable)
-        if (ext != '.exe'):
-            executable = executable + '.exe'
-        if not os.path.isfile(executable):
-            paths.reverse()         # go over the paths and keep the last one
-            for p in paths:
-                f = os.path.join( p, executable )
-                if os.path.isfile ( f ):
-                    # the file exists, we have a shot at spawn working
-                    executable = f
+        # either we find one or it stays the same
+        executable = find_executable(executable) or executable 
     if verbose:
         print string.join ([executable] + cmd[1:], ' ')
     if not dry_run:
@@ -91,7 +84,6 @@ def _spawn_nt (cmd,
             raise DistutilsExecError, \
                   "command '%s' failed with exit status %d" % (cmd[0], rc)
 
-    
                 
 def _spawn_posix (cmd,
                   search_path=1,
@@ -147,3 +139,28 @@ def _spawn_posix (cmd,
                       "unknown error executing '%s': termination status %d" % \
                       (cmd[0], status)
 # _spawn_posix ()
+
+
+def find_executable(executable, path=None):
+    """Try to find 'executable' in the directories listed in 'path' (a
+    string listing directories separated by 'os.pathsep'; defaults to
+    os.environ['PATH']).  Returns the complete filename or None if not
+    found.
+    """
+    if path is None:
+        path = os.environ['PATH']
+    paths = string.split(path, os.pathsep)
+    (base, ext) = os.path.splitext(executable)
+    if (sys.platform == 'win32') and (ext != '.exe'):
+        executable = executable + '.exe'
+    if not os.path.isfile(executable):
+        for p in paths:
+            f = os.path.join(p, executable)
+            if os.path.isfile(f):
+                # the file exists, we have a shot at spawn working
+                return f
+        return None
+    else:
+        return executable
+
+# find_executable()