Esempio n. 1
0
def merge_patches(old, new, opt_desc):
    """Return the merge of the old and new patches"""
    old_desc = patchfns.gen_tempfile()
    open(old_desc, 'w').write(putils.get_patch_descr(old))
    new_desc = patchfns.gen_tempfile()
    open(new_desc, 'w').write(putils.get_patch_descr(new))
    if opt_desc is None:
        if os.path.getsize(old_desc) == 0:
            opt_desc = 'n'
        elif os.path.getsize(new_desc) == 0:
            opt_desc = 'o'
        if opt_desc is None:
            result = shell.run_cmd('diff -u %s %s' % (old_desc, new_desc))
            diff_lines = result.stdout.splitlines(True)
            if len(diff_lines) > 2:
                output.error('Patch headers differ:\n')
                output.error(''.join(diff_lines[2:]))
                output.error('Please use -d {o|a|n} to specify which patch header(s) to keep.\n')
                os.remove(old_desc)
                os.remove(new_desc)
                return False
    patchtext = open(old_desc).read() if opt_desc != 'n' else ''
    if opt_desc == 'a':
        patchtext += '---\n'
    if opt_desc == 'o':
        patchtext += putils.get_patch_diff(new)
    else:
        patchtext += fsutils.get_file_contents(new)
    os.remove(old_desc)
    os.remove(new_desc)
    return patchtext
Esempio n. 2
0
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
Esempio n. 3
0
def run_header(args):
    def read_input():
        text = sys.stdin.read()
        if args.opt_strip_trailing_whitespace:
            return _trim_trailing_ws(text)
        return text
    def get_text(pfile):
        text = putils.get_patch_hdr(pfile, omit_diffstat=args.opt_strip_diffstat)
        if args.opt_strip_trailing_whitespace:
            return _trim_trailing_ws(text)
        return text
    def set_text(pfile, text):
        if args.opt_backup:
            try:
                shutil.copy2(pfile, pfile + '~')
            except Exception as edata:
                output.perror(edata)
        if args.opt_strip_trailing_whitespace:
            text = _trim_trailing_ws(text)
        putils.set_patch_hdr(pfile, text, omit_diffstat=args.opt_strip_diffstat)
    patchfns.chdir_to_base_dir()
    if not args.opt_backup:
        args.opt_backup = customization.get_config('QUILT_BACKUP')
    patch = patchfns.find_patch_in_series(args.arg_patch)
    if not patch:
        return cmd_result.ERROR
    patch_file = patchfns.patch_file_name(patch)
    if args.opt_replace:
        set_text(patch_file, read_input())
        output.write('Replaced header of patch %s\n' % patchfns.print_patch(patch))
    elif args.opt_append:
        set_text(patch_file, get_text(patch_file) + read_input())
        output.write('Appended text to header of patch %s\n' % patchfns.print_patch(patch))
    elif args.opt_edit:
        savelang = os.getenv('LANG', None)
        os.environ['LANG'] = patchfns.ORIGINAL_LANG
        tempfile = patchfns.gen_tempfile()
        result = shell.run_cmd('%s %s' % (os.getenv('EDITOR'), tempfile))
        if savelang:
            os.environ['LANG'] = savelang
        output.error(result.stderr)
        output.write(result.stdout)
        text = open(tempfile).read()
        os.remove(tempfile)
        if result.eflags != 0:
            return cmd_result.ERROR
        set_text(patch_file, text)
        output.write('Replaced header of patch %s\n' % patchfns.print_patch(patch))
    else:
        if not os.path.exists(patch_file):
            return cmd_result.OK
        output.start_pager()
        output.write(get_text(patch_file))
        output.wait_for_pager()
    return cmd_result.OK
Esempio 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
Esempio n. 5
0
def run_annotate(args):
    patchfns.chdir_to_base_dir()
    args.opt_patch = patchfns.find_applied_patch(args.opt_patch)
    opt_file = os.path.join(patchfns.SUBDIR, args.filename)
    if not args.opt_patch:
        return cmd_result.ERROR
    patches = []
    files = []
    next_patch = None
    for patch in patchfns.applied_patches():
        old_file = patchfns.backup_file_name(patch, opt_file)
        if os.path.isfile(old_file):
            patches.append(patch)
            files.append(old_file)
        if patch == args.opt_patch:
            next_patch = patchfns.next_patch_for_file(patch, opt_file)
            break
    if not next_patch:
        files.append(opt_file)
    else:
        files.append(patchfns.backup_file_name(next_patch, opt_file))
    if len(patches) == 0:
        for line in open(files[-1]).readlines():
            output.write('\t%s\n' % line)
        return cmd_result.OK
    difftxt = ''
    for index in range(len(patches)):
        difftxt += annotation_for(files[index], files[index + 1], index + 1)
    template = patchfns.gen_tempfile()
    open(template, 'w').write('\n' * len(open(files[0]).readlines()))
    shell.run_cmd('patch %s' % template, difftxt)
    annotations = [line.rstrip() for line in open(template).readlines()]
    os.remove(template)
    output.start_pager()
    if os.path.exists(files[-1]):
        for annotation, line in zip(annotations, open(files[-1]).readlines()):
            output.write('%s\t%s' % (annotation, line))
    output.write('\n')
    for index, patch in zip(range(len(patches)), patches):
        output.write('%s\t%s\n' % (index + 1, patchfns.print_patch(patch)))
    output.wait_for_pager()
    return cmd_result.OK
Esempio n. 6
0
def check_for_pending_changes(patch):
    patch_file = patchfns.patch_file_name(patch)
    workdir = patchfns.gen_tempfile(template='quilt', asdir=True)
    patchdir = os.path.join(patchfns.QUILT_PC, patch)
    if os.path.isdir(patchdir):
        prefix = os.path.abspath(patchdir)
        if not backup.restore(prefix, to_dir=workdir, keep=True):
            output.error('Failed to copy files to temporary directory\n')
            shutil.rmtree(workdir)
            return False
    if os.path.exists(patch_file) and os.path.getsize(patch_file) > 0:
        patch_args = '%s --no-backup-if-mismatch -E' % ' '.join(patchfns.patch_args(patch))
        result = putils.apply_patch(patch_file, indir=workdir, patch_args=patch_args)
        if result.eflags != 0 and not os.path.exists(patchdir):
            output.write(result.stdout)
            output.error('Failed to patch temporary files\n')
            shutil.rmtree(workdir)
            return False
    failed = False
    for file_nm in patchfns.files_in_patch(patch):
        wfile_nm = os.path.join(workdir, file_nm)
        if not os.path.exists(file_nm):
            if os.path.exists(wfile_nm):
                failed = True
                break
            else:
                continue
        elif not os.path.exists(wfile_nm):
            failed = True
            break
        if not diff.same_contents(file_nm, wfile_nm):
            failed = True
            break
    shutil.rmtree(workdir)
    if failed:
        output.error('Patch %s does not remove cleanly (refresh it or enforce with -f)\n' % patchfns.print_patch(patch))
        return cmd_result.ERROR_SUGGEST_FORCE
    return True
Esempio n. 7
0
 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
Esempio n. 8
0
def run_refresh(args):
    workdir = None
    def clean_up(status):
        if workdir and os.path.isdir(workdir):
            shutil.rmtree(workdir)
        return status
    patchfns.chdir_to_base_dir()
    patch = patchfns.find_applied_patch(args.patchname)
    if not patch:
        return cmd_result.ERROR
    if not args.opt_sort:
        files = patchfns.files_in_patch_ordered(patch)
    else:
        files = patchfns.files_in_patch(patch)
    if args.opt_new_name:
        if args.patchname:
            output.error('Can only refresh the topmost patch with -z currently\n')
            return cmd_result.ERROR
        old_patch = patch
        old_patch_args = patchfns.patch_args(old_patch)
        if args.opt_new_name is True:
            patch = patchfns.next_filename(patch)
        else:
            patch = args.opt_new_name
        if os.path.exists(patchfns.patch_file_name(patch)):
            output.error('Patch %s exists already\n' % patchfns.print_patch(patch))
            return cmd_result.ERROR
    if args.opt_strip_level is None:
        args.opt_strip_level = patchfns.patch_strip_level(patch)
    if args.opt_strip_level in ['0', '1']:
        num_strip_level = args.opt_strip_level
    elif args.opt_strip_level == 'ab':
        num_strip_level = '1'
    else:
        output.error('Cannot refresh patches with -p%s, please specify -p0 or -p1 instead\n' % args.opt_strip_level)
        return cmd_result.ERROR
    if args.opt_new_name:
        workdir = patchfns.gen_tempfile(asdir=True, template=os.path.join(os.getcwd(), 'quilt'))
        if not patchfns.apply_patch_temporarily(workdir, old_patch):
            return clean_up(cmd_result.ERROR)
    patch_content = ''
    files_were_shadowed = False
    for filn in files:
        if args.opt_new_name:
            old_file = os.path.join(workdir, filn)
            new_file = filn
        else:
            old_file = patchfns.backup_file_name(patch, filn)
            next_patch = patchfns.next_patch_for_file(patch, filn)
            if not next_patch:
                new_file = filn
            else:
                new_file = patchfns.backup_file_name(next_patch, filn)
                files_were_shadowed = True
        result = diff.diff_file(filn, old_file, new_file, args)
        if result.eflags > 1:
            output.error('\n'.join(result.stderr, 'Diff failed, aborting\n'))
            return clean_up(cmd_result.ERROR)
        elif result.eflags == 0 or not result.stdout:
            continue
        else:
            patch_content += result.stdout
    patch_file = patchfns.patch_file_name(patch)
    prev_patch_file = patch_file if os.path.isfile(patch_file) else '/dev/null'
    result_content = patchfns.patch_header(prev_patch_file)
    if not patch_content:
        output.error('Nothing in patch %s\n' % patchfns.print_patch(patch))
    else:
        if files_were_shadowed:
            if not args.opt_force:
                output.error('More recent patches modify files in patch %s. Enforce refresh with -f.\n' % patchfns.print_patch(patch))
                return clean_up(cmd_result.ERROR_SUGGEST_FORCE)
            if args.opt_strip_trailing_whitespace:
                output.error('Cannot use --strip-trailing-whitespace on a patch that has shadowed files.\n')
        if args.opt_strip_trailing_whitespace and not files_were_shadowed:
            result = putils.remove_trailing_ws(patch_content, num_strip_level)
            if result.eflags == cmd_result.OK:
                patch_content = result.stdout
            if result.stderr:
                output.error(result.stderr)
        else:
            result = putils.remove_trailing_ws(patch_content, num_strip_level, dry_run=True)
            if result.stderr:
                output.error(result.stderr)
        if args.opt_diffstat:
            diffstat_text = diffstat.get_diffstat(patch_content, num_strip_level)
            result_content += diffstat_text
        result_content += patch_content
    patch_file_dir = os.path.dirname(patch_file)
    if not os.path.exists(patch_file_dir):
        os.makedirs(patch_file_dir)
    is_ok = True
    QUILT_PC = customization.get_config('QUILT_PC')
    if fsutils.file_contents_equal(patch_file, result_content):
        output.write('Patch %s is unchanged\n' % patchfns.print_patch(patch))
    else:
        if args.opt_backup and os.path.isfile(patch_file):
            try:
                os.rename(patch_file, patch_file + '~')
            except OSError:
                output.error('Failed to create backup %s\n' % patch_file + '~')
                is_ok = False
        if is_ok:
            is_ok = fsutils.set_file_contents(patch_file, result_content)
        if is_ok and args.opt_new_name:
            insert_ok = patchfns.insert_in_series(patch, old_patch_args)
            if not insert_ok:
                output.error('Failed to insert patch %s into file series\n' % patchfns.print_patch(patch))
                return clean_up(cmd_result.ERROR)
            try:
                patch_dir = os.path.join(QUILT_PC, patch)
                if os.path.exists(patch_dir):
                    shutil.rmtree(patch_dir)
                os.rename(workdir, patch_dir)
                open(patchfns.DB, 'a').write(patch + '\n')
            except:
                output.error('Failed to create patch %s\n' % patchfns.print_patch(patch))
                return clean_up(cmd_result.ERROR)
            output.write('Fork of patch %s created as %s\n' % (patchfns.print_patch(old_patch), patchfns.print_patch(patch)))
        elif is_ok and patch_content:
            output.write('Refreshed patch %s\n' % patchfns.print_patch(patch))
        fsutils.touch(os.path.join(QUILT_PC, patch, '.timestamp'))
    if is_ok:
        tagf = os.path.join(QUILT_PC, patch + '~refresh')
        if os.path.exists(tagf):
            os.remove(tagf)
        is_ok = patchfns.change_db_strip_level('-p%s' % num_strip_level, patch)
    return clean_up(cmd_result.OK if is_ok and patch_content else cmd_result.ERROR)
Esempio n. 9
0
def run_diff(args):
    patchfns.chdir_to_base_dir()
    snap_subdir = os.path.join(patchfns.QUILT_PC, '.snap') if args.opt_snapshot else None
    if args.opt_combine:
        first_patch = '-' if args.opt_combine == '-' else patchfns.find_applied_patch(args.opt_combine)
        if not first_patch:
            return cmd_result.ERROR
    else:
        first_patch = None
    if len([opt for opt in [args.opt_combine, args.opt_snapshot, args.opt_relative] if opt]) > 1:
        output.error('Options `--combine\', `--snapshot\', and `-z\' cannot be combined.\n')
        return cmd_result.ERROR
    last_patch = patchfns.find_applied_patch(args.last_patch)
    if not last_patch:
        return cmd_result.ERROR
    if args.opt_strip_level is None:
        args.opt_strip_level = patchfns.patch_strip_level(last_patch)
    if args.opt_strip_level not in ['0', '1', 'ab']:
        output.error('Cannot diff patches with -p%s, please specify -p0, -p1, or -pab instead\n' % args.opt_strip_level)
        return cmd_result.ERROR
    files = []
    if args.opt_snapshot and len(args.opt_files) == 0:
        for path, _dirs, bases in os.walk(snap_subdir):
            rpath = '' if path == snap_subdir else os.path.relpath(path, snap_subdir)
            files += [os.path.join(rpath, base) for base in bases]
        files.sort()
        args.opt_combine = True
        first_patch = patchfns.applied_patches()[0]
    if args.opt_combine:
        patches = patchfns.patches_before(last_patch) + [last_patch]
        if first_patch != '-':
            try:
                patches = patches[patches.index(first_patch):]
            except ValueError:
                output.error('Patch %s not applied before patch %s\n' % (patchfns.print_patch(first_patch), patchfns.print_patch(last_patch)))
                return cmd_result.ERROR
    else:
        patches = [last_patch]
    if len(args.opt_files) > 0:
        # use a set as it should be more efficient
        ofiles = set()
        for ofile in args.opt_files:
            if ofile.startswith('.' + os.sep):
                ofile = ofile[2:]
            if patchfns.SUBDIR:
                ofile = os.path.join(patchfns.SUBDIR, ofile)
            ofiles.add(ofile)
        for patch in patches:
            for fname in patchfns.files_in_patch_ordered(patch):
                if fname in ofiles and fname not in files:
                    files.append(fname)
    else:
        for patch in patches:
            for fname in patchfns.files_in_patch_ordered(patch):
                if fname not in files:
                    files.append(fname)
    if args.opt_sort:
        files.sort()
    if args.opt_relative:
        workdir = patchfns.gen_tempfile(os.path.join(os.getcwd(), 'quilt'), asdir=True)
        atexit.register(clean_up, workdir)
        if not patchfns.apply_patch_temporarily(workdir, last_patch, files):
            return cmd_result.ERROR
    is_ok = True
    files_were_shadowed = False
    if args.opt_color:
        colour.set_up()
    output.start_pager()
    for filename in files:
        snapshot_path = os.path.join(snap_subdir, filename) if snap_subdir else None
        if snapshot_path and os.path.exists(snapshot_path):
            old_file = snapshot_path
        elif args.opt_relative:
            old_file = os.path.join(workdir, filename)
        else:
            patch = patchfns.first_modified_by(filename, patches)
            if not patch:
                if not args.opt_snapshot:
                    output.error('File %s is not being modified\n' % filename)
                continue
            old_file = patchfns.backup_file_name(patch, filename)
        next_patch = patchfns.next_patch_for_file(last_patch, filename)
        if not next_patch:
            new_file = filename
        else:
            new_file = patchfns.backup_file_name(next_patch, filename)
            files_were_shadowed = True
        if not do_diff(filename, old_file, new_file, args):
            output.error('Diff failed, aborting\n')
            return cmd_result.ERROR
    if files_were_shadowed:
        output.error('Warning: more recent patches modify files in patch %s\n' % patchfns.print_patch(last_patch))
    output.wait_for_pager()
    return cmd_result.OK
Esempio n. 10
0
def run_mail(args):
    patchfns.chdir_to_base_dir()
    if not shell.which('formail'):
        output.write("You have to install 'formail' to use 'quilt mail'")
        return cmd_result.ERROR
    if not args.opt_signature or args.opt_signature == '-':
        args.opt_signature = None
    else:
        try:
            args.opt_signature = open(args.opt_signature).read()
        except IOError as edata:
            output.perror(edata)
    if args.first_patch:
        args.first_patch = patchfns.find_first_patch() if args.first_patch == '-' else patchfns.find_patch(args.first_patch)
        if not args.first_patch:
            return cmd_result.ERROR
        if not args.last_patch:
            args.last_patch = args.first_patch
        else:
            args.last_patch = patchfns.find_last_patch() if args.last_patch == '-' else patchfns.find_patch(args.last_patch)
            if not args.last_patch:
                return cmd_result.ERROR
    if not args.opt_sender:
        hostname = socket.gethostname()
        args.opt_sender = '%s@%s' % (get_login_name(), hostname)
        if not re.match('^\S+@\S+\.\S+$', args.opt_sender):
            output.error('Could not determine the envelope sender address. Please use --sender.\n')
            return cmd_result.ERROR
    _dummy, args.opt_sender_address = email.utils.parseaddr(args.opt_sender)
    if not args.opt_charset:
        lc_all = os.getenv('LC_ALL', patchfns.ORIGINAL_LANG)
        if lc_all and lc_all.endswith('UTF-8'):
            args.opt_charset = 'UTF-8'
        else:
            args.opt_charset = 'ISO-8859-15'
    patches = patchfns.cat_series()
    if args.first_patch:
        first_index = patches.index(args.first_patch)
        last_index = patches.index(args.last_patch)
        if last_index < first_index:
            output.error('Patch %s not applied before patch %s\n' % (patchfns.print_patch(args.first_patch), patchfns.print_patch(args.first_patch)))
            return cmd_result.ERROR
        patches = patches[first_index:last_index + 1]
    total = len(patches)
    tmpdir = patchfns.gen_tempfile(asdir=True)
    atexit.register(lambda: not os.path.exists(tmpdir) or shutil.rmtree(tmpdir, ignore_errors=True))
    subject_map = {}
    patch_msgs = []
    for patch in patches:
        contents = fsutils.get_file_contents(patchfns.patch_file_name(patch))
        mailmsg = message_from_patch(contents, args.opt_charset)
        if mailmsg is False:
            subject = None
        else:
            patch_msgs.append(mailmsg)
            subject = mailmsg['Replace-Subject']
        if mailmsg is False or not subject:
            output.error('Unable to extract a subject header from %s\n' % patchfns.print_patch(patch))
            return cmd_result.ERROR
        if subject in subject_map:
            subject_map[subject].append(patch)
        else:
            subject_map[subject] = [patch]
    if len(subject_map) != len(patches):
        duplicates = []
        for key in sorted(subject_map):
            plist = subject_map[key]
            if len(plist) > 1:
                duplicates += plist
        output.error('Patches %s have duplicate subject headers.\n' % ', '.join([patchfns.print_patch(dup) for dup in duplicates]))
        return cmd_result.ERROR
    if args.opt_reply_to:
        if not os.path.exists(args.opt_reply_to):
            output.error('File %s does not exist\n' % args.opt_reply_to)
            return cmd_result.ERROR
        args.opt_reply_to = email.message_from_string(open(args.opt_reply_to).read())
        if not args.opt_subject:
            repto_subject = args.opt_reply_to['Subject']
            args.opt_subject = 'Re: %s' % re.sub('^([ \t]*[rR][eE]:[ \t]*)', '', repto_subject)
    intro = 'Message-Id: <%s>\n' % msgid(args)
    intro += 'User-Agent: pyquilt\n'
    last_ts = time.localtime()
    intro += time.strftime('Date: %a, %d %b %Y %H:%M:%S %z\n', last_ts)
    intro += 'From: %s\n' % (args.opt_from if args.opt_from else args.opt_sender)
    intro += 'To: %s\n' % (', '.join(args.opt_to) if args.opt_to else '')
    intro += 'Cc: %s\n' % (', '.join(args.opt_cc) if args.opt_cc else '')
    intro += 'Bcc: %s\n' % (', '.join(args.opt_bcc) if args.opt_bcc else '')
    if args.opt_reply_to:
        intro += in_reply_to_header(args.opt_reply_to)
        intro += references_header(args.opt_reply_to)
    intro += 'Subject-Prefix: [%s @num@/@total@]\n' % args.opt_prefix
    intro += 'Subject: %s\n\n' % (args.opt_subject if args.opt_subject else '')
    intro += ('%s\n\n' % args.opt_message) if args.opt_message else ''
    intro += ('-- \n%s\n' % args.opt_signature) if args.opt_signature else ''
    intro_message = email.message_from_string(intro)
    intro_message.set_charset(args.opt_charset)
    if not args.opt_message:
        introfile = patchfns.gen_tempfile()
        open(introfile, 'w').write(intro_message.as_string())
        result = shell.run_cmd('%s %s' % (os.getenv('EDITOR'), introfile))
        output.write(result.stdout)
        output.error(result.stderr)
        intro_message = email.message_from_string(open(introfile).read(), charset=args.opt_charset)
        os.remove(introfile)
        if result.eflags != 0:
            return cmd_result.ERROR
    subject = join_lines(intro_message['Subject'])
    if not subject:
        if not args.opt_message:
            savefile = patchfns.gen_tempfile()
            open(savefile, 'w').write(intro_message.as_string())
            output.error('Introduction has no subject header (saved as %s)\n' % savefile)
        else:
            output.error('Introduction has no subject header\n')
        return cmd_result.ERROR
    if args.opt_mbox:
        fsutils.touch(args.opt_mbox)
    subject_prefix = email.utils.quote(join_lines(intro_message['Subject-Prefix']))
    subject_prefix += ' ' if subject_prefix else ''
    subject_prefix = re.sub('@total@', str(total), subject_prefix)
    del intro_message['Subject-Prefix']
    pnum_fmt = '{0:0%s}' % len(str(total))
    pfx = re.sub('@num@', pnum_fmt.format(0), subject_prefix)
    intro_message.replace_header('Subject', pfx + intro_message['Subject'])
    remove_empty_headers(intro_message)
    for key in ['To', 'Cc', 'Bcc']:
        # Consolidate up the various recipient fields
        values = intro_message.get_all(key)
        if values:
            del intro_message[key]
            intro_message[key] = ', '.join(values)
    process_mail(intro_message, args)
    pnum = 0
    for msg in patch_msgs:
        msg.add_header('Content-Disposition', 'inline',  filename=patches[pnum])
        for key in intro_message.keys():
            if key.lower() not in ['message-id', 'references', 'in-reply-to', 'subject']:
                for value in intro_message.get_all(key):
                    msg[key] = value
        msg['References'] = get_reference_to(intro_message)
        msg.set_charset(args.opt_charset)
        for aclass in ['To', 'Cc', 'Bcc']:
            rclass = 'Recipient-' + aclass
            if msg.has_key(rclass):
                msg[aclass] = ',\n '.join(msg.get_all(rclass))
                del msg[rclass]
        pnum += 1
        ppfx = re.sub('@num@', pnum_fmt.format(pnum), subject_prefix)
        msg['Message-Id'] = msgid(args)
        msg['Subject'] = '%s%s' % (ppfx, msg['Replace-Subject']) if ppfx else msg['Replace-Subject']
        del msg['Replace-Subject']
        remove_empty_headers(msg)
        process_mail(msg, args)
    return cmd_result.OK
Esempio n. 11
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