Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
spawn.py106 linesDownload Raw Back to _distutils
1"""distutils.spawn2 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 given6executable name.7"""8 9import os10import subprocess11import sys12 13from ._log import log14from .debug import DEBUG15from .errors import DistutilsExecError16 17 18def spawn(cmd, search_path=1, verbose=0, dry_run=0, env=None):  # noqa: C90119    """Run another program, specified as a command list 'cmd', in a new process.20 21    'cmd' is just the argument list for the new process, ie.22    cmd[0] is the program to run and cmd[1:] are the rest of its arguments.23    There is no way to run a program with a name different from that of its24    executable.25 26    If 'search_path' is true (the default), the system's executable27    search path will be used to find the program; otherwise, cmd[0]28    must be the exact path to the executable.  If 'dry_run' is true,29    the command will not actually be run.30 31    Raise DistutilsExecError if running the program fails in any way; just32    return on success.33    """34    # cmd is documented as a list, but just in case some code passes a tuple35    # in, protect our %-formatting code against horrible death36    cmd = list(cmd)37 38    log.info(subprocess.list2cmdline(cmd))39    if dry_run:40        return41 42    if search_path:43        executable = find_executable(cmd[0])44        if executable is not None:45            cmd[0] = executable46 47    env = env if env is not None else dict(os.environ)48 49    if sys.platform == 'darwin':50        from distutils.util import MACOSX_VERSION_VAR, get_macosx_target_ver51 52        macosx_target_ver = get_macosx_target_ver()53        if macosx_target_ver:54            env[MACOSX_VERSION_VAR] = macosx_target_ver55 56    try:57        proc = subprocess.Popen(cmd, env=env)58        proc.wait()59        exitcode = proc.returncode60    except OSError as exc:61        if not DEBUG:62            cmd = cmd[0]63        raise DistutilsExecError(f"command {cmd!r} failed: {exc.args[-1]}") from exc64 65    if exitcode:66        if not DEBUG:67            cmd = cmd[0]68        raise DistutilsExecError(f"command {cmd!r} failed with exit code {exitcode}")69 70 71def find_executable(executable, path=None):72    """Tries to find 'executable' in the directories listed in 'path'.73 74    A string listing directories separated by 'os.pathsep'; defaults to75    os.environ['PATH'].  Returns the complete filename or None if not found.76    """77    _, ext = os.path.splitext(executable)78    if (sys.platform == 'win32') and (ext != '.exe'):79        executable = executable + '.exe'80 81    if os.path.isfile(executable):82        return executable83 84    if path is None:85        path = os.environ.get('PATH', None)86        if path is None:87            try:88                path = os.confstr("CS_PATH")89            except (AttributeError, ValueError):90                # os.confstr() or CS_PATH is not available91                path = os.defpath92        # bpo-35755: Don't use os.defpath if the PATH environment variable is93        # set to an empty string94 95    # PATH='' doesn't match, whereas PATH=':' looks in the current directory96    if not path:97        return None98 99    paths = path.split(os.pathsep)100    for p in paths:101        f = os.path.join(p, executable)102        if os.path.isfile(f):103            # the file exists, we have a shot at spawn working104            return f105    return None106 
codekingpro/portable-devtools · Team Ai