示例#1
0
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
示例#2
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
示例#3
0
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
示例#4
0
def run_rename(args):
    patchfns.chdir_to_base_dir()
    patch = patchfns.find_patch_in_series(args.opt_patch)
    if not patch:
        return cmd_result.ERROR
    new_patch = patchfns.patch_name_base(args.new_name)
    new_patch_exists = patchfns.patch_in_series(new_patch)
    new_patch_exists = True if new_patch_exists else os.path.isdir(os.path.join(patchfns.QUILT_PC, new_patch))
    new_patch_exists = True if new_patch_exists else os.path.exists(patchfns.patch_file_name(new_patch))
    if new_patch_exists:
        output.error('Patch %s exists already, please choose a different name\n' % patchfns.print_patch(new_patch))
        return cmd_result.ERROR
    is_ok = True
    if patchfns.is_applied(patch):
        is_ok = patchfns.rename_in_db(patch, new_patch)
        if is_ok:
            is_ok = move_file(os.path.join(patchfns.QUILT_PC, patch), os.path.join(patchfns.QUILT_PC, new_patch))
    if is_ok:
        is_ok = patchfns.rename_in_series(patch, new_patch)
        if is_ok and os.path.exists(patchfns.patch_file_name(patch)):
            is_ok = move_file(patchfns.patch_file_name(patch), patchfns.patch_file_name(new_patch))
    if is_ok:
        output.write('Patch %s renamed to %s\n' % (patchfns.print_patch(patch), patchfns.print_patch(new_patch)))
        return cmd_result.OK
    else:
        output.error('Renaming of patch %s to %s failed\n' % (patchfns.print_patch(patch), patchfns.print_patch(new_patch)))
        return cmd_result.ERROR
示例#5
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
示例#6
0
def run_next(args):
    patchfns.chdir_to_base_dir()
    nextpatch = patchfns.find_unapplied_patch(args.arg_patch)
    if not nextpatch:
        return cmd_result.ERROR
    output.write('%s\n' % patchfns.print_patch(nextpatch))
    return cmd_result.OK
示例#7
0
def remove_patch(patch, force, check, silent):
    try:
        status = True
        if not force and (check or files_may_have_changed(patch)):
            status = check_for_pending_changes(patch)
        if status is True:
            patchdir = os.path.join(patchfns.QUILT_PC, patch)
            try:
                os.remove(os.path.join(patchdir, '.timestamp'))
            except OSError:
                pass
            if not os.path.exists(patchdir) or not os.listdir(patchdir):
                output.write('Patch %s appears to be empty, removing\n' % patchfns.print_patch(patch))
                try:
                    os.rmdir(patchdir)
                except OSError as edata:
                    if edata.errno != errno.ENOENT:
                        output.error('%s: %s\n' % (patchdir, edata.errstring))
                        status = False
            else:
                output.write('Removing patch %s\n' % patchfns.print_patch(patch))
                if not backup.restore(patchdir, touch=True, verbose=not silent):
                    status = False
            patchfns.remove_from_db(patch)
            try:
                os.remove(os.path.join(patchdir + '~refresh'))
            except OSError as edata:
                if edata.errno != errno.ENOENT:
                    output.error('%s: %s\n' % (patchdir, edata.errstring))
                    status = False
        return status
    except KeyboardInterrupt:
        return False
示例#8
0
def scan_unapplied(category, prefix, file_paths, patches):
    for patch in patches:
        strip = patchfns.patch_strip_level(patch)
        pfn = patchfns.patch_file_name(patch)
        patch_files = putils.get_patch_files(pfn, strip_level=strip)
        for file_path in file_paths:
            if file_path in patch_files:
                output.write(colour.wrap('%s%s\n' % (prefix, patchfns.print_patch(patch)), category))
示例#9
0
def run_applied(args):
    patchfns.chdir_to_base_dir()
    patch = patchfns.find_applied_patch(args.patch)
    if not patch:
        return cmd_result.ERROR
    output.start_pager()
    for patch in patchfns.applied_before(patch) + [patch]:
        output.write('%s\n' % patchfns.print_patch(patch))
    output.wait_for_pager()
    return cmd_result.OK
示例#10
0
def run_previous(args):
    patchfns.chdir_to_base_dir()
    patch = patchfns.find_patch_in_series(args.arg_patch)
    if not patch:
        return cmd_result.ERROR
    befores = patchfns.applied_before(patch)
    if not befores:
        return cmd_result.ERROR
    output.write('%s\n' % patchfns.print_patch(befores[-1]))
    return cmd_result.OK
示例#11
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
示例#12
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
示例#13
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
示例#14
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
示例#15
0
 def remove_file(backup):
     try:
         if verbose:
             output.write('Removing %s\n' % backup)
         os.unlink(backup)
     except OSError as edata:
         output.perror(edata, backup)
         return False
     try:
         _remove_parents(backup)
     except IOError as edata:
         output.perror(edata, backup)
         return False
     return True
示例#16
0
def run_edit(args):
    patchfns.chdir_to_base_dir()
    if patchfns.pyquilt_command('add %s' % ' '.join(args.filelist)) not in [0, 2]:
        return cmd_result.ERROR
    efilelist = args.filelist if not patchfns.SUBDIR else [os.path.join(patchfns.SUBDIR, fnm) for fnm in args.filelist]
    os.environ['LANG'] = patchfns.ORIGINAL_LANG
    result = shell.run_cmd('%s %s' % (os.getenv('EDITOR'), ' '.join(efilelist)))
    output.error(result.stderr)
    output.write(result.stdout)
    status = cmd_result.OK if result.eflags == 0 else cmd_result.ERROR
    for filename in args.filelist:
        efname = filename if not patchfns.SUBDIR else os.path.join(patchfns.SUBDIR, filename)
        if not os.path.exists(efname):
            patchfns.pyquilt_command('revert %s' % filename)
            status = cmd_result.ERROR
    return status
示例#17
0
def run_files(args):
    patchfns.chdir_to_base_dir()
    first_patch = None
    if args.opt_combine:
        args.opt_all = True
        if args.opt_combine != '-':
            first_patch = patchfns.find_patch_in_series(args.opt_combine)
            if not first_patch:
                return cmd_result.ERROR
    last_patch = patchfns.find_applied_patch(args.patch)
    if not last_patch:
        return cmd_result.ERROR
    if args.opt_all:
        if not first_patch:
            first_patch = patchfns.applied_patches()[0]
        patches = patchfns.patches_before(last_patch) + [last_patch]
        if first_patch not in patches:
            output.error('Patch %s not applied before patch %s\n' % (patchfns.print_patch(first_patch), patchfns.print_patch(last_patch)))
            return cmd_result.ERROR
        patches = patches[patches.index(first_patch):]
    else:
        patches = [last_patch]
    use_status = args.opt_verbose and not args.opt_labels
    # Note: If opt_labels is set, then use_status is not set.
    output.start_pager()
    for patch in patches:
        if args.opt_all and args.opt_verbose and not args.opt_labels:
            output.write('%s\n' % patch)
        for filename in sorted(patchfns.files_in_patch(patch)):
            if args.opt_labels:
                if args.opt_verbose:
                    output.write('[%s] ' % patch)
                else:
                    output.write('%s ' % patch)
            if not use_status:
                output.write('%s\n' % filename)
            else:
                status = ' '
                buname = patchfns.backup_file_name(patch, filename)
                if os.path.exists(buname) and os.path.getsize(buname) > 0:
                    if not os.path.exists(filename) or os.path.getsize(filename) == 0:
                        status = '-'
                elif os.path.exists(filename) or os.path.getsize(filename) > 0:
                    status = '+'
                output.write('%s %s\n' % (status, filename))
    output.wait_for_pager()
    return cmd_result.OK
示例#18
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
示例#19
0
def ensure_nolinks_in_dir(in_dir, verbose=False):
    def onerror(exception):
        raise exception
    if not os.path.isdir(in_dir):
        return False
    try:
        for basedir, dirnames, filenames in os.walk(in_dir, onerror=onerror):
            for filename in filenames:
                filename = os.path.join(basedir, filename)
                if verbose:
                    output.write('Delinking %s\n' % filename)
                if not ensure_nolinks(filename):
                    return False
    except IOError as edata:
        output.perror(edata)
        return False
    return True
示例#20
0
def find_unapplied_patch(name=None):
    if name is not None:
        patch = find_patch(name)
        if not patch:
            return False
        if is_applied(patch):
            output.write('Patch %s is currently applied\n' % print_patch(patch))
            return False
        return patch
    else:
        start = top_patch()
        if not start:
            return find_first_patch()
        patch = patch_after(start)
        if not patch:
            output.write('File series fully applied, ends at patch %s\n' % print_patch(start))
            return False
        return patch
示例#21
0
def process_mail(message, args):
    if args.opt_send:
        sendmail_cmd = '%s %s --f %s ' % (os.getenv('QUILT_SENDMAIL', 'sendmail'), os.getenv('QUILT_SENDMAIL_ARGS', ''), args.opt_sender)
        sendmail_cmd += extract_recipients(message)
        output.write(sendmail_cmd)
        del message['Bcc']
        result = shell.run_cmd(sendmail_cmd, message.as_string(False))
        output.write(result.stdout)
        output.error(result.stderr)
    else:
        from_date = time.strftime('+%a %b %e %H:%M:%S %Y')
        fobj = open(args.opt_mbox, 'a')
        fobj.write('From %s %s\n' % (args.opt_sender_address, from_date))
        for field, value in message.items():
            fobj.write('%s: %s\n' % (field, value))
        fobj.write('\n')
        for line in message.get_payload().splitlines(True):
            fobj.write(re.sub('^From ', '>From ', line))
        fobj.close()
示例#22
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
示例#23
0
def run_pop(args):
    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 = patchfns.find_applied_patch(args.patchnamornum)
            if not stop_at_patch:
                return cmd_result.ERROR
    elif not args.opt_all:
        number = 1
    silent = args.opt_quiet
    if patchfns.top_patch_needs_refresh() and not args.opt_force:
        output.error('The topmost patch %s needs to be refreshed first.\n' % patchfns.print_top_patch())
        return cmd_result.ERROR | cmd_result.SUGGEST_FORCE_OR_REFRESH
    patches = list_patches(number=number, stop_at_patch=stop_at_patch)
    if not patches:
        output.error('No patch removed\n')
        return cmd_result.ERROR
    is_ok = True
    for patch in patches:
        result = remove_patch(patch, force=args.opt_force, check=args.opt_remove, silent=silent)
        if result is not True:
            return cmd_result.ERROR if result is False else result
        if not args.opt_quiet:
            output.write('\n')
    if not patchfns.top_patch():
        output.write('No patches applied\n')
    else:
        output.write('Now at patch %s\n' % patchfns.print_top_patch())
    return cmd_result.OK if is_ok is True else cmd_result.ERROR
示例#24
0
def do_diff(filename, old_file, new_file, args):
    """Output the diff for the nominated files"""
    if args.opt_reverse:
        old_file, new_file = new_file, old_file
    if args.opt_diff:
        if not os.path.exists(old_file):
            old_file = '/dev/null'
        if not os.path.exists(new_file):
            new_file = '/dev/null'
        if not diff.same_contents(old_file, new_file):
            os.environ['LANG'] = patchfns.ORIGINAL_LANG
            shell.run_cmd('%s %s %s' % (args.opt_diff, old_file, new_file))
            os.environ['LANG'] = 'POSIX'
            return True
    else:
        result = diff.diff_file(filename, old_file, new_file, args)
        output.error(result.stderr)
        if args.opt_color:
            output.write(colorize(result.stdout))
        else:
            output.write(result.stdout)
        return result.eflags < 2
示例#25
0
 def backup_file(file_nm):
     backup = os.path.join(bu_dir, file_nm)
     try:
         stat_data = os.stat(file_nm)
     except OSError as edata:
         missing_file = edata.errno == errno.ENOENT
     else:
         missing_file = False
     try:
         os.unlink(backup)
     except OSError as edata:
         if edata.errno != errno.ENOENT:
             output.perror(edata)
             return False
     _create_parents(backup)
     if missing_file:
         if verbose:
             output.write('New file %s\n' % file_nm)
         try:
             os.close(_creat(backup, mode=0666))
         except OSError as edata:
             output.perror(edata)
             return False
     else:
         if verbose:
             output.write('Copying %s\n' % file_nm)
         if stat_data.st_nlink == 1:
             result = _copy_file(file_nm, stat_data, backup)
             if result is not True:
                 return result
         else:
             result = _link_or_copy_file(file_nm, stat_data, backup)
             if result is not True:
                 return result
             result = ensure_nolinks(file_nm)
             if result is not True:
                 return result
         os.utime(backup, (stat_data.st_mtime, stat_data.st_mtime,))
     return True
示例#26
0
def run_grep(args):
    patchfns.chdir_to_base_dir()
    problem_args = [] # i.e. those wit optional arguments
    for arg in ['--color', '--colour']:
        while arg in args.remainder_of_args:
            problem_args.append(arg)
            args.remainder_of_args.remove(arg)
    grep_opts, grep_args = getopt.getopt(args.remainder_of_args, grep_options, grep_long_options)
    opt_list = make_opt_list(grep_opts) + problem_args
    if expect_pattern(grep_opts):
        files = grep_args[1:]
        opt_list += grep_args[0:1]
    else:
        files = grep_args
    if not files:
        files = get_files()
    if len(files) == 1 and '-h' not in opt_list and '--no-filename' not in opt_list:
        opt_list.append('-H')
    result = shell.run_cmd(['grep'] + opt_list + files)
    output.write(result.stdout)
    output.error(result.stderr)
    return result.eflags
示例#27
0
 def restore_file(file_nm):
     backup = os.path.join(bu_dir, file_nm)
     file_nm = file_nm if to_dir is None else os.path.relpath(os.path.join(to_dir, file_nm))
     _create_parents(file_nm)
     try:
         stat_data = os.stat(backup)
     except OSError as edata:
         output.perror(edata, backup)
         return False
     if stat_data.st_size == 0:
         try:
             os.unlink(file_nm)
         except OSError as edata:
             if edata.errno != errno.ENOENT:
                 output.perror(edata, file_nm)
                 return False
         if verbose:
             output.write('Removing %s\n' % file_nm)
         if not keep:
             os.unlink(backup)
             _remove_parents(backup)
     else:
         if verbose:
             output.write('Restoring %s\n' % file_nm)
         try:
             os.unlink(file_nm)
         except OSError as edata:
             if edata.errno != errno.ENOENT:
                 raise
         if not _link_or_copy_file(backup, stat_data, file_nm):
             return False
         if not keep:
             os.unlink(backup)
             _remove_parents(backup)
         if touch:
             os.utime(file_nm, None)
         else:
             os.utime(file_nm, (stat_data.st_mtime, stat_data.st_mtime))
     return True
示例#28
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
示例#29
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
示例#30
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