Ejemplo n.º 1
0
def run_import(args):
    patchfns.chdir_to_base_dir()
    if args.opt_patch and len(args.patchfiles) > 1:
        output.error('Option `-P\' can only be used when importing a single patch\n')
        return cmd_result.ERROR
    patch_args = ('-p%s' % args.opt_strip) if args.opt_strip else ''
    if args.opt_reverse:
        patch_args = '-R' if not patch_args else patch_args + ' -R'
    before = patchfns.patch_after(patchfns.top_patch())
    for patch_file in args.patchfiles:
        patch = args.opt_patch if args.opt_patch else os.path.basename(patch_file)
        patch_file_name = patchfns.find_patch_file(patch_file)
        if not patch_file_name:
            return cmd_result.ERROR
        merged = False
        if patchfns.is_applied(patch):
            output.error('Patch %s is applied\n' % patchfns.print_patch(patch))
            return cmd_result.ERROR
        dest = patchfns.patch_file_name(patch)
        if patchfns.patch_in_series(patch):
            if patch_file_name == dest:
                output.error('Patch %s already exists in series.\n' % patchfns.print_patch(patch))
                return cmd_result.ERROR
            if not args.opt_force:
                output.error('Patch %s exists. Replace with -f.\n' % patchfns.print_patch(patch))
                return cmd_result.ERROR_SUGGEST_FORCE
            if args.opt_desc != 'n':
                merged_patch = merge_patches(dest, patch_file_name, args.opt_desc)
                if merged_patch is False:
                    return cmd_result.ERROR
                merged = True
            output.error('Replacing patch %s with new version\n' % patchfns.print_patch(patch))
        elif os.path.exists(dest):
            output.write('Importing patch %s\n' % patchfns.print_patch(patch))
        else:
            output.write('Importing patch %s (stored as %s)\n' % (patch_file, patchfns.print_patch(patch)))
            dest_dir = os.path.dirname(dest)
            if not os.path.exists(dest_dir):
                os.makedirs(dest_dir)
        try:
            if merged:
                fsutils.set_file_contents(dest, merged_patch)
            else:
                # just in case dest == patch_file do it this way
                text = fsutils.get_file_contents(patch_file_name)
                fsutils.set_file_contents(dest, text)
        except IOError as edata:
            output.error('Failed to import patch %s\n' % patchfns.print_patch(patch))
            return cmd_result.ERROR
        if not patchfns.patch_in_series(patch) and not patchfns.insert_in_series(patch, patch_args, before):
            output.error('Failed to insert patch %s into file series\n' % patchfns.print_patch(patch))
    return cmd_result.OK
Ejemplo n.º 2
0
def run_unapplied(args):
    patchfns.chdir_to_base_dir()
    if args.arg_patch:
        start = patchfns.find_patch_in_series(args.arg_patch)
        if not start:
            return cmd_result.ERROR
        patch = patchfns.patch_after(start)
    else:
        patch = patchfns.find_unapplied_patch()
    if not patch:
        return cmd_result.OK
    output.start_pager()
    patches = [patch] + patchfns.patches_after(patch)
    for patch in patches:
        output.write('%s\n' % patchfns.print_patch(patch))
    output.wait_for_pager()
    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