def run_fold(args): patchfns.chdir_to_base_dir() if not args.opt_strip_level: args.opt_strip_level = '1' opt_patch_args = '' if not args.opt_quiet else ' -s' if not args.opt_force or args.opt_quiet: opt_patch_args += ' -f' if args.opt_reverse: opt_patch_args += ' -R' top = patchfns.find_top_patch() if not top: return cmd_result.ERROR failed = suggest_force = False try: workdir = patchfns.gen_tempfile(template=os.getcwd(), asdir=True) if patchfns.SUBDIR: subdir = patchfns.SUBDIR prefix = os.path.join(workdir, patchfns.SUBDIR) + os.sep else: subdir = '.' prefix = workdir + os.sep patch_args = '-p%s --backup --prefix="%s" -E %s' % (args.opt_strip_level, prefix, opt_patch_args) patch_text = sys.stdin.read() result = putils.apply_patch_text(patch_text, indir=subdir, patch_args=patch_args) output.write(result.stdout) output.error(result.stderr) if result.eflags != 0 and not args.opt_force: suggest_force = True failed = True if not failed: for filename in fsutils.files_in_dir(workdir): backup_file = patchfns.backup_file_name(top, filename) if not os.path.exists(backup_file): try: backup_file_dir = os.path.dirname(backup_file) if backup_file_dir and not os.path.exists(backup_file_dir): os.makedirs(backup_file_dir) os.link(os.path.join(workdir, filename), backup_file) except OSError as edata: failed = True break except KeyboardInterrupt: failed = True if failed: for filename in fsutils.files_in_dir(workdir): try: shutil.move(os.path.join(workdir, filename), filename) except OSError: output.error('File %s may be corrupted\n' % filename) if os.path.exists(workdir): shutil.rmtree(workdir) return cmd_result.OK if not failed else cmd_result.ERROR_SUGGEST_FORCE if suggest_force else cmd_result.ERROR
def files_in_patch(patch): path = os.path.join(QUILT_PC, patch) if os.path.isdir(path): return sorted(fsutils.files_in_dir(path, exclude_timestamp=True)) return []