Ejemplo n.º 1
0
def run_fork(args):
    patchfns.chdir_to_base_dir()
    top_patch = patchfns.find_top_patch()
    if not top_patch:
        return cmd_result.ERROR
    new_patch = args.arg_new_name if args.arg_new_name else patchfns.next_filename(top_patch)
    new_patch_file = patchfns.patch_file_name(new_patch)
    new_patch_dir = os.path.join(patchfns.QUILT_PC, new_patch)
    if patchfns.patch_in_series(new_patch) or os.path.isdir(new_patch_dir) or os.path.exists(new_patch_file):
        output.error('Patch %s exists already, please choose a new name\n' % patchfns.print_patch(new_patch))
        return cmd_result.ERROR | cmd_result.SUGGEST_RENAME
    is_ok = patchfns.rename_in_db(top_patch, new_patch)
    is_ok = is_ok if not is_ok else patchfns.rename_in_series(top_patch, new_patch)
    if is_ok:
        top_patch_dir = os.path.join(patchfns.QUILT_PC, top_patch)
        try:
            os.rename(top_patch_dir, new_patch_dir)
        except OSError:
            is_ok = False
    if is_ok:
        top_patch_file = patchfns.patch_file_name(top_patch)
        if os.path.exists(top_patch_file):
            try:
                shutil.copy2(top_patch_file, new_patch_file)
            except Exception:
                is_ok = False
    if not is_ok:
        output.write('Fork of patch %s to patch %s failed\n' % (patchfns.print_patch(top_patch), patchfns.print_patch(new_patch)))
    else:
        output.error('Fork of patch %s created as %s\n' % (patchfns.print_patch(top_patch), patchfns.print_patch(new_patch)))
    return cmd_result.OK if is_ok else cmd_result.ERROR
Ejemplo n.º 2
0
def run_top(args):
    patchfns.chdir_to_base_dir()
    top = patchfns.find_top_patch()
    if not top:
        return cmd_result.ERROR
    output.write('%s\n' % patchfns.print_patch(top))
    return cmd_result.OK
Ejemplo n.º 3
0
def run_delete(args):
    patchfns.chdir_to_base_dir()
    if args.patch:
        patch = patchfns.find_patch(args.patch)
        if not patch:
            return cmd_result.ERROR
    else:
        patch = patchfns.top_patch()
    if args.opt_next:
        patch =patchfns.patch_after(patch)
        if not patch:
            output.error('No next patch\n')
            return cmd_result.ERROR
    if not patch:
        patchfns.find_top_patch()
        return cmd_result.ERROR
    if patchfns.is_applied(patch):
        if patch != patchfns.top_patch():
            output.error('Patch %s is currently applied\n' % patchfns.print_patch(patch))
            return cmd_result.ERROR
        if patchfns.pyquilt_command('pop -qf') != cmd_result.OK:
            return cmd_result.ERROR
    if patchfns.remove_from_series(patch):
        output.write('Removed patch %s\n' % patchfns.print_patch(patch))
    else:
        output.error('Failed to remove patch %s\n' % patchfns.print_patch(patch))
        return cmd_result.ERROR
    patch_file = patchfns.patch_file_name(patch)
    if args.opt_remove and os.path.exists(patch_file):
        if args.opt_backup:
            try:
                os.rename(patch_file, patch_file + '~')
            except IOError:
                output.error('Failed to backup patch file %s\n' % patch_file)
                return cmd_result.ERROR
        else:
            try:
                os.remove(patch_file)
            except IOError:
                output.error('Failed to remove patch file %s\n' % patch_file)
                return cmd_result.ERROR
    return cmd_result.OK
Ejemplo n.º 4
0
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