home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 14 / hacker14.iso / programacao / pythonwin / python.exe / DIFF.PY < prev    next >
Encoding:
Python Source  |  2003-06-09  |  1.5 KB  |  42 lines

  1. """ Command line interface to difflib.py providing diffs in three formats:
  2.  
  3. * ndiff:    lists every line and highlights interline changes.
  4. * context:  highlights clusters of changes in a before/after format
  5. * unified:  highlights clusters of changes in an inline format.
  6.  
  7. """
  8.  
  9. import sys, os, time, difflib, optparse
  10.  
  11. usage = "usage: %prog [options] fromfile tofile"
  12. parser = optparse.OptionParser(usage)
  13. parser.add_option("-c", action="store_true", default=False, help='Produce a context format diff (default)')
  14. parser.add_option("-u", action="store_true", default=False, help='Produce a unified format diff')
  15. parser.add_option("-n", action="store_true", default=False, help='Produce a ndiff format diff')
  16. parser.add_option("-l", "--lines", type="int", default=3, help='Set number of context lines (default 3)')
  17. (options, args) = parser.parse_args()
  18.  
  19. if len(args) == 0:
  20.     parser.print_help()
  21.     sys.exit(1)
  22. if len(args) != 2:
  23.     parser.error("need to specify both a fromfile and tofile")
  24.  
  25. n = options.lines
  26. fromfile, tofile = args
  27.  
  28. fromdate = time.ctime(os.stat(fromfile).st_mtime)
  29. todate = time.ctime(os.stat(tofile).st_mtime)
  30. fromlines = open(fromfile).readlines()
  31. tolines = open(tofile).readlines()
  32.  
  33. if options.u:
  34.     diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
  35. elif options.n:
  36.     diff = difflib.ndiff(fromlines, tolines)
  37. else:
  38.     diff = difflib.context_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
  39.  
  40. sys.stdout.writelines(diff)
  41.  
  42.