Skip to content

Commit 6409449

Browse files
committed
More complete I/O capturing.
- If I/O is captured, redirect I/O from subprocesses created with planemo.io. - Update Galaxy's commands.py to allow overriding sys object for I/O. - Eliminate one planemo call to Galaxy's commands.execute. Ping @erasche ... just for a second set of eyes.
1 parent 8e459a8 commit 6409449

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

planemo/commands/cmd_brew_init.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from tempfile import mkstemp
55

66
from planemo.cli import pass_context
7-
from galaxy.tools.deps import commands
7+
from planemo.io import shell
88

99

1010
INSTALL_SCRIPT = "https://raw.github.com/Homebrew/linuxbrew/go/install"
@@ -24,4 +24,4 @@ def cli(ctx):
2424
"""
2525
fname = mkstemp('install_brew')
2626
urllib.urlretrieve(INSTALL_SCRIPT, fname)
27-
commands.execute(["ruby", fname])
27+
shell(["ruby", fname])

planemo/io.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
def communicate(cmds, **kwds):
1919
info(cmds)
2020
p = commands.shell_process(cmds, **kwds)
21-
ret_val = p.communicate()
21+
if kwds.get("stdout", None) is None and commands.redirecting_io(sys=sys):
22+
output = commands.redirect_aware_commmunicate(p)
23+
else:
24+
output = p.communicate()
25+
2226
if p.returncode != 0:
2327
template = "Problem executing commands {0} - ({1}, {2})"
24-
msg = template.format(cmds, ret_val[0], ret_val[1])
28+
msg = template.format(cmds, output[0], output[1])
2529
raise RuntimeError(msg)
26-
return ret_val
30+
return output
2731

2832

2933
def shell(cmds, **kwds):

planemo_ext/galaxy/tools/deps/commands.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,49 @@
11
import os
22
import subprocess
3+
import sys as _sys
4+
5+
6+
def redirecting_io(sys=_sys):
7+
assert sys is not None
8+
# We are redirecting standard out and standard error.
9+
return not hasattr(sys.stdout, "fileno")
10+
11+
12+
def redirect_aware_commmunicate(p, sys=_sys):
13+
assert sys is not None
14+
out, err = p.communicate()
15+
if redirecting_io(sys=sys):
16+
if out:
17+
sys.stdout.write(out)
18+
out = None
19+
if err:
20+
sys.stderr.write(err)
21+
err = None
22+
return out, err
323

424

525
def shell(cmds, env=None, **kwds):
26+
sys = kwds.get("sys", _sys)
27+
assert sys is not None
628
p = shell_process(cmds, env, **kwds)
7-
return p.wait()
29+
if redirecting_io(sys=sys):
30+
redirect_aware_commmunicate(p, sys=sys)
31+
exit = p.returncode
32+
return exit
33+
else:
34+
return p.wait()
835

936

1037
def shell_process(cmds, env=None, **kwds):
38+
sys = kwds.get("sys", _sys)
1139
popen_kwds = dict(
1240
shell=True,
1341
)
42+
if kwds.get("stdout", None) is None and redirecting_io(sys=sys):
43+
popen_kwds["stdout"] = subprocess.PIPE
44+
if kwds.get("stderr", None) is None and redirecting_io(sys=sys):
45+
popen_kwds["stderr"] = subprocess.PIPE
46+
1447
popen_kwds.update(**kwds)
1548
if env:
1649
new_env = os.environ.copy()

tests/test_io.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from .test_utils import io
2+
3+
4+
def test_io_capture():
5+
with io.conditionally_captured_io(True, tee=False) as capture:
6+
io.warn("Problem...")
7+
assert capture[0]["data"] == "Problem..."
8+
9+
with io.conditionally_captured_io(True, tee=False) as capture:
10+
io.shell("echo 'Problem...'")
11+
assert capture[0]["data"] == "echo 'Problem...'"
12+
assert capture[1]["data"] == "Problem..."
13+
14+
with io.conditionally_captured_io(True, tee=False) as capture:
15+
io.communicate("echo 'Problem...'")
16+
assert capture[0]["data"] == "echo 'Problem...'"
17+
assert capture[1]["data"] == "Problem..."
18+
19+
with io.conditionally_captured_io(False, tee=False) as capture:
20+
io.communicate("echo 'Test...'")
21+
22+
assert capture is None

0 commit comments

Comments
 (0)