Skip to content

Commit baac45d

Browse files
Replace instances of common.exec_subproc
1 parent 9ca9722 commit baac45d

3 files changed

Lines changed: 5 additions & 120 deletions

File tree

Coupled_Drivers/common.py

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -228,73 +228,6 @@ def setup_runtime(common_env):
228228

229229
return runlen_sec
230230

231-
def exec_subproc_timeout(cmd, timeout_sec=10):
232-
'''
233-
Execute a given shell command with a timeout. Takes a list containing
234-
the commands to be run, and an integer timeout_sec for how long to
235-
wait for the command to run. Returns the return code from the process
236-
and the standard out from the command or 'None' if the command times out.
237-
'''
238-
process = subprocess.Popen(cmd, shell=False,
239-
stdin=subprocess.PIPE,
240-
stdout=subprocess.PIPE,
241-
stderr=subprocess.PIPE)
242-
timer = threading.Timer(timeout_sec, process.kill)
243-
try:
244-
timer.start()
245-
stdout, err = process.communicate()
246-
if err:
247-
sys.stderr.write('[SUBPROCESS ERROR] %s\n' % error)
248-
rcode = process.returncode
249-
finally:
250-
timer.cancel()
251-
if sys.version_info[0] >= 3:
252-
output = stdout.decode()
253-
else:
254-
output = stdout
255-
return rcode, output
256-
257-
258-
def exec_subproc(cmd, verbose=True):
259-
'''
260-
Execute given shell command. Takes a list containing the commands to be
261-
run, and a logical verbose which if set to true will write the output of
262-
the command to stdout.
263-
'''
264-
process = subprocess.Popen(cmd, shell=False,
265-
stdin=subprocess.PIPE,
266-
stdout=subprocess.PIPE,
267-
stderr=subprocess.PIPE)
268-
output, err = process.communicate()
269-
if verbose and output:
270-
sys.stdout.write('[SUBPROCESS OUTPUT] %s\n' % output)
271-
if err:
272-
sys.stderr.write('[SUBPROCESS ERROR] %s\n' % error)
273-
if sys.version_info[0] >= 3:
274-
output = output.decode()
275-
return process.returncode, output
276-
277-
278-
def __exec_subproc_true_shell(cmd, verbose=True):
279-
'''
280-
Execute given shell command, with shell=True. Only use this function if
281-
exec_subproc does not work correctly. Takes a list containing the commands
282-
to be run, and a logical verbose which if set to true will write the
283-
output of the command to stdout.
284-
'''
285-
process = subprocess.Popen(cmd, shell=True,
286-
stdin=subprocess.PIPE,
287-
stdout=subprocess.PIPE,
288-
stderr=subprocess.PIPE)
289-
output, err = process.communicate()
290-
if verbose and output:
291-
sys.stdout.write('[SUBPROCESS OUTPUT] %s\n' % output)
292-
if err:
293-
sys.stderr.write('[SUBPROCESS ERROR] %s\n' % error)
294-
if sys.version_info[0] >= 3:
295-
output = output.decode()
296-
return process.returncode, output
297-
298231

299232
def _calculate_ppn_values(nproc, nodes):
300233
'''

Postprocessing/common/utils.py

Lines changed: 3 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import os
2424
import errno
2525
import shutil
26-
import subprocess
26+
import shellout
2727
import timer
2828

2929

@@ -153,61 +153,13 @@ def finalcycle():
153153
return fcycle
154154

155155

156-
@timer.run_timer
157-
def exec_subproc(cmd, verbose=True, cwd=os.getcwd()):
158-
'''
159-
Execute given shell command.
160-
'cmd' input should be in the form of either a:
161-
string - "cd DIR; command arg1 arg2"
162-
list of words - ["command", "arg1", "arg2"]
163-
Optional arguments:
164-
verbose = False: only reproduce the command std.out upon
165-
failure of the command
166-
True: reproduce std.out regardless of outcome
167-
cwd = Directory in which to execute the command
168-
'''
169-
import shlex
170-
171-
cmd_array = [cmd]
172-
if not isinstance(cmd, list):
173-
cmd_array = cmd.split(';')
174-
for i, cmd in enumerate(cmd_array):
175-
# Use shlex.split to cope with arguments that contain whitespace
176-
cmd_array[i] = shlex.split(cmd)
177-
178-
# Initialise rcode, in the event there is no command
179-
rcode = 99
180-
output = 'No command provided'
181-
182-
for cmd in cmd_array:
183-
try:
184-
output = subprocess.check_output(cmd,
185-
stderr=subprocess.STDOUT,
186-
universal_newlines=True, cwd=cwd)
187-
rcode = 0
188-
if verbose:
189-
log_msg('[SUBPROCESS]: ' + str(output))
190-
except subprocess.CalledProcessError as exc:
191-
output = exc.output
192-
rcode = exc.returncode
193-
except OSError as exc:
194-
output = exc.strerror
195-
rcode = exc.errno
196-
if rcode != 0:
197-
msg = '[SUBPROCESS]: Command: {}\n[SUBPROCESS]: Error = {}:\n\t{}'
198-
log_msg(msg.format(' '.join(cmd), rcode, output), level='WARN')
199-
break
200-
201-
return rcode, output
202-
203-
204156
def get_utility_avail(utility):
205157
'''Return True/False if shell command is available'''
206158
try:
207159
status = shutil.which(utility)
208160
except AttributeError:
209161
# subprocess.getstatusoutput does not exist at Python2.7
210-
status, _ = utils.exec_subproc(utility + ' --help', verbose=False)
162+
status, _ = shellout.exec_subprocess(utility + ' --help')
211163

212164
return bool(status)
213165

@@ -496,7 +448,7 @@ def _mod_all_calendars_date(indate, delta, cal):
496448
cmd = '{} {} --calendar {} --offset {} --print-format ' \
497449
'%Y,%m,%d,%H,%M'.format(datecmd, dateinput, cal, offset)
498450

499-
rcode, output = exec_subproc(cmd, verbose=False)
451+
rcode, output = shellout._exec_subprocess(cmd)
500452
else:
501453
log_msg('add_period_to_date: Invalid date for conversion to '
502454
'ISO 8601 date representation: ' + str(outdate),

Utilities/lib/shellout.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
@timer.run_timer
9-
def _exec_subprocess(cmd, verbose=True, current_working_directory=os.getcwd()):
9+
def _exec_subprocess(cmd, verbose=False, current_working_directory=os.getcwd()):
1010
"""
1111
Execute a given shell command
1212
@@ -44,4 +44,4 @@ def _exec_subprocess(cmd, verbose=True, current_working_directory=os.getcwd()):
4444
output = exc.strerror
4545
rcode = exc.errno
4646

47-
return output, rcode
47+
return rcode, output

0 commit comments

Comments
 (0)