+++ /dev/null
-"""distutils.spawn\r
-\r
-Provides the 'spawn()' function, a front-end to various platform-\r
-specific functions for launching another program in a sub-process.\r
-Also provides the 'find_executable()' to search the path for a given\r
-executable name.\r
-"""\r
-\r
-__revision__ = "$Id$"\r
-\r
-import sys\r
-import os\r
-\r
-from distutils.errors import DistutilsPlatformError, DistutilsExecError\r
-from distutils import log\r
-\r
-def spawn(cmd, search_path=1, verbose=0, dry_run=0):\r
- """Run another program, specified as a command list 'cmd', in a new process.\r
-\r
- 'cmd' is just the argument list for the new process, ie.\r
- cmd[0] is the program to run and cmd[1:] are the rest of its arguments.\r
- There is no way to run a program with a name different from that of its\r
- executable.\r
-\r
- If 'search_path' is true (the default), the system's executable\r
- search path will be used to find the program; otherwise, cmd[0]\r
- must be the exact path to the executable. If 'dry_run' is true,\r
- the command will not actually be run.\r
-\r
- Raise DistutilsExecError if running the program fails in any way; just\r
- return on success.\r
- """\r
- if os.name == 'posix':\r
- _spawn_posix(cmd, search_path, dry_run=dry_run)\r
- elif os.name == 'nt':\r
- _spawn_nt(cmd, search_path, dry_run=dry_run)\r
- elif os.name == 'os2':\r
- _spawn_os2(cmd, search_path, dry_run=dry_run)\r
- else:\r
- raise DistutilsPlatformError, \\r
- "don't know how to spawn programs on platform '%s'" % os.name\r
-\r
-def _nt_quote_args(args):\r
- """Quote command-line arguments for DOS/Windows conventions.\r
-\r
- Just wraps every argument which contains blanks in double quotes, and\r
- returns a new argument list.\r
- """\r
- # XXX this doesn't seem very robust to me -- but if the Windows guys\r
- # say it'll work, I guess I'll have to accept it. (What if an arg\r
- # contains quotes? What other magic characters, other than spaces,\r
- # have to be escaped? Is there an escaping mechanism other than\r
- # quoting?)\r
- for i, arg in enumerate(args):\r
- if ' ' in arg:\r
- args[i] = '"%s"' % arg\r
- return args\r
-\r
-def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):\r
- executable = cmd[0]\r
- cmd = _nt_quote_args(cmd)\r
- if search_path:\r
- # either we find one or it stays the same\r
- executable = find_executable(executable) or executable\r
- log.info(' '.join([executable] + cmd[1:]))\r
- if not dry_run:\r
- # spawn for NT requires a full path to the .exe\r
- try:\r
- rc = os.spawnv(os.P_WAIT, executable, cmd)\r
- except OSError, exc:\r
- # this seems to happen when the command isn't found\r
- raise DistutilsExecError, \\r
- "command '%s' failed: %s" % (cmd[0], exc[-1])\r
- if rc != 0:\r
- # and this reflects the command running but failing\r
- raise DistutilsExecError, \\r
- "command '%s' failed with exit status %d" % (cmd[0], rc)\r
-\r
-def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0):\r
- executable = cmd[0]\r
- if search_path:\r
- # either we find one or it stays the same\r
- executable = find_executable(executable) or executable\r
- log.info(' '.join([executable] + cmd[1:]))\r
- if not dry_run:\r
- # spawnv for OS/2 EMX requires a full path to the .exe\r
- try:\r
- rc = os.spawnv(os.P_WAIT, executable, cmd)\r
- except OSError, exc:\r
- # this seems to happen when the command isn't found\r
- raise DistutilsExecError, \\r
- "command '%s' failed: %s" % (cmd[0], exc[-1])\r
- if rc != 0:\r
- # and this reflects the command running but failing\r
- log.debug("command '%s' failed with exit status %d" % (cmd[0], rc))\r
- raise DistutilsExecError, \\r
- "command '%s' failed with exit status %d" % (cmd[0], rc)\r
-\r
-\r
-def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):\r
- log.info(' '.join(cmd))\r
- if dry_run:\r
- return\r
- exec_fn = search_path and os.execvp or os.execv\r
- pid = os.fork()\r
-\r
- if pid == 0: # in the child\r
- try:\r
- exec_fn(cmd[0], cmd)\r
- except OSError, e:\r
- sys.stderr.write("unable to execute %s: %s\n" %\r
- (cmd[0], e.strerror))\r
- os._exit(1)\r
-\r
- sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])\r
- os._exit(1)\r
- else: # in the parent\r
- # Loop until the child either exits or is terminated by a signal\r
- # (ie. keep waiting if it's merely stopped)\r
- while 1:\r
- try:\r
- pid, status = os.waitpid(pid, 0)\r
- except OSError, exc:\r
- import errno\r
- if exc.errno == errno.EINTR:\r
- continue\r
- raise DistutilsExecError, \\r
- "command '%s' failed: %s" % (cmd[0], exc[-1])\r
- if os.WIFSIGNALED(status):\r
- raise DistutilsExecError, \\r
- "command '%s' terminated by signal %d" % \\r
- (cmd[0], os.WTERMSIG(status))\r
-\r
- elif os.WIFEXITED(status):\r
- exit_status = os.WEXITSTATUS(status)\r
- if exit_status == 0:\r
- return # hey, it succeeded!\r
- else:\r
- raise DistutilsExecError, \\r
- "command '%s' failed with exit status %d" % \\r
- (cmd[0], exit_status)\r
-\r
- elif os.WIFSTOPPED(status):\r
- continue\r
-\r
- else:\r
- raise DistutilsExecError, \\r
- "unknown error executing '%s': termination status %d" % \\r
- (cmd[0], status)\r
-\r
-def find_executable(executable, path=None):\r
- """Tries to find 'executable' in the directories listed in 'path'.\r
-\r
- A string listing directories separated by 'os.pathsep'; defaults to\r
- os.environ['PATH']. Returns the complete filename or None if not found.\r
- """\r
- if path is None:\r
- path = os.environ['PATH']\r
- paths = path.split(os.pathsep)\r
- base, ext = os.path.splitext(executable)\r
-\r
- if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):\r
- executable = executable + '.exe'\r
-\r
- if not os.path.isfile(executable):\r
- for p in paths:\r
- f = os.path.join(p, executable)\r
- if os.path.isfile(f):\r
- # the file exists, we have a shot at spawn working\r
- return f\r
- return None\r
- else:\r
- return executable\r