def run_add(args): patchfns.chdir_to_base_dir() patch = patchfns.find_applied_patch(args.opt_patch) if not patch: return 1 patch_dir = os.path.join(patchfns.QUILT_PC, patch) status = 0 for filename in args.filelist: filename = patchfns.filename_rel_base(filename) if not patchfns.in_valid_dir(filename): status = 1 continue if patchfns.file_in_patch(patch, filename): output.error('File %s is already in patch %s\n' % (filename, patchfns.print_patch(patch))) status = 2 if status != 1 else 1 next_patch = patchfns.next_patch_for_file(patch, filename) if next_patch is not None: output.error('File %s modified by patch %s\n' % (filename, patchfns.print_patch(next_patch))) status = 1 continue if os.path.islink(filename): output.error('Cannot add symbolic link %s\n' % filename) status = 1 continue if not backup.backup(patch_dir, [filename]): output.error('Failed to back up file %s\n' % filename) status = 1 continue if os.path.exists(filename): # The original tree may be read-only. os.chmod(filename, os.stat(filename).st_mode|stat.S_IWUSR) output.write('File %s added to patch %s\n' % (filename, patchfns.print_patch(patch))) return status
def run_remove(args): patchfns.chdir_to_base_dir() patch = patchfns.find_applied_patch(args.opt_patch) if not patch: return cmd_result.ERROR prpatch = patchfns.print_patch(patch) patchfn = patchfns.patch_file_name(patch) patchrefrfile = os.path.join(patchfns.QUILT_PC, patch + '~refresh') patchrefrdir = os.path.dirname(patchrefrfile) budir = patchfns.backup_dir_name(patch) is_ok = True for filename in args.file_list: if patchfns.SUBDIR: filename = os.path.join(patchfns.SUBDIR, filename) if not patchfns.file_in_patch(filename, patch): output.error('File %s is not in patch %s\n' % (filename, prpatch)) is_ok = False continue next_patch = patchfns.next_patch_for_file(patch, filename) if next_patch: output.error('File %s modified by patch %s\n' % (filename, patchfns.print_patch(next_patch))) is_ok = False continue # Restore file from backup if not backup.restore(budir, filelist=[filename], touch=True): output.error('Failed to remove file %s from patch %s\n' % (filename, prpatch)) is_ok = False continue if os.path.exists(patchrefrdir) and os.path.exists(patchfn): fsutils.touch(patchrefrfile) output.write('File %s removed from patch %s\n' % (filename, prpatch)) return cmd_result.OK if is_ok else cmd_result.ERROR
def run_revert(args): patchfns.chdir_to_base_dir() patch = patchfns.find_applied_patch(args.opt_patch) if not patch: return cmd_result.ERROR prpatch = patchfns.print_patch(patch) if patchfns.SUBDIR: args.files = [os.path.join(patchfns.SUBDIR, item) for item in args.files] is_ok = True for filename in args.files: if not patchfns.file_in_patch(filename, patch): is_ok = False output.error('File %s is not in patch %s\n' % (filename, prpatch)) continue next_patch = patchfns.next_patch_for_file(patch, filename) if next_patch: is_ok = False output.error('' % (filename, patchfns.print_patch(next_patch))) continue if not is_ok: return cmd_result.ERROR workdir = patchfns.gen_tempfile(os.getcwd(), asdir=True) atexit.register(clean_up, workdir) if not patchfns.apply_patch_temporarily(workdir, patch, args.files): return cmd_result.ERROR for filename in args.files: revert_ok = True wdfilename = os.path.join(workdir, filename) if os.path.exists(wdfilename) and os.path.getsize(wdfilename) > 0: if os.path.exists(filename) and diff.same_contents(filename, wdfilename): output.write('File %s is unchanged\n' % filename) continue try: fdir = os.path.dirname(filename) if fdir and not os.path.exists(fdir): os.makedirs(fdir) shutil.copy2(wdfilename, filename) except OSError as edata: revert_ok = False else: if not os.path.exists(filename): output.write('File %s is unchanged\n' % filename) continue try: os.remove(filename) fdir = os.path.dirname(filename) if os.path.exists(fdir) and len(os.listdir(fdir)) == 0: os.removedirs(fdir) except OSError as edata: revert_ok = False if revert_ok: output.write('Changes to %s in patch %s reverted\n' % (filename, prpatch)) else: output.error('Failed to revert changes to %s in patch %s\n' % (filename, prpatch)) is_ok = False return cmd_result.OK if is_ok else cmd_result.ERROR