Little refactoring.

This commit is contained in:
Darko Poljak 2017-09-11 11:36:15 +02:00 committed by Darko Poljak
parent 00cd5fa91e
commit be4668bf2d
3 changed files with 23 additions and 29 deletions

View file

@ -34,7 +34,7 @@ import datetime
import cdist import cdist
import cdist.message import cdist.message
from cdist import core from cdist import core
import cdist.exec.util as exec_util import cdist.exec.util as util
CONF_SUBDIRS_LINKED = ["explorer", "files", "manifest", "type", ] CONF_SUBDIRS_LINKED = ["explorer", "files", "manifest", "type", ]
@ -203,16 +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 _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): def _log_std_fd(self, stdfd, which, quiet, save_output):
if not quiet and save_output and stdfd is not None: if not quiet and save_output and stdfd is not None:
stdfd.seek(0, 0) stdfd.seek(0, 0)
@ -233,10 +223,10 @@ class Local(object):
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 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 close_stdout = True
if not quiet and save_output and stderr is None: 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 close_stderr = True
if env is None: if env is None:
@ -271,7 +261,7 @@ class Local(object):
self._log_std_fd(stderr, 'stderr', quiet, save_output) self._log_std_fd(stderr, 'stderr', quiet, save_output)
self._log_std_fd(stdout, 'stdout', quiet, save_output) self._log_std_fd(stdout, 'stdout', quiet, save_output)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
exec_util.handle_called_process_error(e, command) util.handle_called_process_error(e, command)
except OSError as error: except OSError as error:
raise cdist.Error(" ".join(command) + ": " + error.args[1]) raise cdist.Error(" ".join(command) + ": " + error.args[1])
finally: finally:

View file

@ -27,7 +27,7 @@ import logging
import multiprocessing import multiprocessing
import cdist import cdist
import cdist.exec.util as exec_util import cdist.exec.util as util
import cdist.util.ipaddr as ipaddr import cdist.util.ipaddr as ipaddr
from cdist.mputil import mp_pool_run 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, return self._run_command(cmd, env=env, return_output=return_output,
stdout=stdout, stderr=stderr) 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): def _log_std_fd(self, stdfd, which):
if stdfd is not None and stdfd != subprocess.DEVNULL: if stdfd is not None and stdfd != subprocess.DEVNULL:
stdfd.seek(0, 0) stdfd.seek(0, 0)
@ -326,10 +316,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 = self._get_std_fd('stdout') stdout = util._get_std_fd(self, 'stdout')
close_stdout = True close_stdout = True
if stderr is None: if stderr is None:
stderr = self._get_std_fd('stderr') stderr = util._get_std_fd(self, 'stderr')
close_stderr = True close_stderr = True
# export target_host, target_hostname, target_fqdn # export target_host, target_hostname, target_fqdn
@ -354,7 +344,7 @@ class Remote(object):
self._log_std_fd(stderr, 'stderr') self._log_std_fd(stderr, 'stderr')
self._log_std_fd(stdout, 'stdout') self._log_std_fd(stdout, 'stdout')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
exec_util.handle_called_process_error(e, command) util.handle_called_process_error(e, command)
except OSError as error: except OSError as error:
raise cdist.Error(" ".join(command) + ": " + error.args[1]) raise cdist.Error(" ".join(command) + ": " + error.args[1])
except UnicodeDecodeError: except UnicodeDecodeError:

View file

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- # -*- 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. # This file is part of cdist.
# #
@ -20,6 +20,7 @@
# #
import subprocess import subprocess
import os
from tempfile import TemporaryFile from tempfile import TemporaryFile
import cdist import cdist
@ -115,6 +116,7 @@ import cdist
# return (result.stdout, result.stderr) # return (result.stdout, result.stderr)
# Currently not used.
def call_get_output(command, env=None, stderr=None): def call_get_output(command, env=None, stderr=None):
"""Run the given command with the given environment. """Run the given command with the given environment.
Return the tuple of stdout and stderr output as a byte strings. 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)) " ".join(command), err.returncode, output))
# Currently not used.
def _call_get_stdout(command, env=None, stderr=None): def _call_get_stdout(command, env=None, stderr=None):
"""Run the given command with the given environment. """Run the given command with the given environment.
Return the stdout output as a byte string, stderr is ignored. 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() output = fout.read()
return output 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