check-newlines-at-eof.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env python3
  2. import os
  3. import subprocess
  4. import sys
  5. os.chdir(os.path.dirname(__file__) + "/..")
  6. files = subprocess.run(
  7. [
  8. "git", "ls-files", "--",
  9. "*.cpp",
  10. "*.h",
  11. "*.gml",
  12. "*.html",
  13. "*.js",
  14. "*.css",
  15. "*.sh",
  16. "*.py",
  17. "CMake*.txt",
  18. "**/CMake*.txt",
  19. ":!:Base",
  20. ":!:Kernel/FileSystem/ext2_fs.h",
  21. ":!:Userland/Libraries/LibC/getopt.cpp",
  22. ":!:Userland/Libraries/LibCore/puff.h",
  23. ":!:Userland/Libraries/LibCore/puff.cpp",
  24. ":!:Userland/Libraries/LibELF/exec_elf.h"
  25. ],
  26. capture_output=True
  27. ).stdout.decode().strip('\n').split('\n')
  28. no_newline_at_eof_errors = []
  29. blank_lines_at_eof_errors = []
  30. did_fail = False
  31. for filename in files:
  32. with open(filename, "r") as f:
  33. f.seek(0, os.SEEK_END)
  34. f.seek(f.tell() - 1, os.SEEK_SET)
  35. if f.read(1) != '\n':
  36. did_fail = True
  37. no_newline_at_eof_errors.append(filename)
  38. continue
  39. while True:
  40. f.seek(f.tell() - 2, os.SEEK_SET)
  41. char = f.read(1)
  42. if not char.isspace():
  43. break
  44. if char == '\n':
  45. did_fail = True
  46. blank_lines_at_eof_errors.append(filename)
  47. break
  48. if no_newline_at_eof_errors:
  49. print("Files with no newline at the end:", " ".join(no_newline_at_eof_errors))
  50. if blank_lines_at_eof_errors:
  51. print("Files that have blank lines at the end:", " ".join(blank_lines_at_eof_errors))
  52. if did_fail:
  53. sys.exit(1)