/usr/lib/python2.7/dist-packages/xxdiff/condrepl.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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | # This file is part of the xxdiff package. See xxdiff for license and details.
"""
Functions for conditional replacement.
"""
__author__ = "Martin Blais <blais@furius.ca>"
# stdlib imports.
import sys, os, optparse, logging
from os.path import *
from subprocess import Popen, PIPE
import shutil
# xxdiff imports.
import xxdiff.backup
import xxdiff.invoke
import xxdiff.checkout
__all__ = ('cond_replace',)
def options_graft(parser):
"""
Graft options on given parser for automatic file backups.
"""
group = optparse.OptionGroup(parser, "Replacement options",
"These options affect the overwriting of the"
"original file.")
group.add_option('-n', '--dry-run', action='store_true',
help="Print the commands that would be executed " +
"but don't really run them.")
group.add_option('-x', '--no-confirm', action='store_true',
help="Do not ask for confirmation with graphical "
"diff viewer.")
parser.add_option_group(group)
return group
def options_validate(opts, parser, logs=None):
"""
Validate replacement options.
"""
pass
# Side-by-side diff2 command.
sbs_diff_cmd = ['diff', '--side-by-side', '--suppress-common-lines']
def cond_replace(origfn, newfn, opts, logs, exitonsame=False, replfn=None):
"""
Given two filenames, spawn xxdiff for a decision, and conditionally replace
the original file with the contents of the new file. This supports options
for backups and SCM checkout.
Arguments:
- 'origfn': filename to process -> string
- 'newfn': the new filename to compare -> string
- 'replfn': if specified, the target filename to replace.
- 'opts': program options object -> Options instance
The options that are used are:
* opts.verbose: How much output to print
Three levels of verbosity are assumed:
- 0: completely silent
- 1: outputs only the decision code and then the filename
(This can be useful for scripts to process the output of a run)
- 2: outputs a side-by-side diff of the changes.
* opts.dry_run: Whether to actually apply the changes or not.
* opts.no_confirm: Apply the changes without confirmation.
- 'exitonsame': do not even pop xxdiff if the files are the same
Returns the decision code for the file.
"""
origfn = normpath(abspath(origfn))
# Print header
if opts.verbose >= 2:
print >> logs, '=' * 80
print >> logs, 'File: ', origfn
if opts.verbose >= 2 or exitonsame:
# Run diff between the original and the new/modified file
p = Popen(sbs_diff_cmd + [origfn, newfn], stdout=PIPE, stderr=PIPE)
diff_output, stderr = p.communicate()
# Print differences.
if p.returncode == 0:
if opts.verbose >= 2:
print >> logs, "(Warning: no differences.)"
if exitonsame:
print_decision('NODIFF', origfn, opts, logs)
return 'NODECISION'
else:
diff_output = None
if replfn is None:
replfn = origfn
if opts.no_confirm:
# No graphical diff, just replace the files without asking.
do_replace_file(replfn, newfn, opts, logs)
decision = 'NOCONFIRM'
print_decision(decision, origfn, opts, logs)
if opts.verbose >= 2 and diff_output: print_diffs(diff_output, logs)
else:
# Call xxdiff!
decision, mergedf, retcode = xxdiff.invoke.xxdiff_decision(
opts, '--title2', 'NEW FILE', origfn, newfn)
print_decision(decision, origfn, opts, logs)
if decision == 'ACCEPT':
# Changes accepted.
if replfn != newfn:
do_replace_file(replfn, newfn, opts, logs)
if opts.verbose >= 2 and diff_output: print_diffs(diff_output, logs)
elif decision == 'REJECT':
# Rejected change (or program killed).
if replfn != origfn:
do_replace_file(replfn, origfn, opts, logs)
if opts.verbose >= 2 and diff_output: print_diffs(diff_output, logs)
elif decision == 'NODECISION':
# Program killed, do nothing.
pass
elif decision == 'MERGED':
if opts.verbose >= 2:
# Run diff again to show the real changes that will be applied.
p = Popen(sbs_diff_cmd + [origfn, mergedf.name],
stdout=PIPE, stderr=PIPE)
diff_output, stderr = p.communicate()
if diff_output: print_diffs(diff_output, logs)
do_replace_file(replfn, mergedf.name, opts, logs)
return decision
def print_decision(decision, origfn, opts, logs):
"""
Print the decision string.
"""
if opts.verbose >= 2:
print >> logs, decision
elif opts.verbose >= 1:
print >> logs, '%-10s %s' % (decision, origfn)
def print_diffs(diff_output, logs):
"""
Format nicely and print the output of side-by-side diff.
"""
print >> logs
for line in map(str.expandtabs, diff_output.splitlines()):
print >> logs, ' |', line
print >> logs
def do_replace_file(ofn, nfn, opts, logs):
"""
Function that performs the file replacement safely. Replace the original
file 'ofn' by 'nfn'.
"""
if opts.dry_run:
return
# Backup the original file first.
if hasattr(opts, 'backup_type'):
xxdiff.backup.backup_file(ofn, opts, logs)
# Insure that the file is checked out.
if getattr(opts, 'checkout', None):
xxdiff.checkout.insure_checkout(ofn, opts, logs)
# Copy the new file over the original.
if not os.access(ofn, os.W_OK):
logging.warn("Could not overwrite file '%s'." % ofn)
else:
shutil.copyfile(nfn, ofn)
diff3_cmd = ['diff3']
proper_decisions = ('ACCEPT', 'REJECT', 'MERGED')
def cond_resolve(mine, ancestor, yours, output, opts, logs=None, extra=None):
"""
Given three filenames, 'mine', 'ancestor', 'yours', spawn a 3-way xxdiff for
a decision, and replace the 'original' file with the contents of the merged
output.
Arguments:
- 'mine', 'ancestor', 'yours': files that contains the local file before
merging, the common ancestor file of 'mine' and 'yours', and 'yours' the
merged file, -> strings
- 'output': the filename where we want to store the merged results (in the
case of a merge, the original filename) -> string
- 'opts': program options object -> Options instance
The options that are used are:
* opts.verbose: How much output to print
Three levels of verbosity are assumed:
- 0: completely silent
- 1: outputs only the decision code and then the filename
(This can be useful for scripts to process the output of a run)
- 2: outputs a side-by-side diff of the changes.
* opts.dry_run: Whether to actually apply the changes or not.
- 'extra': are extra parameters to pass on to xxdiff.
Returns the decision code for the file.
"""
mine, ancestor, yours, output = map(abspath,
(mine, ancestor, yours, output))
files3 = [mine, ancestor, yours]
# Print header
if opts.verbose >= 2:
print >> logs, '=' * 80
print >> logs, 'File: ', output
if opts.verbose >= 2:
# Run diff between the three files.
p = Popen(diff3_cmd + files3, stdout=PIPE, stderr=PIPE)
diff_output, stderr = p.communicate()
# Print differences.
if not diff_output: # Note: we cannot rely on the return code.
if opts.verbose >= 2:
print >> logs, "(Warning: no differences.)"
print_decision('NODIFF', mine, opts, logs)
return 'NODECISION'
else:
diff_output = None
# Add arguments to identify files in the title bars.
dargs = xxdiff.invoke.title_opts('%s (WORKING)' % mine,
'%s (ANCESTOR)' % ancestor,
'%s (MERGED/NEW BASE)' % yours)
if extra is not None:
dargs = list(extra) + dargs
# Call xxdiff!
dargs.extend(files3)
decision, mergedf, retcode = xxdiff.invoke.xxdiff_decision(opts, *dargs)
print_decision(decision, mine, opts, logs)
# Backup all 3 files and the destination output before overwriting.
if decision != 'NODECISION' and not opts.dry_run:
if hasattr(opts, 'backup_type'):
xxdiff.backup.backup_file(output, opts, logs)
if decision == 'ACCEPT':
# Accept: discard our local changes and replace file with the update.
shutil.copyfile(yours, output)
elif decision == 'REJECT':
# Reject: discard the update and keep our local file as it were before
# the merge.
shutil.copyfile(mine, output)
elif decision == 'MERGED':
# Merge: replace the output file with the merged output of xxdiff.
shutil.copyfile(mergedf.name, output)
elif decision == 'NODECISION':
# No Decision: Do not change anything.
pass
if opts.verbose >= 2 and decision in proper_decisions:
# Run diff again to show the real changes that will be applied to
# 'mine' into the output file.
p = Popen(sbs_diff_cmd + [mine, output], stdout=PIPE, stderr=PIPE)
diff_output, stderr = p.communicate()
if diff_output:
print_diffs(diff_output, logs)
return decision
def test():
"""
Simple interactive test.
"""
import tempfile
from pprint import pprint
newdir = tempfile.mkdtemp()
fm = dict(map(lambda x: ('file%d' % x, join(newdir, 'file%d' % x)),
xrange(1, 5)))
pprint(fm)
os.system('cp /home/blais/.emacs %(file1)s' % fm) # TEST
os.system('cat %(file1)s | sed -e "s/contr/FREAK/" > %(file2)s' % fm) # TEST
class Opts:
"dummy options class"
verbose = 2
dry_run = False
no_confirm = False
print cond_replace(fm['file1'], fm['file2'], Opts, sys.stdout)
pprint(fm)
if __name__ == '__main__':
test()
|