diff --git a/pullv/timed_subprocess.py b/pullv/timed_subprocess.py deleted file mode 100644 index 3dadc594..00000000 --- a/pullv/timed_subprocess.py +++ /dev/null @@ -1,70 +0,0 @@ -# -*- coding: utf-8 -*- -"""For running command line executables with a timeout - -from salt.util.timed_process - -Apache 2 license. Changes: - -- move TimedProcTimeoutError to this file -""" - -import subprocess -import threading - - -class TimedProcTimeoutError(Exception): - pass - - -class TimedProc(object): - - ''' - Create a TimedProc object, calls subprocess.Popen with passed args and **kwargs - ''' - def __init__(self, args, **kwargs): - - self.command = args - self.stdin = kwargs.pop('stdin', None) - if self.stdin is not None: - # Translate a newline submitted as '\n' on the CLI to an actual - # newline character. - self.stdin = self.stdin.replace('\\n', '\n') - kwargs['stdin'] = subprocess.PIPE - self.with_communicate = kwargs.pop('with_communicate', True) - - self.process = subprocess.Popen(args, **kwargs) - - def wait(self, timeout=None): - ''' - wait for subprocess to terminate and return subprocess' return code. - If timeout is reached, throw TimedProcTimeoutError - ''' - def receive(): - if self.with_communicate: - (self.stdout, self.stderr) = self.process.communicate( - input=self.stdin) - else: - self.process.wait() - (self.stdout, self.stderr) = (None, None) - - if timeout: - if not isinstance(timeout, (int, float)): - raise TimedProcTimeoutError('Error: timeout must be a number') - rt = threading.Thread(target=receive) - rt.start() - rt.join(timeout) - if rt.isAlive(): - # Subprocess cleanup (best effort) - self.process.kill() - - def terminate(): - if rt.isAlive(): - self.process.terminate() - threading.Timer(10, terminate).start() - raise TimedProcTimeoutError('%s : Timed out after %s seconds' % ( - self.command, - str(timeout), - )) - else: - receive() - return self.process.returncode diff --git a/pullv/util.py b/pullv/util.py index b06d2e1b..8ee0fd40 100644 --- a/pullv/util.py +++ b/pullv/util.py @@ -12,11 +12,9 @@ import subprocess import os import logging +import fnmatch logger = logging.getLogger(__name__) -from . import timed_subprocess - - def expand_config(config): '''Expand configuration into full form. @@ -111,24 +109,17 @@ def _run(cmd, } try: - proc = timed_subprocess.TimedProc(cmd, **kwargs) + proc = subprocess.Popen(cmd, **kwargs) except (OSError, IOError) as exc: - raise Error('Unable to urn command: {0}'.format(exc)) + raise Error('Unable to run command: {0}'.format(exc)) - try: - proc.wait(timeout) - except timed_subprocess.TimedProcTimeoutError as exc: - ret['stdout'] = str(exc) - ret['stderr'] = '' - ret['pid'] = proc.process.pid - ret['retcode'] = 1 - return ret + proc.wait() - out, err = proc.stdout, proc.stderr + out, err = proc.stdout.read(), proc.stderr.read() ret['stdout'] = out ret['stderr'] = err - ret['pid'] = proc.process.pid - ret['retcode'] = proc.process.returncode + ret['pid'] = proc.pid + ret['retcode'] = proc.returncode return ret