~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/distutils/spawn.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""distutils.spawn
 
2
 
 
3
Provides the 'spawn()' function, a front-end to various platform-
 
4
specific functions for launching another program in a sub-process.
 
5
Also provides the 'find_executable()' to search the path for a given
 
6
executable name.
 
7
"""
 
8
 
 
9
__revision__ = "$Id: spawn.py 59793 2008-01-06 21:13:42Z georg.brandl $"
 
10
 
 
11
import sys, os
 
12
from distutils.errors import *
 
13
from distutils import log
 
14
 
 
15
def spawn(cmd, search_path=1, verbose=0, dry_run=0):
 
16
    """Run another program, specified as a command list 'cmd', in a new
 
17
    process.  'cmd' is just the argument list for the new process, ie.
 
18
    cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
 
19
    There is no way to run a program with a name different from that of its
 
20
    executable.
 
21
 
 
22
    If 'search_path' is true (the default), the system's executable
 
23
    search path will be used to find the program; otherwise, cmd[0]
 
24
    must be the exact path to the executable.  If 'dry_run' is true,
 
25
    the command will not actually be run.
 
26
 
 
27
    Raise DistutilsExecError if running the program fails in any way; just
 
28
    return on success.
 
29
    """
 
30
    if os.name == 'posix':
 
31
        _spawn_posix(cmd, search_path, dry_run=dry_run)
 
32
    elif os.name == 'nt':
 
33
        _spawn_nt(cmd, search_path, dry_run=dry_run)
 
34
    elif os.name == 'os2':
 
35
        _spawn_os2(cmd, search_path, dry_run=dry_run)
 
36
    else:
 
37
        raise DistutilsPlatformError(
 
38
              "don't know how to spawn programs on platform '%s'" % os.name)
 
39
 
 
40
 
 
41
def _nt_quote_args(args):
 
42
    """Quote command-line arguments for DOS/Windows conventions: just
 
43
    wraps every argument which contains blanks in double quotes, and
 
44
    returns a new argument list.
 
45
    """
 
46
    # XXX this doesn't seem very robust to me -- but if the Windows guys
 
47
    # say it'll work, I guess I'll have to accept it.  (What if an arg
 
48
    # contains quotes?  What other magic characters, other than spaces,
 
49
    # have to be escaped?  Is there an escaping mechanism other than
 
50
    # quoting?)
 
51
    for i in range(len(args)):
 
52
        if args[i].find(' ') != -1:
 
53
            args[i] = '"%s"' % args[i]
 
54
    return args
 
55
 
 
56
def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
 
57
    executable = cmd[0]
 
58
    cmd = _nt_quote_args(cmd)
 
59
    if search_path:
 
60
        # either we find one or it stays the same
 
61
        executable = find_executable(executable) or executable
 
62
    log.info(' '.join([executable] + cmd[1:]))
 
63
    if not dry_run:
 
64
        # spawn for NT requires a full path to the .exe
 
65
        try:
 
66
            rc = os.spawnv(os.P_WAIT, executable, cmd)
 
67
        except OSError as exc:
 
68
            # this seems to happen when the command isn't found
 
69
            raise DistutilsExecError(
 
70
                  "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
 
71
        if rc != 0:
 
72
            # and this reflects the command running but failing
 
73
            raise DistutilsExecError(
 
74
                  "command '%s' failed with exit status %d" % (cmd[0], rc))
 
75
 
 
76
 
 
77
def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0):
 
78
    executable = cmd[0]
 
79
    #cmd = _nt_quote_args(cmd)
 
80
    if search_path:
 
81
        # either we find one or it stays the same
 
82
        executable = find_executable(executable) or executable
 
83
    log.info(' '.join([executable] + cmd[1:]))
 
84
    if not dry_run:
 
85
        # spawnv for OS/2 EMX requires a full path to the .exe
 
86
        try:
 
87
            rc = os.spawnv(os.P_WAIT, executable, cmd)
 
88
        except OSError as exc:
 
89
            # this seems to happen when the command isn't found
 
90
            raise DistutilsExecError(
 
91
                  "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
 
92
        if rc != 0:
 
93
            # and this reflects the command running but failing
 
94
            print("command '%s' failed with exit status %d" % (cmd[0], rc))
 
95
            raise DistutilsExecError(
 
96
                  "command '%s' failed with exit status %d" % (cmd[0], rc))
 
97
 
 
98
 
 
99
def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
 
100
    log.info(' '.join(cmd))
 
101
    if dry_run:
 
102
        return
 
103
    exec_fn = search_path and os.execvp or os.execv
 
104
 
 
105
    pid = os.fork()
 
106
    if pid == 0: # in the child
 
107
        try:
 
108
            exec_fn(cmd[0], cmd)
 
109
        except OSError as e:
 
110
            sys.stderr.write("unable to execute %s: %s\n"
 
111
                             % (cmd[0], e.strerror))
 
112
            os._exit(1)
 
113
 
 
114
        sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
 
115
        os._exit(1)
 
116
    else: # in the parent
 
117
        # Loop until the child either exits or is terminated by a signal
 
118
        # (ie. keep waiting if it's merely stopped)
 
119
        while True:
 
120
            try:
 
121
                (pid, status) = os.waitpid(pid, 0)
 
122
            except OSError as exc:
 
123
                import errno
 
124
                if exc.errno == errno.EINTR:
 
125
                    continue
 
126
                raise DistutilsExecError(
 
127
                      "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
 
128
            if os.WIFSIGNALED(status):
 
129
                raise DistutilsExecError(
 
130
                      "command '%s' terminated by signal %d"
 
131
                      % (cmd[0], os.WTERMSIG(status)))
 
132
            elif os.WIFEXITED(status):
 
133
                exit_status = os.WEXITSTATUS(status)
 
134
                if exit_status == 0:
 
135
                    return              # hey, it succeeded!
 
136
                else:
 
137
                    raise DistutilsExecError(
 
138
                          "command '%s' failed with exit status %d"
 
139
                          % (cmd[0], exit_status))
 
140
            elif os.WIFSTOPPED(status):
 
141
                continue
 
142
            else:
 
143
                raise DistutilsExecError(
 
144
                      "unknown error executing '%s': termination status %d"
 
145
                      % (cmd[0], status))
 
146
 
 
147
 
 
148
def find_executable(executable, path=None):
 
149
    """Try to find 'executable' in the directories listed in 'path' (a
 
150
    string listing directories separated by 'os.pathsep'; defaults to
 
151
    os.environ['PATH']).  Returns the complete filename or None if not
 
152
    found.
 
153
    """
 
154
    if path is None:
 
155
        path = os.environ['PATH']
 
156
    paths = path.split(os.pathsep)
 
157
    (base, ext) = os.path.splitext(executable)
 
158
    if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
 
159
        executable = executable + '.exe'
 
160
    if not os.path.isfile(executable):
 
161
        for p in paths:
 
162
            f = os.path.join(p, executable)
 
163
            if os.path.isfile(f):
 
164
                # the file exists, we have a shot at spawn working
 
165
                return f
 
166
        return None
 
167
    else:
 
168
        return executable