Example #1
0
def run_new(args):
    patchfns.chdir_to_base_dir()
    patch = patchfns.patch_name_base(args.patchname)
    if patchfns.patch_in_series(patch):
        output.error('Patch "%s" exists already\n' % patchfns.print_patch(patch))
        return cmd_result.ERROR | cmd_result.SUGGEST_RENAME
    patchfns.create_db()
    if not patchfns.insert_in_series(patch) or not patchfns.add_to_db(patch):
        output.error('Failed to create patch %s\n' % patchfns.print_patch(patch))
        return cmd_result.ERROR
    else:
        output.write('Patch %s is now on top\n' % patchfns.print_patch(patch))
        return cmd_result.OK
Example #2
0
def run_push(args):
    def add_patch(patch):
        def apply_patch(patch_file, patch_args):
            if not os.path.exists(patch_file) or os.path.getsize(patch_file) == 0:
                return cmd_result.Result(0, '', '')
            return putils.apply_patch(patch_file, patch_args=patch_args)
        tmp = None
        patch_file = patchfns.patch_file_name(patch)
        output.write('Applying patch %s\n' % patchfns.print_patch(patch))
        try:
            pp_args = push_patch_args(patch, reverse=False)
            prefix = os.path.join(patchfns.QUILT_PC, patch)
            if not args.opt_leave_rejects:
                tmp = patchfns.gen_tempfile()
                trf = '-r %s' % tmp
            else:
                trf = ''
            patch_args = '%s --backup --prefix="%s/" %s -E %s' % (pp_args, prefix, trf, more_patch_args)
            result = apply_patch(patch_file, patch_args=patch_args)
            if not args.opt_quiet or result.eflags != 0:
                if do_colorize:
                    output.error(colorize(cleanup_patch_output(result.stderr, args)))
                    output.write(colorize(cleanup_patch_output(result.stdout, args)))
                else:
                    output.error(cleanup_patch_output(result.stderr, args))
                    output.write(cleanup_patch_output(result.stdout, args))
        except KeyboardInterrupt:
            rollback_patch(patch)
            output.error('Interrupted by user; patch %s was not applied.\n' % patchfns.print_patch(patch))
            return False
        finally:
            if tmp:
                os.remove(tmp)
        if result.eflags == 0 or (result.eflags == 1 and args.opt_force):
            patchfns.add_to_db(patch)
            refresh_file = os.path.join(patchfns.QUILT_PC, patch + '~refresh')
            if result.eflags == 0:
                if os.path.exists(refresh_file):
                    os.remove(refresh_file)
            else:
                fsutils.touch(refresh_file)
            patch_dir = os.path.join(patchfns.QUILT_PC, patch)
            if os.path.exists(patch_dir):
                fsutils.touch(os.path.join(patch_dir, '.timestamp'))
            else:
                os.mkdir(patch_dir)
            if not os.path.exists(patch_file):
                output.write('Patch %s does not exist; applied empty patch\n' % patchfns.print_patch(patch))
            elif not putils.get_patch_diff(patch_file):
                output.write('Patch %s appears to be empty; applied\n' % patchfns.print_patch(patch))
            elif result.eflags != 0:
                output.write('Applied patch %s (forced; needs refresh)\n' % patchfns.print_patch(patch))
                return False
        else:
            rollback_patch(patch)
            tmp = patchfns.gen_tempfile()
            trf = '-r %s' % tmp
            pp_args = push_patch_args(patch, reverse=True)
            patch_args = '%s --backup --prefix="%s/" %s -E %s' % (pp_args, prefix, trf, more_patch_args)
            result = apply_patch(patch_file, patch_args=patch_args)
            if result.eflags == 0:
                output.write('Patch %s can be reverse-applied\n' % patchfns.print_patch(patch))
            else:
                output.write('Patch %s does not apply (enforce with -f)\n' % patchfns.print_patch(patch))
            rollback_patch(patch)
            os.remove(tmp)
            return False
        return True
    number = stop_at_patch = None
    patchfns.chdir_to_base_dir()
    if args.patchnamornum:
        if args.patchnamornum.isdigit():
            number = int(args.patchnamornum)
        else:
            stop_at_patch = args.patchnamornum
    elif not args.opt_all:
        number = 1
    stop_at_patch = patchfns.find_unapplied_patch(stop_at_patch)
    if not stop_at_patch:
        return cmd_result.ERROR
    silent_unless_verbose = '-s' if not args.opt_verbose else None
    if args.opt_force:
        args.opt_leave_rejects = True
    more_patch_args = ' -s' if args.opt_quiet else ''
    more_patch_args += ' -f' if not args.opt_force or args.opt_quiet else ''
    if args.opt_merge is 'default':
        more_patch_args += ' --merge'
    elif args.opt_merge:
        more_patch_args += ' --merge=%s' % args.opt_merge
    more_patch_args += ' -F%d' % args.opt_fuzz if args.opt_fuzz else ''
    if patchfns.top_patch_needs_refresh():
        output.write('The topmost patch %s needs to be refreshed first.\n' % patchfns.print_top_patch())
        return cmd_result.ERROR | cmd_result.SUGGEST_REFRESH
    patches = list_patches(number=number, stop_at_patch=stop_at_patch, push_all=args.opt_all)
    patchfns.create_db()
    do_colorize = args.opt_color == 'always' or (args.opt_color == 'auto' and sys.stderr.isatty())
    if do_colorize:
        colour.set_up()
    is_ok = True
    for patch in patches:
        is_ok = add_patch(patch)
        if not is_ok:
            break
        if not args.opt_quiet:
            output.write('\n')
    if is_ok:
        output.write('Now at patch %s\n' % patchfns.print_top_patch())
    return cmd_result.OK if is_ok else cmd_result.ERROR
Example #3
0
def run_setup(args):
    spec_file = series_file = None
    patchfns.chdir_to_base_dir()
    tmpfile = patchfns.gen_tempfile()
    def add_exit_handler():
        try:
            os.remove(tmpfile)
        except OSError:
            pass
    script = []
    if args.series_file.endswith('.spec'):
        spec_file = args.series_file
        print 'not yet implemented'
    else:
        series_file = args.series_file
        tar_dir = patch_dir = '.'
        for line in open(series_file).readlines():
            if line.startswith('# Sourcedir: '):
                tar_dir = line[len('# Sourcedir: '):].rstrip()
            elif line.startswith('# Source: '):
                script.append(('tar', tar_dir, line[len('# Source: '):].rstrip()))
            elif line.startswith('# Patchdir: '):
                patch_dir = line[len('# Patchdir: '):].rstrip()
            elif line.startswith('#') or len(line.strip()) == 0:
                pass
            else:
                script.append(('patch', patch_dir, line.rstrip()))
    if check_for_existing_directories(args, script):
        return cmd_result.ERROR
    for action in script:
        if action[0] == 'tar':
            tarball = os.path.join(args.sourcedir, action[2]) if args.sourcedir else action[2]
            if not os.path.exists(tarball):
                output.error('File %s not found\n' % tarball)
                return cmd_result.ERROR
            output.write('Unpacking archive %s\n' % tarball)
            target_dir = os.path.join(args.prefix, action[1]) if args.prefix else action[1]
            try:
                os.makedirs(target_dir)
            except OSError as edata:
                if edata.errno != errno.EEXIST:
                    output.error('%s: %s\n' % (target_dir, edata.strerror))
                    return cmd_result.ERROR
            if tarfile.is_tarfile(tarball):
                tarobj = tarfile.open(tarball, 'r')
                tarobj.extractall(target_dir)
                tarobj.close()
            else:
                output.error('%s: is not a supported tar format\n' % tarball)
                return cmd_result.ERROR
    if check_for_existing_files(args, script):
        output.error("Trying alternative patches and series names...\n")
        patchfns.QUILT_PATCHES = "quilt_patches"
        patchfns.QUILT_SERIES = "quilt_series"
        if check_for_existing_files(args, script):
            return cmd_result.ERROR
    tar_dir = tar_file = None
    for action in script:
        if action[0] == 'tar':
            tar_dir = None if action[1] == '.' else action[1]
            tar_file = action[2]
        elif action[0] == 'patch':
            patches_dir = os.path.join(action[1], patchfns.QUILT_PATCHES)
            if args.prefix:
                patches_dir = os.path.join(args.prefix, patches_dir)
            if not os.path.exists(patches_dir):
                create_symlink(args.sourcedir, patches_dir)
                patchfns.create_db(os.path.dirname(patches_dir))
            this_series_file = os.path.join(action[1], patchfns.QUILT_SERIES)
            if args.prefix:
                this_series_file = os.path.join(args.prefix, this_series_file)
            if series_file:
                if not os.path.exists(this_series_file):
                    create_symlink(series_file, this_series_file)
            else:
                if not os.path.exists(this_series_file):
                    fobj = open(this_series_file, 'w')
                    fobj.write('# Patch series file for quilt," created by pyquilt\n')
                    if tar_dir is not None:
                        fobj.write('# Sourcedir: %s\n' % tar_dir)
                    if tar_file is not None:
                        fobj.write('# Source: %s\n' % tar_file)
                    fobj.write('# Patchdir: %s\n' % action[1])
                    fobj.write('# \n' % action[1])
                else:
                    fobj = open(this_series_file, 'a')
                fobj.write('%s\n' % action[2])
                fobj.close()
    return cmd_result.OK