From be4668bf2dcf11a1d32bf251635ae44217e78623 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Mon, 11 Sep 2017 11:36:15 +0200 Subject: [PATCH] Little refactoring. --- cdist/exec/local.py | 18 ++++-------------- cdist/exec/remote.py | 18 ++++-------------- cdist/exec/util.py | 16 +++++++++++++++- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/cdist/exec/local.py b/cdist/exec/local.py index 4531bd7a..b46e8a52 100644 --- a/cdist/exec/local.py +++ b/cdist/exec/local.py @@ -34,7 +34,7 @@ import datetime import cdist import cdist.message from cdist import core -import cdist.exec.util as exec_util +import cdist.exec.util as util CONF_SUBDIRS_LINKED = ["explorer", "files", "manifest", "type", ] @@ -203,16 +203,6 @@ class Local(object): self.log.trace("Local mkdir: %s", path) os.makedirs(path, exist_ok=True) - def _get_std_fd(self, which): - if which == 'stdout': - base = self.stdout_base_path - else: - base = self.stderr_base_path - - path = os.path.join(base, 'remote') - stdfd = open(path, 'ba+') - return stdfd - 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) @@ -233,10 +223,10 @@ class Local(object): close_stdout = False close_stderr = False if not quiet and save_output and not return_output and stdout is None: - stdout = self._get_std_fd('stdout') + stdout = util._get_std_fd(self, 'stdout') close_stdout = True if not quiet and save_output and stderr is None: - stderr = self._get_std_fd('stderr') + stderr = util._get_std_fd(self, 'stderr') close_stderr = True if env is None: @@ -271,7 +261,7 @@ class Local(object): self._log_std_fd(stderr, 'stderr', quiet, save_output) self._log_std_fd(stdout, 'stdout', quiet, save_output) except subprocess.CalledProcessError as e: - exec_util.handle_called_process_error(e, command) + util.handle_called_process_error(e, command) except OSError as error: raise cdist.Error(" ".join(command) + ": " + error.args[1]) finally: diff --git a/cdist/exec/remote.py b/cdist/exec/remote.py index 475f4294..e6047af8 100644 --- a/cdist/exec/remote.py +++ b/cdist/exec/remote.py @@ -27,7 +27,7 @@ import logging import multiprocessing import cdist -import cdist.exec.util as exec_util +import cdist.exec.util as util import cdist.util.ipaddr as ipaddr from cdist.mputil import mp_pool_run @@ -295,16 +295,6 @@ class Remote(object): return self._run_command(cmd, env=env, return_output=return_output, stdout=stdout, stderr=stderr) - def _get_std_fd(self, which): - if which == 'stdout': - base = self.stdout_base_path - else: - base = self.stderr_base_path - - path = os.path.join(base, 'remote') - stdfd = open(path, 'ba+') - return stdfd - def _log_std_fd(self, stdfd, which): if stdfd is not None and stdfd != subprocess.DEVNULL: stdfd.seek(0, 0) @@ -326,10 +316,10 @@ class Remote(object): close_stdout = False close_stderr = False if not return_output and stdout is None: - stdout = self._get_std_fd('stdout') + stdout = util._get_std_fd(self, 'stdout') close_stdout = True if stderr is None: - stderr = self._get_std_fd('stderr') + stderr = util._get_std_fd(self, 'stderr') close_stderr = True # export target_host, target_hostname, target_fqdn @@ -354,7 +344,7 @@ class Remote(object): self._log_std_fd(stderr, 'stderr') self._log_std_fd(stdout, 'stdout') except subprocess.CalledProcessError as e: - exec_util.handle_called_process_error(e, command) + util.handle_called_process_error(e, command) except OSError as error: raise cdist.Error(" ".join(command) + ": " + error.args[1]) except UnicodeDecodeError: diff --git a/cdist/exec/util.py b/cdist/exec/util.py index e3c090cc..561b13aa 100644 --- a/cdist/exec/util.py +++ b/cdist/exec/util.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- # -# 2016 Darko Poljak (darko.poljak at gmail.com) +# 2016-2017 Darko Poljak (darko.poljak at gmail.com) # # This file is part of cdist. # @@ -20,6 +20,7 @@ # import subprocess +import os from tempfile import TemporaryFile import cdist @@ -115,6 +116,7 @@ import cdist # return (result.stdout, result.stderr) +# Currently not used. def call_get_output(command, env=None, stderr=None): """Run the given command with the given environment. Return the tuple of stdout and stderr output as a byte strings. @@ -145,6 +147,7 @@ def handle_called_process_error(err, command): " ".join(command), err.returncode, output)) +# Currently not used. def _call_get_stdout(command, env=None, stderr=None): """Run the given command with the given environment. Return the stdout output as a byte string, stderr is ignored. @@ -158,3 +161,14 @@ def _call_get_stdout(command, env=None, stderr=None): output = fout.read() return output + + +def _get_std_fd(obj, which): + if which == 'stdout': + base = obj.stdout_base_path + else: + base = obj.stderr_base_path + + path = os.path.join(base, 'remote') + stdfd = open(path, 'ba+') + return stdfd