def apply_patch(patchfile,cwd=None,posix=False,level=<NUM_LIT:0>):
if not os.path.exists(patchfile):<EOL><INDENT>raise RuntimeError('<STR_LIT>'%patchfile)<EOL><DEDENT>fd = open(patchfile,mode='<STR_LIT:r>')<EOL>level_str = '<STR_LIT>'%level<EOL>args = ['<STR_LIT>',level_str]<EOL>if posix:<EOL><INDENT>args.append('<STR_LIT>')<EOL><DEDENT>log.info('<STR_LIT>', '<STR_LIT:U+0020>'.join(args), patchfile)<EOL>log.info('<STR_LIT>', cwd)<EOL><INDENT>print >> sys.stderr, '<STR_LIT>','<STR_LIT:U+0020>'.join(args),'<STR_LIT:<>',patchfile<EOL>print >> sys.stderr, '<STR_LIT>',cwd<EOL><DEDENT>res = subprocess.Popen(<EOL>args, cwd=cwd,<EOL>stdin=fd,<EOL>stdout=subprocess.PIPE,<EOL>stderr=subprocess.PIPE,<EOL>universal_newlines=True<EOL>)<EOL>returncode=None<EOL>while returncode is None:<EOL><INDENT>returncode = res.poll()<EOL>ready = select.select( [res.stdout,res.stderr],[],[],<NUM_LIT:0.1>)<EOL>if res.stdout in ready[<NUM_LIT:0>]:<EOL><INDENT>sys.stdout.write(res.stdout.read(<NUM_LIT:1>))<EOL>sys.stdout.flush()<EOL><DEDENT>if res.stderr in ready[<NUM_LIT:0>]:<EOL><INDENT>sys.stderr.write(res.stderr.read(<NUM_LIT:1>))<EOL>sys.stderr.flush()<EOL><DEDENT><DEDENT>sys.stdout.write(res.stdout.read())<EOL>sys.stdout.flush()<EOL>sys.stderr.write(res.stderr.read())<EOL>sys.stderr.flush()<EOL>if returncode:<EOL><INDENT>log.error('<STR_LIT>', '<STR_LIT:U+0020>'.join(args))<EOL>log.error('<STR_LIT>', cwd)<EOL><INDENT>print >> sys.stderr, '<STR_LIT>'%('<STR_LIT:U+0020>'.join(args),)<EOL>print >> sys.stderr, '<STR_LIT>',cwd<EOL><DEDENT>raise RuntimeError('<STR_LIT>'%returncode)<EOL><DEDENT>
call 'patch -p[level] [--posix] < arg1'

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