Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 861d644

Browse files
author
Tarek Ziadé
committed
Merged revisions 73147 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r73147 | tarek.ziade | 2009-06-02 17:58:43 +0200 (Tue, 02 Jun 2009) | 1 line improved distutils.spawn test coverage + cleaned it up ........
1 parent 60fe6b0 commit 861d644

2 files changed

Lines changed: 63 additions & 24 deletions

File tree

Lib/distutils/spawn.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@
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
1315
from distutils import log
1416

1517
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+
"""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-
4143
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+
"""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

5659
def _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-
7779
def _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-
9999
def _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-
148146
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.
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)

Lib/distutils/tests/test_spawn.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
"""Tests for distutils.spawn."""
22
import unittest
3+
import os
4+
import time
5+
from test.support import captured_stdout
6+
37
from distutils.spawn import _nt_quote_args
8+
from distutils.spawn import spawn, find_executable
9+
from distutils.errors import DistutilsExecError
10+
from distutils.tests import support
411

5-
class SpawnTestCase(unittest.TestCase):
12+
class SpawnTestCase(support.TempdirManager,
13+
support.LoggingSilencer,
14+
unittest.TestCase):
615

716
def test_nt_quote_args(self):
817

@@ -13,6 +22,35 @@ def test_nt_quote_args(self):
1322
res = _nt_quote_args(args)
1423
self.assertEquals(res, wanted)
1524

25+
26+
@unittest.skipUnless(os.name in ('nt', 'posix'),
27+
'Runs only under posix or nt')
28+
def test_spawn(self):
29+
tmpdir = self.mkdtemp()
30+
31+
# creating something executable
32+
# through the shell that returns 1
33+
if os.name == 'posix':
34+
exe = os.path.join(tmpdir, 'foo.sh')
35+
self.write_file(exe, '#!/bin/sh\nexit 1')
36+
else:
37+
exe = os.path.join(tmpdir, 'foo.bat')
38+
self.write_file(exe, 'exit 1')
39+
40+
os.chmod(exe, 0o777)
41+
self.assertRaises(DistutilsExecError, spawn, [exe])
42+
43+
# now something that works
44+
if os.name == 'posix':
45+
exe = os.path.join(tmpdir, 'foo.sh')
46+
self.write_file(exe, '#!/bin/sh\nexit 0')
47+
else:
48+
exe = os.path.join(tmpdir, 'foo.bat')
49+
self.write_file(exe, 'exit 0')
50+
51+
os.chmod(exe, 0o777)
52+
spawn([exe]) # should work without any error
53+
1654
def test_suite():
1755
return unittest.makeSuite(SpawnTestCase)
1856

0 commit comments

Comments
 (0)