Fix copy-paste bug. Refactor and simplify code.
This commit is contained in:
parent
be4668bf2d
commit
8cfa1c37d0
3 changed files with 37 additions and 41 deletions
|
@ -203,12 +203,6 @@ class Local(object):
|
||||||
self.log.trace("Local mkdir: %s", path)
|
self.log.trace("Local mkdir: %s", path)
|
||||||
os.makedirs(path, exist_ok=True)
|
os.makedirs(path, exist_ok=True)
|
||||||
|
|
||||||
def _log_std_fd(self, stdfd, which, quiet, save_output):
|
|
||||||
if not quiet and save_output and stdfd is not None:
|
|
||||||
stdfd.seek(0, 0)
|
|
||||||
self.log.trace("Local {}:\n{}\n".format(
|
|
||||||
which, stdfd.read().decode()))
|
|
||||||
|
|
||||||
def run(self, command, env=None, return_output=False, message_prefix=None,
|
def run(self, command, env=None, return_output=False, message_prefix=None,
|
||||||
stdout=None, stderr=None, save_output=True, quiet_mode=False):
|
stdout=None, stderr=None, save_output=True, quiet_mode=False):
|
||||||
"""Run the given command with the given environment.
|
"""Run the given command with the given environment.
|
||||||
|
@ -219,14 +213,19 @@ class Local(object):
|
||||||
"list or tuple argument expected, got: %s" % command)
|
"list or tuple argument expected, got: %s" % command)
|
||||||
|
|
||||||
quiet = self.quiet_mode or quiet_mode
|
quiet = self.quiet_mode or quiet_mode
|
||||||
|
do_save_output = save_output and not quiet
|
||||||
|
|
||||||
close_stdout = False
|
close_stdout = False
|
||||||
close_stderr = False
|
close_stderr = False
|
||||||
if not quiet and save_output and not return_output and stdout is None:
|
if quiet:
|
||||||
stdout = util._get_std_fd(self, 'stdout')
|
stderr = subprocess.DEVNULL
|
||||||
|
stdout = subprocess.DEVNULL
|
||||||
|
elif do_save_output:
|
||||||
|
if not return_output and stdout is None:
|
||||||
|
stdout = util.get_std_fd(self.stdout_base_path, 'local')
|
||||||
close_stdout = True
|
close_stdout = True
|
||||||
if not quiet and save_output and stderr is None:
|
if stderr is None:
|
||||||
stderr = util._get_std_fd(self, 'stderr')
|
stderr = util.get_std_fd(self.stderr_base_path, 'local')
|
||||||
close_stderr = True
|
close_stderr = True
|
||||||
|
|
||||||
if env is None:
|
if env is None:
|
||||||
|
@ -246,20 +245,19 @@ class Local(object):
|
||||||
|
|
||||||
self.log.trace("Local run: %s", command)
|
self.log.trace("Local run: %s", command)
|
||||||
try:
|
try:
|
||||||
if quiet:
|
|
||||||
stderr = subprocess.DEVNULL
|
|
||||||
if return_output:
|
if return_output:
|
||||||
output = subprocess.check_output(
|
output = subprocess.check_output(
|
||||||
command, env=env, stderr=stderr)
|
command, env=env, stderr=stderr).decode()
|
||||||
self._log_std_fd(stderr, 'stderr', quiet, save_output)
|
|
||||||
return output.decode()
|
|
||||||
else:
|
else:
|
||||||
if quiet:
|
|
||||||
stdout = subprocess.DEVNULL
|
|
||||||
subprocess.check_call(command, env=env, stderr=stderr,
|
subprocess.check_call(command, env=env, stderr=stderr,
|
||||||
stdout=stdout)
|
stdout=stdout)
|
||||||
self._log_std_fd(stderr, 'stderr', quiet, save_output)
|
output = None
|
||||||
self._log_std_fd(stdout, 'stdout', quiet, save_output)
|
|
||||||
|
if do_save_output:
|
||||||
|
util.log_std_fd(self.log, stderr, 'Local stderr')
|
||||||
|
util.log_std_fd(self.log, stdout, 'Local stdout')
|
||||||
|
|
||||||
|
return output
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
util.handle_called_process_error(e, command)
|
util.handle_called_process_error(e, command)
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
|
|
|
@ -295,12 +295,6 @@ class Remote(object):
|
||||||
return self._run_command(cmd, env=env, return_output=return_output,
|
return self._run_command(cmd, env=env, return_output=return_output,
|
||||||
stdout=stdout, stderr=stderr)
|
stdout=stdout, stderr=stderr)
|
||||||
|
|
||||||
def _log_std_fd(self, stdfd, which):
|
|
||||||
if stdfd is not None and stdfd != subprocess.DEVNULL:
|
|
||||||
stdfd.seek(0, 0)
|
|
||||||
self.log.trace("Remote {}: {}".format(
|
|
||||||
which, stdfd.read().decode()))
|
|
||||||
|
|
||||||
def _run_command(self, command, env=None, return_output=False, stdout=None,
|
def _run_command(self, command, env=None, return_output=False, stdout=None,
|
||||||
stderr=None):
|
stderr=None):
|
||||||
"""Run the given command with the given environment.
|
"""Run the given command with the given environment.
|
||||||
|
@ -316,10 +310,10 @@ class Remote(object):
|
||||||
close_stdout = False
|
close_stdout = False
|
||||||
close_stderr = False
|
close_stderr = False
|
||||||
if not return_output and stdout is None:
|
if not return_output and stdout is None:
|
||||||
stdout = util._get_std_fd(self, 'stdout')
|
stdout = util.get_std_fd(self.stdout_base_path, 'remote')
|
||||||
close_stdout = True
|
close_stdout = True
|
||||||
if stderr is None:
|
if stderr is None:
|
||||||
stderr = util._get_std_fd(self, 'stderr')
|
stderr = util.get_std_fd(self.stderr_base_path, 'remote')
|
||||||
close_stderr = True
|
close_stderr = True
|
||||||
|
|
||||||
# export target_host, target_hostname, target_fqdn
|
# export target_host, target_hostname, target_fqdn
|
||||||
|
@ -335,14 +329,16 @@ class Remote(object):
|
||||||
stderr = subprocess.DEVNULL
|
stderr = subprocess.DEVNULL
|
||||||
if return_output:
|
if return_output:
|
||||||
output = subprocess.check_output(command, env=os_environ,
|
output = subprocess.check_output(command, env=os_environ,
|
||||||
stderr=stderr)
|
stderr=stderr).decode()
|
||||||
self._log_std_fd(stderr, 'stderr')
|
|
||||||
return output.decode()
|
|
||||||
else:
|
else:
|
||||||
subprocess.check_call(command, env=os_environ, stdout=stdout,
|
subprocess.check_call(command, env=os_environ, stdout=stdout,
|
||||||
stderr=stderr)
|
stderr=stderr)
|
||||||
self._log_std_fd(stderr, 'stderr')
|
output = None
|
||||||
self._log_std_fd(stdout, 'stdout')
|
|
||||||
|
util.log_std_fd(self.log, stderr, 'Remote stderr')
|
||||||
|
util.log_std_fd(self.log, stdout, 'Remote stdout')
|
||||||
|
|
||||||
|
return output
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
util.handle_called_process_error(e, command)
|
util.handle_called_process_error(e, command)
|
||||||
except OSError as error:
|
except OSError as error:
|
||||||
|
|
|
@ -163,12 +163,14 @@ def _call_get_stdout(command, env=None, stderr=None):
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def _get_std_fd(obj, which):
|
def get_std_fd(base_path, name):
|
||||||
if which == 'stdout':
|
path = os.path.join(base_path, name)
|
||||||
base = obj.stdout_base_path
|
|
||||||
else:
|
|
||||||
base = obj.stderr_base_path
|
|
||||||
|
|
||||||
path = os.path.join(base, 'remote')
|
|
||||||
stdfd = open(path, 'ba+')
|
stdfd = open(path, 'ba+')
|
||||||
return stdfd
|
return stdfd
|
||||||
|
|
||||||
|
|
||||||
|
def log_std_fd(log, stdfd, prefix):
|
||||||
|
if stdfd is not None and stdfd != subprocess.DEVNULL:
|
||||||
|
stdfd.seek(0, 0)
|
||||||
|
log.trace("{}: {}".format(
|
||||||
|
prefix, stdfd.read().decode()))
|
||||||
|
|
Loading…
Reference in a new issue