/Tools/scripts/ndiff.py

http://unladen-swallow.googlecode.com/ · Python · 133 lines · 93 code · 16 blank · 24 comment · 20 complexity · 1d1ec8295ac7480d77b4e9b33b2194de MD5 · raw file

  1. #! /usr/bin/env python
  2. # Module ndiff version 1.7.0
  3. # Released to the public domain 08-Dec-2000,
  4. # by Tim Peters (tim.one@home.com).
  5. # Provided as-is; use at your own risk; no warranty; no promises; enjoy!
  6. # ndiff.py is now simply a front-end to the difflib.ndiff() function.
  7. # Originally, it contained the difflib.SequenceMatcher class as well.
  8. # This completes the raiding of reusable code from this formerly
  9. # self-contained script.
  10. """ndiff [-q] file1 file2
  11. or
  12. ndiff (-r1 | -r2) < ndiff_output > file1_or_file2
  13. Print a human-friendly file difference report to stdout. Both inter-
  14. and intra-line differences are noted. In the second form, recreate file1
  15. (-r1) or file2 (-r2) on stdout, from an ndiff report on stdin.
  16. In the first form, if -q ("quiet") is not specified, the first two lines
  17. of output are
  18. -: file1
  19. +: file2
  20. Each remaining line begins with a two-letter code:
  21. "- " line unique to file1
  22. "+ " line unique to file2
  23. " " line common to both files
  24. "? " line not present in either input file
  25. Lines beginning with "? " attempt to guide the eye to intraline
  26. differences, and were not present in either input file. These lines can be
  27. confusing if the source files contain tab characters.
  28. The first file can be recovered by retaining only lines that begin with
  29. " " or "- ", and deleting those 2-character prefixes; use ndiff with -r1.
  30. The second file can be recovered similarly, but by retaining only " " and
  31. "+ " lines; use ndiff with -r2; or, on Unix, the second file can be
  32. recovered by piping the output through
  33. sed -n '/^[+ ] /s/^..//p'
  34. """
  35. __version__ = 1, 7, 0
  36. import difflib, sys
  37. def fail(msg):
  38. out = sys.stderr.write
  39. out(msg + "\n\n")
  40. out(__doc__)
  41. return 0
  42. # open a file & return the file object; gripe and return 0 if it
  43. # couldn't be opened
  44. def fopen(fname):
  45. try:
  46. return open(fname, 'U')
  47. except IOError, detail:
  48. return fail("couldn't open " + fname + ": " + str(detail))
  49. # open two files & spray the diff to stdout; return false iff a problem
  50. def fcompare(f1name, f2name):
  51. f1 = fopen(f1name)
  52. f2 = fopen(f2name)
  53. if not f1 or not f2:
  54. return 0
  55. a = f1.readlines(); f1.close()
  56. b = f2.readlines(); f2.close()
  57. for line in difflib.ndiff(a, b):
  58. print line,
  59. return 1
  60. # crack args (sys.argv[1:] is normal) & compare;
  61. # return false iff a problem
  62. def main(args):
  63. import getopt
  64. try:
  65. opts, args = getopt.getopt(args, "qr:")
  66. except getopt.error, detail:
  67. return fail(str(detail))
  68. noisy = 1
  69. qseen = rseen = 0
  70. for opt, val in opts:
  71. if opt == "-q":
  72. qseen = 1
  73. noisy = 0
  74. elif opt == "-r":
  75. rseen = 1
  76. whichfile = val
  77. if qseen and rseen:
  78. return fail("can't specify both -q and -r")
  79. if rseen:
  80. if args:
  81. return fail("no args allowed with -r option")
  82. if whichfile in ("1", "2"):
  83. restore(whichfile)
  84. return 1
  85. return fail("-r value must be 1 or 2")
  86. if len(args) != 2:
  87. return fail("need 2 filename args")
  88. f1name, f2name = args
  89. if noisy:
  90. print '-:', f1name
  91. print '+:', f2name
  92. return fcompare(f1name, f2name)
  93. # read ndiff output from stdin, and print file1 (which=='1') or
  94. # file2 (which=='2') to stdout
  95. def restore(which):
  96. restored = difflib.restore(sys.stdin.readlines(), which)
  97. sys.stdout.writelines(restored)
  98. if __name__ == '__main__':
  99. args = sys.argv[1:]
  100. if "-profile" in args:
  101. import profile, pstats
  102. args.remove("-profile")
  103. statf = "ndiff.pro"
  104. profile.run("main(args)", statf)
  105. stats = pstats.Stats(statf)
  106. stats.strip_dirs().sort_stats('time').print_stats()
  107. else:
  108. main(args)