wrapper to subprocess.run

This commit is contained in:
Martin Diehl 2021-08-31 07:11:30 +02:00
parent e7530e0897
commit a809650a07
1 changed files with 13 additions and 7 deletions

View File

@ -17,8 +17,8 @@ from . import version
# limit visibility # limit visibility
__all__=[ __all__=[
'srepr', 'srepr',
'emph','deemph','warn','strikeout', 'emph', 'deemph', 'warn', 'strikeout',
'execute', 'execute', 'run',
'natural_sort', 'natural_sort',
'show_progress', 'show_progress',
'scale_to_coprime', 'scale_to_coprime',
@ -144,9 +144,9 @@ def strikeout(what):
return _colors['crossout']+srepr(what)+_colors['end_color'] return _colors['crossout']+srepr(what)+_colors['end_color']
def execute(cmd,wd='./',env=None): def run(cmd,wd='./',env=None,timeout=None):
""" """
Execute command. Run a command.
Parameters Parameters
---------- ----------
@ -156,20 +156,23 @@ def execute(cmd,wd='./',env=None):
Working directory of process. Defaults to ./ . Working directory of process. Defaults to ./ .
env : dict, optional env : dict, optional
Environment for execution. Environment for execution.
timeout : integer, optional
Timeout in seconds.
Returns Returns
------- -------
stdout, stderr : str stdout, stderr : (str, str)
Output of the executed command. Output of the executed command.
""" """
print(f"executing '{cmd}' in '{wd}'") print(f"running '{cmd}' in '{wd}'")
process = subprocess.run(shlex.split(cmd), process = subprocess.run(shlex.split(cmd),
stdout = subprocess.PIPE, stdout = subprocess.PIPE,
stderr = subprocess.PIPE, stderr = subprocess.PIPE,
env = os.environ if env is None else env, env = os.environ if env is None else env,
cwd = wd, cwd = wd,
encoding = 'utf-8') encoding = 'utf-8',
timeout = timeout)
if process.returncode != 0: if process.returncode != 0:
print(process.stdout) print(process.stdout)
@ -179,6 +182,9 @@ def execute(cmd,wd='./',env=None):
return process.stdout, process.stderr return process.stdout, process.stderr
execute = run
def natural_sort(key): def natural_sort(key):
""" """
Natural sort. Natural sort.