88
99__revision__ = "$Id$"
1010
11- import sys , os
12- from distutils .errors import *
11+ import sys
12+ import os
13+
14+ from distutils .errors import DistutilsPlatformError , DistutilsExecError
1315from distutils import log
1416
1517def 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+ """Run another program, specified as a command list 'cmd', in a new process.
19+
20+ 'cmd' is just the argument list for the new process, ie.
1821 cmd[0] is the program to run and cmd[1:] are the rest of its arguments.
1922 There is no way to run a program with a name different from that of its
2023 executable.
@@ -37,20 +40,20 @@ def spawn(cmd, search_path=1, verbose=0, dry_run=0):
3740 raise DistutilsPlatformError (
3841 "don't know how to spawn programs on platform '%s'" % os .name )
3942
40-
4143def _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+ """Quote command-line arguments for DOS/Windows conventions.
45+
46+ Just wraps every argument which contains blanks in double quotes, and
4447 returns a new argument list.
4548 """
4649 # XXX this doesn't seem very robust to me -- but if the Windows guys
4750 # say it'll work, I guess I'll have to accept it. (What if an arg
4851 # contains quotes? What other magic characters, other than spaces,
4952 # have to be escaped? Is there an escaping mechanism other than
5053 # quoting?)
51- for i in range ( len ( args ) ):
52- if args [ i ]. find ( ' ' ) != - 1 :
53- args [i ] = '"%s"' % args [ i ]
54+ for i , arg in enumerate ( args ):
55+ if ' ' in arg :
56+ args [i ] = '"%s"' % arg
5457 return args
5558
5659def _spawn_nt (cmd , search_path = 1 , verbose = 0 , dry_run = 0 ):
@@ -73,10 +76,8 @@ def _spawn_nt(cmd, search_path=1, verbose=0, dry_run=0):
7376 raise DistutilsExecError (
7477 "command '%s' failed with exit status %d" % (cmd [0 ], rc ))
7578
76-
7779def _spawn_os2 (cmd , search_path = 1 , verbose = 0 , dry_run = 0 ):
7880 executable = cmd [0 ]
79- #cmd = _nt_quote_args(cmd)
8081 if search_path :
8182 # either we find one or it stays the same
8283 executable = find_executable (executable ) or executable
@@ -91,17 +92,15 @@ def _spawn_os2(cmd, search_path=1, verbose=0, dry_run=0):
9192 "command '%s' failed: %s" % (cmd [0 ], exc .args [- 1 ]))
9293 if rc != 0 :
9394 # and this reflects the command running but failing
94- print ("command '%s' failed with exit status %d" % (cmd [0 ], rc ))
95+ log . debug ("command '%s' failed with exit status %d" % (cmd [0 ], rc ))
9596 raise DistutilsExecError (
9697 "command '%s' failed with exit status %d" % (cmd [0 ], rc ))
9798
98-
9999def _spawn_posix (cmd , search_path = 1 , verbose = 0 , dry_run = 0 ):
100100 log .info (' ' .join (cmd ))
101101 if dry_run :
102102 return
103103 exec_fn = search_path and os .execvp or os .execv
104-
105104 pid = os .fork ()
106105 if pid == 0 : # in the child
107106 try :
@@ -118,7 +117,7 @@ def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
118117 # (ie. keep waiting if it's merely stopped)
119118 while True :
120119 try :
121- ( pid , status ) = os .waitpid (pid , 0 )
120+ pid , status = os .waitpid (pid , 0 )
122121 except OSError as exc :
123122 import errno
124123 if exc .errno == errno .EINTR :
@@ -132,7 +131,7 @@ def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
132131 elif os .WIFEXITED (status ):
133132 exit_status = os .WEXITSTATUS (status )
134133 if exit_status == 0 :
135- return # hey, it succeeded!
134+ return # hey, it succeeded!
136135 else :
137136 raise DistutilsExecError (
138137 "command '%s' failed with exit status %d"
@@ -144,19 +143,21 @@ def _spawn_posix(cmd, search_path=1, verbose=0, dry_run=0):
144143 "unknown error executing '%s': termination status %d"
145144 % (cmd [0 ], status ))
146145
147-
148146def 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.
147+ """Tries to find 'executable' in the directories listed in 'path'.
148+
149+ A string listing directories separated by 'os.pathsep'; defaults to
150+ os.environ['PATH']. Returns the complete filename or None if not found.
153151 """
154152 if path is None :
155153 path = os .environ ['PATH' ]
154+
156155 paths = path .split (os .pathsep )
157- (base , ext ) = os .path .splitext (executable )
156+ base , ext = os .path .splitext (executable )
157+
158158 if (sys .platform == 'win32' or os .name == 'os2' ) and (ext != '.exe' ):
159159 executable = executable + '.exe'
160+
160161 if not os .path .isfile (executable ):
161162 for p in paths :
162163 f = os .path .join (p , executable )
0 commit comments