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