Back to index

python3.2  3.2.2
Functions | Variables
distutils.spawn Namespace Reference

Functions

def spawn
def _nt_quote_args
def _spawn_nt
def _spawn_os2
def _spawn_posix
def find_executable

Variables

 _cfg_target = None
 _cfg_target_split = None

Detailed Description

distutils.spawn

Provides the 'spawn()' function, a front-end to various platform-
specific functions for launching another program in a sub-process.
Also provides the 'find_executable()' to search the path for a given
executable name.

Function Documentation

def distutils.spawn._nt_quote_args (   args) [private]
Quote command-line arguments for DOS/Windows conventions.

Just wraps every argument which contains blanks in double quotes, and
returns a new argument list.

Definition at line 41 of file spawn.py.

00041 
00042 def _nt_quote_args(args):
00043     """Quote command-line arguments for DOS/Windows conventions.
00044 
00045     Just wraps every argument which contains blanks in double quotes, and
00046     returns a new argument list.
00047     """
00048     # XXX this doesn't seem very robust to me -- but if the Windows guys
00049     # say it'll work, I guess I'll have to accept it.  (What if an arg
00050     # contains quotes?  What other magic characters, other than spaces,
00051     # have to be escaped?  Is there an escaping mechanism other than
00052     # quoting?)
00053     for i, arg in enumerate(args):
00054         if ' ' in arg:
00055             args[i] = '"%s"' % arg
00056     return args

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.spawn._spawn_nt (   cmd,
  search_path = 1,
  verbose = 0,
  dry_run = 0 
) [private]

Definition at line 57 of file spawn.py.

00057 
00058 def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
00059     executable = cmd[0]
00060     cmd = _nt_quote_args(cmd)
00061     if search_path:
00062         # either we find one or it stays the same
00063         executable = find_executable(executable) or executable
00064     log.info(' '.join([executable] + cmd[1:]))
00065     if not dry_run:
00066         # spawn for NT requires a full path to the .exe
00067         try:
00068             rc = os.spawnv(os.P_WAIT, executable, cmd)
00069         except OSError as exc:
00070             # this seems to happen when the command isn't found
00071             raise DistutilsExecError(
00072                   "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
00073         if rc != 0:
00074             # and this reflects the command running but failing
00075             raise DistutilsExecError(
00076                   "command '%s' failed with exit status %d" % (cmd[0], rc))

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.spawn._spawn_os2 (   cmd,
  search_path = 1,
  verbose = 0,
  dry_run = 0 
) [private]

Definition at line 77 of file spawn.py.

00077 
00078 def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0):
00079     executable = cmd[0]
00080     if search_path:
00081         # either we find one or it stays the same
00082         executable = find_executable(executable) or executable
00083     log.info(' '.join([executable] + cmd[1:]))
00084     if not dry_run:
00085         # spawnv for OS/2 EMX requires a full path to the .exe
00086         try:
00087             rc = os.spawnv(os.P_WAIT, executable, cmd)
00088         except OSError as exc:
00089             # this seems to happen when the command isn't found
00090             raise DistutilsExecError(
00091                   "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
00092         if rc != 0:
00093             # and this reflects the command running but failing
00094             log.debug("command '%s' failed with exit status %d" % (cmd[0], rc))
00095             raise DistutilsExecError(
00096                   "command '%s' failed with exit status %d" % (cmd[0], rc))

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.spawn._spawn_posix (   cmd,
  search_path = 1,
  verbose = 0,
  dry_run = 0 
) [private]

Definition at line 102 of file spawn.py.

00102 
00103 def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
00104     log.info(' '.join(cmd))
00105     if dry_run:
00106         return
00107     exec_fn = search_path and os.execvp or os.execv
00108     exec_args = [cmd[0], cmd]
00109     if sys.platform == 'darwin':
00110         global _cfg_target, _cfg_target_split
00111         if _cfg_target is None:
00112             _cfg_target = sysconfig.get_config_var(
00113                                   'MACOSX_DEPLOYMENT_TARGET') or ''
00114             if _cfg_target:
00115                 _cfg_target_split = [int(x) for x in _cfg_target.split('.')]
00116         if _cfg_target:
00117             # ensure that the deployment target of build process is not less
00118             # than that used when the interpreter was built. This ensures
00119             # extension modules are built with correct compatibility values
00120             cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target)
00121             if _cfg_target_split > [int(x) for x in cur_target.split('.')]:
00122                 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: '
00123                           'now "%s" but "%s" during configure'
00124                                 % (cur_target, _cfg_target))
00125                 raise DistutilsPlatformError(my_msg)
00126             env = dict(os.environ,
00127                        MACOSX_DEPLOYMENT_TARGET=cur_target)
00128             exec_fn = search_path and os.execvpe or os.execve
00129             exec_args.append(env)
00130     pid = os.fork()
00131     if pid == 0: # in the child
00132         try:
00133             exec_fn(*exec_args)
00134         except OSError as e:
00135             sys.stderr.write("unable to execute %s: %s\n"
00136                              % (cmd[0], e.strerror))
00137             os._exit(1)
00138 
00139         sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
00140         os._exit(1)
00141     else: # in the parent
00142         # Loop until the child either exits or is terminated by a signal
00143         # (ie. keep waiting if it's merely stopped)
00144         while True:
00145             try:
00146                 pid, status = os.waitpid(pid, 0)
00147             except OSError as exc:
00148                 import errno
00149                 if exc.errno == errno.EINTR:
00150                     continue
00151                 raise DistutilsExecError(
00152                       "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
00153             if os.WIFSIGNALED(status):
00154                 raise DistutilsExecError(
00155                       "command '%s' terminated by signal %d"
00156                       % (cmd[0], os.WTERMSIG(status)))
00157             elif os.WIFEXITED(status):
00158                 exit_status = os.WEXITSTATUS(status)
00159                 if exit_status == 0:
00160                     return   # hey, it succeeded!
00161                 else:
00162                     raise DistutilsExecError(
00163                           "command '%s' failed with exit status %d"
00164                           % (cmd[0], exit_status))
00165             elif os.WIFSTOPPED(status):
00166                 continue
00167             else:
00168                 raise DistutilsExecError(
00169                       "unknown error executing '%s': termination status %d"
00170                       % (cmd[0], status))

Here is the call graph for this function:

Here is the caller graph for this function:

def distutils.spawn.find_executable (   executable,
  path = None 
)
Tries 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.

Definition at line 171 of file spawn.py.

00171 
00172 def find_executable(executable, path=None):
00173     """Tries to find 'executable' in the directories listed in 'path'.
00174 
00175     A string listing directories separated by 'os.pathsep'; defaults to
00176     os.environ['PATH'].  Returns the complete filename or None if not found.
00177     """
00178     if path is None:
00179         path = os.environ['PATH']
00180 
00181     paths = path.split(os.pathsep)
00182     base, ext = os.path.splitext(executable)
00183 
00184     if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
00185         executable = executable + '.exe'
00186 
00187     if not os.path.isfile(executable):
00188         for p in paths:
00189             f = os.path.join(p, executable)
00190             if os.path.isfile(f):
00191                 # the file exists, we have a shot at spawn working
00192                 return f
00193         return None
00194     else:
00195         return executable

Here is the caller graph for this function:

def distutils.spawn.spawn (   cmd,
  search_path = 1,
  verbose = 0,
  dry_run = 0 
)
Run another program, specified as a command list 'cmd', in a new process.

'cmd' is just the argument list for the new process, ie.
cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
There is no way to run a program with a name different from that of its
executable.

If 'search_path' is true (the default), the system's executable
search path will be used to find the program; otherwise, cmd[0]
must be the exact path to the executable.  If 'dry_run' is true,
the command will not actually be run.

Raise DistutilsExecError if running the program fails in any way; just
return on success.

Definition at line 15 of file spawn.py.

00015 
00016 def spawn(cmd, search_path=1, verbose=0, dry_run=0):
00017     """Run another program, specified as a command list 'cmd', in a new process.
00018 
00019     'cmd' is just the argument list for the new process, ie.
00020     cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
00021     There is no way to run a program with a name different from that of its
00022     executable.
00023 
00024     If 'search_path' is true (the default), the system's executable
00025     search path will be used to find the program; otherwise, cmd[0]
00026     must be the exact path to the executable.  If 'dry_run' is true,
00027     the command will not actually be run.
00028 
00029     Raise DistutilsExecError if running the program fails in any way; just
00030     return on success.
00031     """
00032     if os.name == 'posix':
00033         _spawn_posix(cmd, search_path, dry_run=dry_run)
00034     elif os.name == 'nt':
00035         _spawn_nt(cmd, search_path, dry_run=dry_run)
00036     elif os.name == 'os2':
00037         _spawn_os2(cmd, search_path, dry_run=dry_run)
00038     else:
00039         raise DistutilsPlatformError(
00040               "don't know how to spawn programs on platform '%s'" % os.name)

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 99 of file spawn.py.

Definition at line 100 of file spawn.py.