Updated run_program() and popen_program()
* Use dicts for clarity * Support cwd flag
This commit is contained in:
parent
a2ef06e6db
commit
d9554314d5
1 changed files with 23 additions and 13 deletions
|
|
@ -405,19 +405,24 @@ def ping(addr='google.com'):
|
||||||
|
|
||||||
def popen_program(cmd, pipe=False, minimized=False, shell=False, **kwargs):
|
def popen_program(cmd, pipe=False, minimized=False, shell=False, **kwargs):
|
||||||
"""Run program and return a subprocess.Popen object."""
|
"""Run program and return a subprocess.Popen object."""
|
||||||
startupinfo=None
|
cmd_kwargs = {'args': cmd, 'shell': shell}
|
||||||
|
|
||||||
if minimized:
|
if minimized:
|
||||||
startupinfo = subprocess.STARTUPINFO()
|
startupinfo = subprocess.STARTUPINFO()
|
||||||
startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
|
startupinfo.dwFlags = subprocess.STARTF_USESHOWWINDOW
|
||||||
startupinfo.wShowWindow = 6
|
startupinfo.wShowWindow = 6
|
||||||
|
cmd_kwargs['startupinfo'] = startupinfo
|
||||||
|
|
||||||
if pipe:
|
if pipe:
|
||||||
popen_obj = subprocess.Popen(cmd, shell=shell, startupinfo=startupinfo,
|
cmd_kwargs.update({
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
'stdout': subprocess.PIPE,
|
||||||
else:
|
'stderr': subprocess.PIPE,
|
||||||
popen_obj = subprocess.Popen(cmd, shell=shell, startupinfo=startupinfo)
|
})
|
||||||
|
|
||||||
return popen_obj
|
if 'cwd' in kwargs:
|
||||||
|
cmd_kwargs['cwd'] = kwargs['cwd']
|
||||||
|
|
||||||
|
return subprocess.Popen(**cmd_kwargs)
|
||||||
|
|
||||||
def print_error(*args, **kwargs):
|
def print_error(*args, **kwargs):
|
||||||
"""Prints message to screen in RED."""
|
"""Prints message to screen in RED."""
|
||||||
|
|
@ -456,7 +461,7 @@ def print_log(message='', end='\n', timestamp=True):
|
||||||
line = line,
|
line = line,
|
||||||
end = end))
|
end = end))
|
||||||
|
|
||||||
def run_program(cmd, args=[], check=True, pipe=True, shell=False):
|
def run_program(cmd, args=[], check=True, pipe=True, shell=False, **kwargs):
|
||||||
"""Run program and return a subprocess.CompletedProcess object."""
|
"""Run program and return a subprocess.CompletedProcess object."""
|
||||||
if args:
|
if args:
|
||||||
# Deprecated so let's raise an exception to find & fix all occurances
|
# Deprecated so let's raise an exception to find & fix all occurances
|
||||||
|
|
@ -466,13 +471,18 @@ def run_program(cmd, args=[], check=True, pipe=True, shell=False):
|
||||||
if shell:
|
if shell:
|
||||||
cmd = ' '.join(cmd)
|
cmd = ' '.join(cmd)
|
||||||
|
|
||||||
if pipe:
|
cmd_kwargs = {'args': cmd, 'check': check, 'shell': shell}
|
||||||
process_return = subprocess.run(cmd, check=check, shell=shell,
|
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
||||||
else:
|
|
||||||
process_return = subprocess.run(cmd, check=check, shell=shell)
|
|
||||||
|
|
||||||
return process_return
|
if pipe:
|
||||||
|
cmd_kwargs.update({
|
||||||
|
'stdout': subprocess.PIPE,
|
||||||
|
'stderr': subprocess.PIPE,
|
||||||
|
})
|
||||||
|
|
||||||
|
if 'cwd' in kwargs:
|
||||||
|
cmd_kwargs['cwd'] = kwargs['cwd']
|
||||||
|
|
||||||
|
return subprocess.run(**cmd_kwargs)
|
||||||
|
|
||||||
def set_title(title='~Some Title~'):
|
def set_title(title='~Some Title~'):
|
||||||
"""Set title.
|
"""Set title.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue