(patchfile, cwd=None, posix=False, level=0)
call 'patch -p[level] [--posix] < arg1'

    posix mode is sometimes necessary. It keeps empty files so that
    dpkg-source removes their contents.
call 'patch -p[level] [--posix] < arg1'
call patch - p [ level ] [ -- posix ] < arg1
def apply_patch(patchfile, cwd=None, posix=False, level=0):
    """call 'patch -p[level] [--posix] < arg1'

    posix mode is sometimes necessary. It keeps empty files so that
    dpkg-source removes their contents.

    """
    if not os.path.exists(patchfile):
        raise RuntimeError('patchfile "%s" does not exist' % patchfile)
    fd = open(patchfile, mode='r')

    level_str = '-p%d' % level
    args = ['/usr/bin/patch', level_str]
    if posix:
        args.append('--posix')

    log.info('PATCH COMMAND: %s < %s', ' '.join(args), patchfile)
    log.info('  PATCHING in dir: %s', cwd)
#    print >> sys.stderr, 'PATCH COMMAND:',' '.join(args),'<',patchfile
#    print >> sys.stderr, '  PATCHING in dir:',cwd
    res = subprocess.Popen(
        args, cwd=cwd,
        stdin=fd,
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE,
        universal_newlines=True
        )
    returncode = None
    while returncode is None:
        returncode = res.poll()
        ready = select.select([res.stdout, res.stderr], [], [], 0.1)
        # XXX figure out how to do this without reading byte-by-byte
        if res.stdout in ready[0]:
            sys.stdout.write(res.stdout.read(1))
            sys.stdout.flush()
        if res.stderr in ready[0]:
            sys.stderr.write(res.stderr.read(1))
            sys.stderr.flush()
    # finish outputting file
    sys.stdout.write(res.stdout.read())
    sys.stdout.flush()
    sys.stderr.write(res.stderr.read())
    sys.stderr.flush()

    if returncode:
        log.error('ERROR running: %s', ' '.join(args))
        log.error('ERROR in %s', cwd)
#        print >> sys.stderr, 'ERROR running: %s'%(' '.join(args),)
#        print >> sys.stderr, 'ERROR in',cwd
        raise RuntimeError('returncode %d' % returncode)
def apply_patch ( patchfile , cwd = None , posix = False , level = 0 ) : if not os . path . exists ( patchfile ) : raise RuntimeError ( 'patchfile "%s" does not exist' % patchfile ) fd = open ( patchfile , mode = 'r' ) level_str = '-p%d' % level args = [ '/usr/bin/patch' , level_str ] if posix : args . append ( '--posix' ) log . info ( 'PATCH COMMAND: %s < %s' , ' ' . join ( args ) , patchfile ) log . info ( '  PATCHING in dir: %s' , cwd ) #    print >> sys.stderr, 'PATCH COMMAND:',' '.join(args),'<',patchfile #    print >> sys.stderr, '  PATCHING in dir:',cwd res = subprocess . Popen ( args , cwd = cwd , stdin = fd , stdout = subprocess . PIPE , stderr = subprocess . PIPE , universal_newlines = True ) returncode = None while returncode is None : returncode = res . poll ( ) ready = select . select ( [ res . stdout , res . stderr ] , [ ] , [ ] , 0.1 ) # XXX figure out how to do this without reading byte-by-byte if res . stdout in ready [ 0 ] : sys . stdout . write ( res . stdout . read ( 1 ) ) sys . stdout . flush ( ) if res . stderr in ready [ 0 ] : sys . stderr . write ( res . stderr . read ( 1 ) ) sys . stderr . flush ( ) # finish outputting file sys . stdout . write ( res . stdout . read ( ) ) sys . stdout . flush ( ) sys . stderr . write ( res . stderr . read ( ) ) sys . stderr . flush ( ) if returncode : log . error ( 'ERROR running: %s' , ' ' . join ( args ) ) log . error ( 'ERROR in %s' , cwd ) #        print >> sys.stderr, 'ERROR running: %s'%(' '.join(args),) #        print >> sys.stderr, 'ERROR in',cwd raise RuntimeError ( 'returncode %d' % returncode )