gendiff.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env python3
  2. """
  3. Clean up control characters and trailing whitespace in the listed source files
  4. and create a unified diff between them.
  5. If more or less than two files are listed on the command line, the cleanup is
  6. performed on all files, but no diff is created.
  7. The source files are assumed to be terminal output captured by the `script`
  8. command.
  9. The cleaned source files are saved with the .clean extension.
  10. """
  11. import sys,re
  12. from difflib import unified_diff
  13. fns = sys.argv[1:]
  14. translate = {
  15. '\r': None,
  16. '\b': '[BS]',
  17. # chr(4): '', # Ctrl-D, EOT
  18. }
  19. def cleanup_file(fn):
  20. # must use binary mode to prevent conversion of DOS CR into newline
  21. with open(fn,'rb') as fp:
  22. data = fp.read().decode()
  23. def gen_text():
  24. for line in data.split('\n'): # do not use splitlines()
  25. line = line.translate({ord(a):b for a,b in translate.items()})
  26. line = re.sub(r'\s+$','',line) # trailing whitespace
  27. yield line
  28. ret = list(gen_text())
  29. sys.stderr.write(f'Saving cleaned file to {fn}.clean\n')
  30. with open(f'{fn}.clean','w') as fp:
  31. fp.write('\n'.join(ret))
  32. return ret
  33. if len(fns) != 2:
  34. sys.stderr.write(f'{len(fns)} input files. Not generating diff.\n')
  35. cleaned_texts = [cleanup_file(fn) for fn in fns]
  36. if len(fns) == 2:
  37. """
  38. chunk headers have trailing newlines, hence the rstrip()
  39. """
  40. sys.stderr.write('Generating diff\n')
  41. print(
  42. f'diff a/{fns[0]} b/{fns[1]}\n' +
  43. '\n'.join(a.rstrip() for a in unified_diff(*cleaned_texts,fromfile=f'a/{fns[0]}',tofile=f'b/{fns[1]}'))
  44. )