• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""distutils.spawn
2
3Provides the 'spawn()' function, a front-end to various platform-
4specific functions for launching another program in a sub-process.
5Also provides the 'find_executable()' to search the path for a given
6executable name.
7"""
8
9import sys
10import os
11import os.path
12
13
14def find_executable(executable, path=None):
15    """Tries to find 'executable' in the directories listed in 'path'.
16
17    A string listing directories separated by 'os.pathsep'; defaults to
18    os.environ['PATH'].  Returns the complete filename or None if not found.
19    """
20    _, ext = os.path.splitext(executable)
21    if (sys.platform == 'win32') and (ext != '.exe'):
22        executable = executable + '.exe'
23
24    if os.path.isfile(executable):
25        return executable
26
27    if path is None:
28        path = os.environ.get('PATH', None)
29        if path is None:
30            try:
31                path = os.confstr("CS_PATH")
32            except (AttributeError, ValueError):
33                # os.confstr() or CS_PATH is not available
34                path = os.defpath
35        # bpo-35755: Don't use os.defpath if the PATH environment variable is
36        # set to an empty string
37
38    # PATH='' doesn't match, whereas PATH=':' looks in the current directory
39    if not path:
40        return None
41
42    paths = path.split(os.pathsep)
43    for p in paths:
44        f = os.path.join(p, executable)
45        if os.path.isfile(f):
46            # the file exists, we have a shot at spawn working
47            return f
48    return None
49