annotate Lib/distutils/spawn.py @ 74135:9c19df6c8ea0 3.2

News entry for Issue12798
author Senthil Kumaran <senthil@uthcode.com>
date Fri, 23 Dec 2011 11:11:21 +0800
parents 3e26c9033306
children 147b9d530dd0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
1 """distutils.spawn
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
2
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
3 Provides the 'spawn()' function, a front-end to various platform-
13803
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
4 specific functions for launching another program in a sub-process.
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
5 Also provides the 'find_executable()' to search the path for a given
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
6 executable name.
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
7 """
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
8
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
9 import sys
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
10 import os
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
11
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
12 from distutils.errors import DistutilsPlatformError, DistutilsExecError
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21851
diff changeset
13 from distutils import log
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
14
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
15 def spawn(cmd, search_path=1, verbose=0, dry_run=0):
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
16 """Run another program, specified as a command list 'cmd', in a new process.
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
17
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
18 'cmd' is just the argument list for the new process, ie.
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
19 cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
20 There is no way to run a program with a name different from that of its
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
21 executable.
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
22
26324
05d6ade3e92b Reflow comment
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
23 If 'search_path' is true (the default), the system's executable
05d6ade3e92b Reflow comment
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
24 search path will be used to find the program; otherwise, cmd[0]
05d6ade3e92b Reflow comment
Andrew M. Kuchling <amk@amk.ca>
parents: 26285
diff changeset
25 must be the exact path to the executable. If 'dry_run' is true,
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
26 the command will not actually be run.
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
27
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
28 Raise DistutilsExecError if running the program fails in any way; just
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
29 return on success.
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
30 """
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
31 if os.name == 'posix':
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21851
diff changeset
32 _spawn_posix(cmd, search_path, dry_run=dry_run)
11257
9286d3e1474c [from 1999/08/28]
Greg Ward <gward@python.net>
parents: 11250
diff changeset
33 elif os.name == 'nt':
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21851
diff changeset
34 _spawn_nt(cmd, search_path, dry_run=dry_run)
21851
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
35 elif os.name == 'os2':
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21851
diff changeset
36 _spawn_os2(cmd, search_path, dry_run=dry_run)
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
37 else:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
38 raise DistutilsPlatformError(
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
39 "don't know how to spawn programs on platform '%s'" % os.name)
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
40
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
41 def _nt_quote_args(args):
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
42 """Quote command-line arguments for DOS/Windows conventions.
11257
9286d3e1474c [from 1999/08/28]
Greg Ward <gward@python.net>
parents: 11250
diff changeset
43
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
44 Just wraps every argument which contains blanks in double quotes, and
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
45 returns a new argument list.
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
46 """
11933
68e0ef052815 Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents: 11897
diff changeset
47 # XXX this doesn't seem very robust to me -- but if the Windows guys
68e0ef052815 Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents: 11897
diff changeset
48 # say it'll work, I guess I'll have to accept it. (What if an arg
68e0ef052815 Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents: 11897
diff changeset
49 # contains quotes? What other magic characters, other than spaces,
68e0ef052815 Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents: 11897
diff changeset
50 # have to be escaped? Is there an escaping mechanism other than
68e0ef052815 Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents: 11897
diff changeset
51 # quoting?)
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
52 for i, arg in enumerate(args):
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
53 if ' ' in arg:
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
54 args[i] = '"%s"' % arg
12058
c6cb0d266839 Duh, it helps if '_nt_quote_args()' actually returns the mutated list,
Greg Ward <gward@python.net>
parents: 12043
diff changeset
55 return args
11933
68e0ef052815 Added '_nt_quote_args()' to deal with whitespace in command-line arguments
Greg Ward <gward@python.net>
parents: 11897
diff changeset
56
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
57 def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
11250
b3f1abd3027f Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents: 11198
diff changeset
58 executable = cmd[0]
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
59 cmd = _nt_quote_args(cmd)
11250
b3f1abd3027f Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents: 11198
diff changeset
60 if search_path:
13803
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
61 # either we find one or it stays the same
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14861
diff changeset
62 executable = find_executable(executable) or executable
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40775
diff changeset
63 log.info(' '.join([executable] + cmd[1:]))
11250
b3f1abd3027f Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents: 11198
diff changeset
64 if not dry_run:
b3f1abd3027f Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents: 11198
diff changeset
65 # spawn for NT requires a full path to the .exe
11665
e998b350a4bb Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 11257
diff changeset
66 try:
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
67 rc = os.spawnv(os.P_WAIT, executable, cmd)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 33965
diff changeset
68 except OSError as exc:
11665
e998b350a4bb Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 11257
diff changeset
69 # this seems to happen when the command isn't found
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
70 raise DistutilsExecError(
44542
d0465e782b01 Fix more exception slicing.
Georg Brandl <georg@python.org>
parents: 43026
diff changeset
71 "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
11250
b3f1abd3027f Patch from Perry Stoll: support for Windows.
Greg Ward <gward@python.net>
parents: 11198
diff changeset
72 if rc != 0:
11665
e998b350a4bb Catch OSError from 'spawnv()' in '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 11257
diff changeset
73 # and this reflects the command running but failing
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
74 raise DistutilsExecError(
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
75 "command '%s' failed with exit status %d" % (cmd[0], rc))
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
76
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
77 def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0):
21851
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
78 executable = cmd[0]
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
79 if search_path:
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
80 # either we find one or it stays the same
32736
c3a096bfbd6b Whitespace normalization, via reindent.py.
Tim Peters <tim.peters@gmail.com>
parents: 31586
diff changeset
81 executable = find_executable(executable) or executable
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40775
diff changeset
82 log.info(' '.join([executable] + cmd[1:]))
21851
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
83 if not dry_run:
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
84 # spawnv for OS/2 EMX requires a full path to the .exe
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
85 try:
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
86 rc = os.spawnv(os.P_WAIT, executable, cmd)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 33965
diff changeset
87 except OSError as exc:
21851
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
88 # this seems to happen when the command isn't found
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
89 raise DistutilsExecError(
44542
d0465e782b01 Fix more exception slicing.
Georg Brandl <georg@python.org>
parents: 43026
diff changeset
90 "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
21851
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
91 if rc != 0:
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
92 # and this reflects the command running but failing
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
93 log.debug("command '%s' failed with exit status %d" % (cmd[0], rc))
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
94 raise DistutilsExecError(
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
95 "command '%s' failed with exit status %d" % (cmd[0], rc))
21851
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
96
71049
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
97 if sys.platform == 'darwin':
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
98 from distutils import sysconfig
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
99 _cfg_target = None
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
100 _cfg_target_split = None
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
101
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
102 def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40775
diff changeset
103 log.info(' '.join(cmd))
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
104 if dry_run:
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
105 return
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
106 exec_fn = search_path and os.execvp or os.execv
71049
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
107 exec_args = [cmd[0], cmd]
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
108 if sys.platform == 'darwin':
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
109 global _cfg_target, _cfg_target_split
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
110 if _cfg_target is None:
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
111 _cfg_target = sysconfig.get_config_var(
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
112 'MACOSX_DEPLOYMENT_TARGET') or ''
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
113 if _cfg_target:
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
114 _cfg_target_split = [int(x) for x in _cfg_target.split('.')]
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
115 if _cfg_target:
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
116 # ensure that the deployment target of build process is not less
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
117 # than that used when the interpreter was built. This ensures
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
118 # extension modules are built with correct compatibility values
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
119 cur_target = os.environ.get('MACOSX_DEPLOYMENT_TARGET', _cfg_target)
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
120 if _cfg_target_split > [int(x) for x in cur_target.split('.')]:
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
121 my_msg = ('$MACOSX_DEPLOYMENT_TARGET mismatch: '
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
122 'now "%s" but "%s" during configure'
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
123 % (cur_target, _cfg_target))
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
124 raise DistutilsPlatformError(my_msg)
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
125 env = dict(os.environ,
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
126 MACOSX_DEPLOYMENT_TARGET=cur_target)
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
127 exec_fn = search_path and os.execvpe or os.execve
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
128 exec_args.append(env)
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
129 pid = os.fork()
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
130 if pid == 0: # in the child
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
131 try:
71049
b95eee310931 Issue #9516: Change distutils to no longer globally attempt to check and
Ned Deily <nad@acm.org>
parents: 54522
diff changeset
132 exec_fn(*exec_args)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 33965
diff changeset
133 except OSError as e:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
134 sys.stderr.write("unable to execute %s: %s\n"
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
135 % (cmd[0], e.strerror))
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
136 os._exit(1)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 14861
diff changeset
137
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
138 sys.stderr.write("unable to execute %s for unknown reasons" % cmd[0])
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
139 os._exit(1)
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
140 else: # in the parent
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
141 # Loop until the child either exits or is terminated by a signal
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
142 # (ie. keep waiting if it's merely stopped)
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
143 while True:
31586
54685d9ffb87 Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26324
diff changeset
144 try:
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
145 pid, status = os.waitpid(pid, 0)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 33965
diff changeset
146 except OSError as exc:
31586
54685d9ffb87 Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26324
diff changeset
147 import errno
54685d9ffb87 Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26324
diff changeset
148 if exc.errno == errno.EINTR:
54685d9ffb87 Make _spawn_posix be ready for EINTR. waitpid(2) can be interrupted
Hye-Shik Chang <hyeshik@gmail.com>
parents: 26324
diff changeset
149 continue
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
150 raise DistutilsExecError(
44542
d0465e782b01 Fix more exception slicing.
Georg Brandl <georg@python.org>
parents: 43026
diff changeset
151 "command '%s' failed: %s" % (cmd[0], exc.args[-1]))
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
152 if os.WIFSIGNALED(status):
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
153 raise DistutilsExecError(
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
154 "command '%s' terminated by signal %d"
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
155 % (cmd[0], os.WTERMSIG(status)))
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
156 elif os.WIFEXITED(status):
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
157 exit_status = os.WEXITSTATUS(status)
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
158 if exit_status == 0:
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
159 return # hey, it succeeded!
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
160 else:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
161 raise DistutilsExecError(
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
162 "command '%s' failed with exit status %d"
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
163 % (cmd[0], exit_status))
14861
09c14ae5c2f5 Reformat docstrings.
Greg Ward <gward@python.net>
parents: 13803
diff changeset
164 elif os.WIFSTOPPED(status):
11198
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
165 continue
e8b24cd17df2 Module to spawn sub-commands in a platform-independent way.
Greg Ward <gward@python.net>
parents:
diff changeset
166 else:
43026
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
167 raise DistutilsExecError(
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
168 "unknown error executing '%s': termination status %d"
a6d3930b5613 General cleanup, raise normalization in Lib/distutils.
Collin Winter <collinw@gmail.com>
parents: 41480
diff changeset
169 % (cmd[0], status))
13803
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
170
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
171 def find_executable(executable, path=None):
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
172 """Tries to find 'executable' in the directories listed in 'path'.
13803
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
173
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
174 A string listing directories separated by 'os.pathsep'; defaults to
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
175 os.environ['PATH']. Returns the complete filename or None if not found.
13803
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
176 """
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
177 if path is None:
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
178 path = os.environ['PATH']
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
179
41480
99027c2b3fd2 Remove functions in string module that are also string methods. Also remove:
Neal Norwitz <nnorwitz@gmail.com>
parents: 40775
diff changeset
180 paths = path.split(os.pathsep)
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
181 base, ext = os.path.splitext(executable)
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
182
21851
e0fc286d57a4 OS/2 patches by Andrew I MacIntyre for distutils.
Marc-André Lemburg <mal@egenix.com>
parents: 21267
diff changeset
183 if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
13803
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
184 executable = executable + '.exe'
54522
5e4d070565e7 Merged revisions 73147 via svnmerge from
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 44542
diff changeset
185
13803
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
186 if not os.path.isfile(executable):
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
187 for p in paths:
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
188 f = os.path.join(p, executable)
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
189 if os.path.isfile(f):
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
190 # the file exists, we have a shot at spawn working
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
191 return f
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
192 return None
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
193 else:
e8b22bc88d95 Rene Liebscher: factor 'find_executable()' out of '_spawn_nt()'.
Greg Ward <gward@python.net>
parents: 12058
diff changeset
194 return executable