summaryrefslogtreecommitdiff
path: root/Meta/check-newlines-at-eof.py
blob: 052813b65742475a33a8a52b0f4af1437affb747 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3

import os
import subprocess
import sys

os.chdir(os.path.dirname(__file__) + "/..")

files = subprocess.run(
    [
        "git", "ls-files", "--",
        "*.cpp",
        "*.h",
        "*.gml",
        "*.html",
        "*.js",
        "*.css",
        "*.sh",
        "*.py",
        "CMake*.txt",
        "**/CMake*.txt",
        ":!:Base",
        ":!:Kernel/FileSystem/ext2_fs.h",
        ":!:Userland/Libraries/LibC/getopt.cpp",
        ":!:Userland/Libraries/LibCore/puff.h",
        ":!:Userland/Libraries/LibCore/puff.cpp",
        ":!:Userland/Libraries/LibELF/exec_elf.h"
    ],
    capture_output=True
).stdout.decode().strip('\n').split('\n')

no_newline_at_eof_errors = []
blank_lines_at_eof_errors = []

did_fail = False
for filename in files:
    with open(filename, "r") as f:
        f.seek(0, os.SEEK_END)

        f.seek(f.tell() - 1, os.SEEK_SET)
        if f.read(1) != '\n':
            did_fail = True
            no_newline_at_eof_errors.append(filename)
            continue

        while True:
            f.seek(f.tell() - 2, os.SEEK_SET)
            char = f.read(1)
            if not char.isspace():
                break
            if char == '\n':
                did_fail = True
                blank_lines_at_eof_errors.append(filename)
                break

if no_newline_at_eof_errors:
    print("Files with no newline at the end:", " ".join(no_newline_at_eof_errors))
if blank_lines_at_eof_errors:
    print("Files that have blank lines at the end:", " ".join(blank_lines_at_eof_errors))

if did_fail:
    sys.exit(1)