def backtick(self, command):
import subprocess
- if SCons.Util.is_List(command):
- p = subprocess.Popen(command,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- universal_newlines=True)
- else:
- p = subprocess.Popen(command,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- universal_newlines=True,
- shell=True)
+ # common arguments
+ kw = { 'stdout' : subprocess.PIPE,
+ 'stderr' : subprocess.PIPE,
+ 'universal_newlines' : True,
+ }
+ # if the command is a list, assume it's been quoted
+ # othewise force a shell
+ if not SCons.Util.is_List(command): kw['shell'] = True
+ # a SubstutionEnvironment has no ENV, so only add it
+ # to the args if it exists
+ e = self._dict.get('ENV')
+ if e: kw['env'] = e
+ # run constructed command
+ #FUTURE p = subprocess.Popen(command, **kw)
+ p = apply(subprocess.Popen, (command,), kw)
out = p.stdout.read()
p.stdout.close()
err = p.stderr.read()
test.write('fail.py', """\
import sys
sys.exit(1)
+""")
+ test.write('echo.py', """\
+import os, sys
+sys.stdout.write(os.environ['ECHO'] + '\\n')
+sys.exit(0)
""")
save_stderr = sys.stderr
python = '"' + sys.executable + '"'
try:
+ sys.stderr = StringIO.StringIO()
cmd = '%s %s' % (python, test.workpath('stdout.py'))
output = env.backtick(cmd)
-
+ errout = sys.stderr.getvalue()
assert output == 'this came from stdout.py\n', output
+ assert errout == '', errout
sys.stderr = StringIO.StringIO()
-
cmd = '%s %s' % (python, test.workpath('stderr.py'))
output = env.backtick(cmd)
errout = sys.stderr.getvalue()
-
assert output == '', output
assert errout == 'this came from stderr.py\n', errout
sys.stderr = StringIO.StringIO()
-
cmd = '%s %s' % (python, test.workpath('fail.py'))
try:
env.backtick(cmd)
else:
self.fail("did not catch expected OSError")
+ sys.stderr = StringIO.StringIO()
+ cmd = '%s %s' % (python, test.workpath('echo.py'))
+ env['ENV'] = os.environ.copy()
+ env['ENV']['ECHO'] = 'this came from ECHO'
+ output = env.backtick(cmd)
+ errout = sys.stderr.getvalue()
+ assert output == 'this came from ECHO\n', output
+ assert errout == '', errout
+
finally:
sys.stderr = save_stderr