pydiffer.py.in 998 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!@PYTHON@
  2. import os
  3. import sys
  4. import difflib
  5. import filecmp
  6. def getdiff(old, new):
  7. diff = []
  8. with open(old) as a:
  9. with open(new) as b:
  10. for l in difflib.unified_diff(a.read().splitlines(), b.read().splitlines()):
  11. diff.append(l)
  12. return diff
  13. def dc_getdiff(dc, old, new):
  14. diff = []
  15. for f in dc.left_only:
  16. diff.append("Only in {}: {}".format(old, f))
  17. for f in dc.right_only:
  18. diff.append("Only in {}: {}".format(new, f))
  19. for f in dc.diff_files:
  20. r = getdiff(os.path.join(old, f), os.path.join(new, f))
  21. diff.extend(r)
  22. for dn, dc in list(dc.subdirs.items()):
  23. r = dc_getdiff(dc, os.path.join(old, dn), os.path.join(new, dn))
  24. diff.extend(r)
  25. return diff
  26. def dcdiff(old, new):
  27. dc = filecmp.dircmp(old, new)
  28. diff = dc_getdiff(dc, old, new)
  29. return diff
  30. def main():
  31. for l in dcdiff(sys.argv[1], sys.argv[2]):
  32. print(l)
  33. if __name__ == '__main__':
  34. main()