1

I need to write a module in python that gets the output of a unix diff -u command and one of the files that were used to create that ouput and in return output the second file.

The diff -u returns a diff file in a unified format

Could anyone explain to me really hoe to understand that unified format?

Giora Guttsait
  • 1,279
  • 15
  • 29

1 Answers1

0

There's a python port of Google's diff-match-patch library:

Install it with pip:

pip install diff-match-patch

An example of applying a patch from the python interpreter:

>>> from diff_match_patch import diff_match_patch
>>> old_version = '''#
... # Mac OS X Notice
... #
... # This file is not used by the host name and address resolution
... # or the DNS query routing mechanisms used by most processes on
... # this Mac OS X system.
... #
... # This file is automatically generated.
... #
... nameserver 192.168.1.1
... nameserver 8.8.8.8'''
>>> patch='''@@ -8,4 +8,4 @@
...  # This file is automatically generated.
...  #
...  nameserver 192.168.1.1
... -nameserver 8.8.8.8
... +nameserver 8.8.4.4'''
>>> patchobj = diff_match_patch()
>>> patches = patchobj.patch_fromText(patch)
>>> patched_version, results = patchobj.patch_apply(patches, old_version)
>>> print str(patched_version)
#
# Mac OS X Notice
#
# This file is not used by the host name and address resolution
# or the DNS query routing mechanisms used by most processes on
# this Mac OS X system.
#
# This file is automatically generated.
#
nameserver 192.168.1.1
nameserver 8.8.4.4
Joe Young
  • 5,749
  • 3
  • 28
  • 27
  • It's not exactly patch that im doing. I need to get either of the diff files and extract the other one. Will this code work both ways? – Giora Guttsait Aug 30 '15 at 12:03
  • I guess I don't have a good understanding of what it is you want to accomplish. You'll have to post exact examples of what you're looking for as the explanation isn't enough. – Joe Young Aug 30 '15 at 14:59
  • I got it. Basically: we create a diff file with diff -u file_a and file_b. We then give program the diff file, and either of file a or b, and in return get the other one as output – Giora Guttsait Aug 30 '15 at 19:26