Skip to content

Instantly share code, notes, and snippets.

@tkf
Created April 2, 2011 08:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tkf/899325 to your computer and use it in GitHub Desktop.
Save tkf/899325 to your computer and use it in GitHub Desktop.
Command line tool for google-diff-match-patch
#!/usr/bin/env python
"""
Command line tool for google-diff-match-patch
"""
from diff_match_patch import diff_match_patch
HTMLTEMP = '''\
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Diffs between "%(file1)s" and "%(file2)s"</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<p>%(text)s</p>
</body>
</html>
'''
def main():
from optparse import OptionParser
parser = OptionParser(usage='%prog [options] FILE1 FILE2',
description=__doc__)
parser.add_option("--timeout", default=None, type="float")
parser.add_option("-o", "--outfile", default=None)
parser.add_option("-t", "--outtype", default="html")
(opts, args) = parser.parse_args()
(path1, path2) = args
dmp = diff_match_patch()
if opts.timeout is not None:
dmp.Diff_Timeout = opts.timeout
diffs = dmp.diff_main(file(path1).read(), file(path2).read())
dmp.diff_cleanupSemantic(diffs)
if opts.outtype == "html":
out = HTMLTEMP % dict(text=dmp.diff_prettyHtml(diffs),
file1=path1, file2=path2)
if opts.outfile is None:
print out
else:
outfile = file(opts.outfile, "w")
outfile.write(out)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment