Skip to content

Commit

Permalink
Add options stdout and stderr to script executing actions #879
Browse files Browse the repository at this point in the history
  • Loading branch information
Bo Peng committed Feb 5, 2018
1 parent 85be47c commit 447b74a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 9 deletions.
74 changes: 66 additions & 8 deletions src/sos/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,79 @@ def run(self, **kwargs):
{'filename': sos_targets(script_file), 'script': self.script})
#
if env.config['run_mode'] == 'interactive':
# need to catch output and send to python output, which will in trun be hijacked by SoS notebook
from .utils import pexpect_run
ret = pexpect_run(cmd)
if 'stdout' in kwargs or 'stderr' in kwargs:
child = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, bufsize=0)
out, err = child.communicate()
if 'stdout' in kwargs:
with open(kwargs['stdout'], 'ab') as so:
so.write(out)
else:
sys.stdout.write(out.decode())

if 'stderr' in kwargs:
with open(kwargs['stderr'], 'ab') as se:
se.write(err)
else:
sys.stderr.write(err.decode())
ret = child.returncode
else:
# need to catch output and send to python output, which will in trun be hijacked by SoS notebook
from .utils import pexpect_run
ret = pexpect_run(cmd)
elif '__std_out__' in env.sos_dict and '__std_err__' in env.sos_dict:
if env.verbosity >= 1:
if 'stdout' in kwargs or 'stderr' in kwargs:
if 'stdout' in kwargs:
so = open(kwargs['stdout'], 'ab')
elif env.verbosity > 0:
so = open(env.sos_dict['__std_out__'], 'ab')
else:
so = subprocess.DEVNULL

if 'stderr' in kwargs:
se = open(kwargs['stderr'], 'ab')
elif env.verbosity > 1:
se = open(env.sos_dict['__std_err__'], 'ab')
else:
se = subprocess.DEVNULL

p = subprocess.Popen(cmd, shell=True, stderr=se, stdout=so)
ret = p.wait()

if so != subprocess.DEVNULL:
so.close()
if se != subprocess.DEVNULL:
se.close()

elif env.verbosity >= 1:
with open(env.sos_dict['__std_out__'], 'ab') as so, open(env.sos_dict['__std_err__'], 'ab') as se:
p = subprocess.Popen(cmd, shell=True, stderr=se, stdout=so)
ret = p.wait()
else:
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)
ret = p.wait()
ret = p.wait()
else:
p = subprocess.Popen(cmd, shell=True,
stderr=None if env.verbosity > 0 else subprocess.DEVNULL,
stdout=None if env.verbosity > 1 else subprocess.DEVNULL)
if 'stdout' in kwargs:
so = open(kwargs['stdout'], 'ab')
elif env.verbosity > 0:
so = None
else:
so = subprocess.DEVNULL

if 'stderr' in kwargs:
se = open(kwargs['stderr'], 'ab')
elif env.verbosity > 1:
se = None
else:
se = subprocess.DEVNULL

p = subprocess.Popen(cmd, shell=True, stderr=se, stdout=so)

ret = p.wait()
if 'stdout' in kwargs:
so.close()
if 'stderr' in kwargs:
se.close()
if ret != 0:
with open(debug_script_file, 'w') as sfile:
sfile.write(self.script)
Expand Down
2 changes: 1 addition & 1 deletion src/sos/syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
'cores', 'mem', 'shared', 'env', 'prepend_path', 'queue', 'to_host',
'from_host', 'map_vars', 'name', 'trunk_size', 'trunk_workers', 'tags']
SOS_ACTION_OPTIONS = ['workdir', 'docker_image', 'docker_file', 'active', 'input', 'output',
'allow_error', 'tracked']
'allow_error', 'tracked', 'stdout', 'stderr']

SOS_DIRECTIVES = ['input', 'output', 'depends', 'task', 'parameter']
SOS_SECTION_OPTIONS = ['skip', 'provides', 'shared', 'workdir']
Expand Down

0 comments on commit 447b74a

Please sign in to comment.