.. _fbf.utils.popen: popen ~~~~~ .. automodule:: fbf.utils.popen :show-inheritance: :members: :undoc-members: CODE ---- :: # fbf/utils/popen.py # # """ popen helper functions. """ .. _fbf.utils.popen_defines_: defines ---------- :: go = False .. _fbf.utils.popen_basic_imports: basic imports ---------------- :: try: from subprocess import Popen, PIPE from .locking import lockdec import _thread, io, logging, types go = True except: go = False if go: .. _fbf.utils.popen_locks_: ## locks -------- :: popenlock = _thread.allocate_lock() popenlocked = lockdec(popenlock) .. _fbf.utils.popen_exceptions_: ## exceptions ------------- :: class PopenWhitelistError(Exception): def __init__(self, item): Exception.__init__(self) self.item = item def __str__(self): return self.item class PopenListError(Exception): def __init__(self, item): Exception.__init__(self) self.item = item def __str__(self): return str(self.item) .. _fbf.utils.popen_GozerStringIO_class: ## GozerStringIO class ---------------------- :: class GozerStringIO(io.StringIO): """ provide readlines support on a StringIO object. """ def readlines(self): """ read multiple lines. """ return self.read().split('\n') .. _fbf.utils.popen_GozerPopen4_class: ## GozerPopen4 class -------------------- :: class GozerPopen4(Popen): """ extend the builtin Popen class with a close method. """ def __init__(self, args): Popen.__init__(self, args, shell=False, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) self.fromchild = self.stdout self.tochild = self.stdin self.errors = self.stderr def close(self): """ shutdown. """ self.wait() try: self.stdin.close() except: pass try: self.stdout.close() except: pass try: self.errors.close() except: pass return self.returncode .. _fbf.utils.popen_gozerpopen_function: ## gozerpopen function ---------------------- :: def gozerpopen(args, userargs=[]): """ do the actual popen .. make sure the arguments are passed on as list. """ if type(args) != list: raise PopenListError(args) if type(userargs) != list: raise PopenListError(args) for i in userargs: if i.startswith('-'): raise PopenWhitelistError(i) proces = GozerPopen4(args + userargs) return proces