further cleanup in regards the commands that are being used within core
This commit is contained in:
parent
870d87804b
commit
6211b09585
15 changed files with 322 additions and 316 deletions
|
@ -14,6 +14,8 @@ import resource
|
|||
|
||||
from core import logger
|
||||
|
||||
DEVNULL = open(os.devnull, "wb")
|
||||
|
||||
|
||||
def closeonexec(fd):
|
||||
"""
|
||||
|
@ -112,18 +114,18 @@ def maketuplefromstr(s, value_type):
|
|||
return tuple(value_type(i) for i in values)
|
||||
|
||||
|
||||
def split_cmd(cmd):
|
||||
def split_args(args):
|
||||
"""
|
||||
Convenience method for splitting potential string commands into a shell-like syntax list.
|
||||
|
||||
:param list/str cmd: command list or string
|
||||
:param list/str args: command list or string
|
||||
:return: shell-like syntax list
|
||||
:rtype: list
|
||||
"""
|
||||
# split shell string to shell array for convenience
|
||||
if type(cmd) == str:
|
||||
cmd = shlex.split(cmd)
|
||||
return cmd
|
||||
if type(args) == str:
|
||||
args = shlex.split(args)
|
||||
return args
|
||||
|
||||
|
||||
def mutecall(*args, **kwargs):
|
||||
|
@ -135,25 +137,11 @@ def mutecall(*args, **kwargs):
|
|||
:return: command result
|
||||
:rtype: int
|
||||
"""
|
||||
kwargs["stdout"] = open(os.devnull, "w")
|
||||
kwargs["stdout"] = DEVNULL
|
||||
kwargs["stderr"] = subprocess.STDOUT
|
||||
return subprocess.call(*args, **kwargs)
|
||||
|
||||
|
||||
def check_alloutput(cmd, **kwargs):
|
||||
"""
|
||||
Convenience wrapper to run subprocess.check_output and include stderr as well.
|
||||
|
||||
:param list[str]/str cmd: command arguments to run
|
||||
:param dict kwargs: option for running subprocess.check_output, beyond setting stderr to stdout
|
||||
:return: combined stdout and stderr
|
||||
:raises subprocess.CalledProcessError: when a non-zero exit status is encountered
|
||||
"""
|
||||
cmd = split_cmd(cmd)
|
||||
kwargs["stderr"] = subprocess.STDOUT
|
||||
return subprocess.check_output(cmd, **kwargs)
|
||||
|
||||
|
||||
def mutecheck_call(*args, **kwargs):
|
||||
"""
|
||||
Run a muted check call command.
|
||||
|
@ -163,7 +151,7 @@ def mutecheck_call(*args, **kwargs):
|
|||
:return: command result
|
||||
:rtype: int
|
||||
"""
|
||||
kwargs["stdout"] = open(os.devnull, "w")
|
||||
kwargs["stdout"] = DEVNULL
|
||||
kwargs["stderr"] = subprocess.STDOUT
|
||||
return subprocess.check_call(*args, **kwargs)
|
||||
|
||||
|
@ -189,7 +177,7 @@ def mutespawn(*args, **kwargs):
|
|||
:return: process id of the command
|
||||
:rtype: int
|
||||
"""
|
||||
kwargs["stdout"] = open(os.devnull, "w")
|
||||
kwargs["stdout"] = DEVNULL
|
||||
kwargs["stderr"] = subprocess.STDOUT
|
||||
return subprocess.Popen(*args, **kwargs).pid
|
||||
|
||||
|
@ -229,48 +217,73 @@ def mutedetach(*args, **kwargs):
|
|||
:rtype: int
|
||||
"""
|
||||
kwargs["preexec_fn"] = detachinit
|
||||
kwargs["stdout"] = open(os.devnull, "w")
|
||||
kwargs["stdout"] = DEVNULL
|
||||
kwargs["stderr"] = subprocess.STDOUT
|
||||
return subprocess.Popen(*args, **kwargs).pid
|
||||
|
||||
|
||||
def cmd_output(cmd):
|
||||
def cmd(args, wait=True):
|
||||
"""
|
||||
Runs a command on and returns the exit status.
|
||||
|
||||
:param list[str]|str args: command arguments
|
||||
:param bool wait: wait for command to end or not
|
||||
:return: command status
|
||||
:rtype: int
|
||||
"""
|
||||
args = split_args(args)
|
||||
try:
|
||||
p = subprocess.Popen(args)
|
||||
if not wait:
|
||||
return 0
|
||||
return p.wait()
|
||||
except OSError:
|
||||
raise subprocess.CalledProcessError(-1, args)
|
||||
|
||||
|
||||
def cmd_output(args):
|
||||
"""
|
||||
Execute a command on the host and return a tuple containing the exit status and result string. stderr output
|
||||
is folded into the stdout result string.
|
||||
|
||||
:param list[str]/str cmd: command arguments
|
||||
:param list[str]|str args: command arguments
|
||||
:return: command status and stdout
|
||||
:rtype: tuple[int, str]
|
||||
:raises subprocess.CalledProcessError: when the file to execute is not found
|
||||
"""
|
||||
# split shell string to shell array for convenience
|
||||
cmd = split_cmd(cmd)
|
||||
p = subprocess.Popen(cmd, stdin=open(os.devnull, "r"), stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
stdout, _ = p.communicate()
|
||||
status = p.wait()
|
||||
return status, stdout
|
||||
args = split_args(args)
|
||||
try:
|
||||
p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||
stdout, _ = p.communicate()
|
||||
status = p.wait()
|
||||
return status, stdout
|
||||
except OSError:
|
||||
raise subprocess.CalledProcessError(-1, args)
|
||||
|
||||
|
||||
def check_cmd(cmd, **kwargs):
|
||||
def check_cmd(args, **kwargs):
|
||||
"""
|
||||
Execute a command on the host and return a tuple containing the exit status and result string. stderr output
|
||||
is folded into the stdout result string.
|
||||
|
||||
:param list[str]/str cmd: command arguments
|
||||
:param list[str]|str args: command arguments
|
||||
:param dict kwargs: keyword arguments to pass to subprocess.Popen
|
||||
:return: command status and stdout
|
||||
:rtype: tuple[int, str]
|
||||
:raises subprocess.CalledProcessError: when there is a non-zero exit status
|
||||
:raises subprocess.CalledProcessError: when there is a non-zero exit status or the file to execute is not found
|
||||
"""
|
||||
kwargs["stdout"] = subprocess.PIPE
|
||||
kwargs["stderr"] = subprocess.STDOUT
|
||||
cmd = split_cmd(cmd)
|
||||
p = subprocess.Popen(cmd, **kwargs)
|
||||
stdout, _ = p.communicate()
|
||||
status = p.wait()
|
||||
if status:
|
||||
raise subprocess.CalledProcessError(status, cmd, stdout)
|
||||
return status, stdout
|
||||
args = split_args(args)
|
||||
try:
|
||||
p = subprocess.Popen(args, **kwargs)
|
||||
stdout, _ = p.communicate()
|
||||
status = p.wait()
|
||||
if status:
|
||||
raise subprocess.CalledProcessError(status, args, stdout)
|
||||
return status, stdout
|
||||
except OSError:
|
||||
raise subprocess.CalledProcessError(-1, args)
|
||||
|
||||
|
||||
def hexdump(s, bytes_per_word=2, words_per_line=8):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue