diff options
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/Client/Tools/__init__.py | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/src/lib/Client/Tools/__init__.py b/src/lib/Client/Tools/__init__.py index f0ae01a0a..dd950a7ef 100644 --- a/src/lib/Client/Tools/__init__.py +++ b/src/lib/Client/Tools/__init__.py @@ -8,17 +8,30 @@ __all__ = ["Action", "APT", "Blast", "Chkconfig", "DebInit", "Encap", "IPS", drivers = [item for item in __all__ if item not in ['rpmtools']] default = [item for item in drivers if item not in ['RPM', 'Yum']] -import os -import stat -import sys -import time -from subprocess import Popen, PIPE -import Bcfg2.Client.XML +import os, popen2, stat, sys, Bcfg2.Client.XML, time class toolInstantiationError(Exception): '''This error is called if the toolset cannot be instantiated''' pass +class readonlypipe(popen2.Popen4): + '''This pipe sets up stdin --> /dev/null''' + def __init__(self, cmd, bufsize=-1): + popen2._cleanup() + c2pread, c2pwrite = os.pipe() + null = open('/dev/null', 'w+') + self.pid = os.fork() + if self.pid == 0: + # Child + os.dup2(null.fileno(), sys.__stdin__.fileno()) + #os.dup2(p2cread, 0) + os.dup2(c2pwrite, 1) + os.dup2(c2pwrite, 2) + self._run_child(cmd) + os.close(c2pwrite) + self.fromchild = os.fdopen(c2pread, 'r', bufsize) + popen2._active.append(self) + class executor: '''this class runs stuff for us''' def __init__(self, logger): @@ -28,12 +41,25 @@ class executor: '''Run a command in a pipe dealing with stdout buffer overloads''' self.logger.debug('> %s' % command) - p = Popen(command, shell=True, bufsize=16384, - stdin=PIPE, stdout=PIPE, close_fds=True) - output = p.communicate()[0] - for line in output.splitlines(): - self.logger.debug('< %s' % line) - return (p.returncode, output.splitlines()) + runpipe = readonlypipe(command, bufsize=16384) + output = [] + try:#macosx doesn't like this + runpipe.fromchild.flush() + except IOError: + pass + line = runpipe.fromchild.readline() + cmdstat = -1 + while cmdstat == -1: + while line: + if len(line) > 0: + self.logger.debug('< %s' % line[:-1]) + output.append(line[:-1]) + line = runpipe.fromchild.readline() + time.sleep(0.1) + cmdstat = runpipe.poll() + output += [line[:-1] for line in runpipe.fromchild.readlines() \ + if line] + return (cmdstat, output) class Tool: ''' |