Fancy magic version of os.system

def systemCall(cmd, sh=True, log=None):
    '''Fancy magic version of os.system'''
    if log is None:
        log = logging

    log.debug('System call [sh:%s]: %s' \
              % (sh, cmd))

    out = []
    proc = None
    poller = None
    outBuf = ['']
    errBuf = ['']

    def pollOutput():
        '''
        Read, log and store output (if any) from processes pipes.
        '''
        removeChars = '\r\n'

         # collect fds with new output
        fds = [entry[0] for entry in poller.poll()]

        if proc.stdout.fileno() in fds:
            while True:
                try:
                    tmp = proc.stdout.read(100)
                except IOError:
                    break
                outBuf[0] += tmp

                while '\n' in outBuf[0]:
                    line, _, outBuf[0] = outBuf[0].partition('\n')
                    log.debug(line)
                    out.append(line + '\n')

                if not tmp:
                    break
        if proc.stderr.fileno() in fds:
            while True:
                try:
                    tmp = proc.stderr.read(100)
                except IOError:
                    break
                errBuf[0] += tmp

                while '\n' in errBuf[0]:
                    line, _, errBuf[0] = errBuf[0].partition('\n')
                    log.warning(line)

                if not tmp:
                    break

    while True:
        if proc is None:
            # create and start process
            proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=sh)

            # create poll select
            poller = select.poll()

            flags = fcntl.fcntl(proc.stdout, fcntl.F_GETFL)
            fcntl.fcntl(proc.stdout, fcntl.F_SETFL, flags| os.O_NONBLOCK)

            flags = fcntl.fcntl(proc.stderr, fcntl.F_GETFL)
            fcntl.fcntl(proc.stderr, fcntl.F_SETFL, flags| os.O_NONBLOCK)

            # register pipes to polling
            poller.register(proc.stdout, select.POLLIN)
            poller.register(proc.stderr, select.POLLIN)

        pollOutput()

        if proc.poll() is not None: # proc finished
            break

    # poll once after the process ended to collect all the missing output
    pollOutput()

    # check return code
    if proc.returncode != 0:
        raise RuntimeError(
            CalledProcessError(proc.returncode, cmd, ''.join(out))
            )

    return ''.join(out)