/usr/lib/python2.7/dist-packages/xxdiff/xformloop.py is in xxdiff-scripts 1:4.0.1+hg487+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | # This file is part of the xxdiff package. See xxdiff for license and details.
"""
Functions to transform files via a search loop. This is meant to be used to
implemnent the filter scripts, and provided as infrastructure for the user to
write his own filtering scripts.
"""
__author__ = "Martin Blais <blais@furius.ca>"
# stdlib imports.
import sys, os, tempfile
from os.path import *
# xxdiff imports.
import xxdiff.scripts
import xxdiff.selectfiles
import xxdiff.backup
import xxdiff.invoke
import xxdiff.checkout
import xxdiff.condrepl
from xxdiff.scripts import tmpprefix
__all__ = ['Transformer', 'parse_args', 'transform_replace_loop']
class Transformer(object):
"""
Base class for all transformers.
"""
def __init__(self, opts):
"""
'opts': an options object -> Option
We're expecting to find opts.dry_run on it.
"""
self.opts = opts
def transform(self, fn, outf):
"""
Process the file 'fn' and write the transformed contents to the
temporary file 'tmpf' (this file object is already opened in 'w' mode.
Return True if the file if the transformation was succesful, False if
the file should be skipped.
"""
raise NotImplementedError
def parse_args(parser):
"""
Parse the options and return:
- an options object -> Options
- program arguments -> list of strings
- a selector of the files to iterate over -> generator
"""
xxmodules = (xxdiff.selectfiles, xxdiff.backup, xxdiff.checkout,
xxdiff.invoke, xxdiff.condrepl)
for mod in xxmodules:
mod.options_graft(parser)
xxdiff.scripts.install_autocomplete(parser)
# Parse arguments
opts, args = parser.parse_args()
vargs = opts, parser, sys.stdout
for mod in xxmodules[1:]:
mod.options_validate(*vargs)
selector = xxdiff.selectfiles.options_validate(*vargs)
return opts, args, selector
def transform_replace_loop(opts, selector, xformer, logs):
"""
Run a loop through the selected files and conditionally transform them by
applying the given Transformer and invoking xxdiff to confirm. Return a map
of the filenames to the decision code that was used on each of them.
"""
decision_map = {}
for fn in selector:
# Create temporary file to receive the transformed file.
tmpf = tempfile.NamedTemporaryFile('w', prefix=tmpprefix)
# Transform the file.
if xformer.transform(fn, tmpf) is False:
# This file is to be skipped by the transformer for some reason.
decision_map[fn] = 'SKIPPED'
continue
# Flush the temporary file before invoking xxdiff on it.
tmpf.flush()
# Invoke conditional replacement via xxdiff.
decision = xxdiff.condrepl.cond_replace(fn, tmpf.name, opts, logs)
decision_map[fn] = decision
return decision_map
def postloop_footer(opts, decision_map=None):
"""
Print some common stuff at the end of the looping scripts.
You don't have to add this to your script, but it's convenient.
"""
xxdiff.backup.print_reminder(opts)
|