From 6abba493b2988f4df8d799b4dd8aff7d30483b83 Mon Sep 17 00:00:00 2001 From: Emanuele Torre Date: Sun, 3 Jan 2021 19:31:44 +0100 Subject: [PATCH] Meta: Rewrite the check-newlines-at-eof script in python The bash version takes around 15 seconds to run; that is way too slow. This python3 version should take less than one second to run. :^) Also, the script will now also check .py files and .txt CMake files. --- Meta/check-newlines-at-eof.py | 62 +++++++++++++++++++++++++++++++++++ Meta/check-newlines-at-eof.sh | 42 ------------------------ Meta/lint-ci.sh | 2 +- 3 files changed, 63 insertions(+), 43 deletions(-) create mode 100755 Meta/check-newlines-at-eof.py delete mode 100755 Meta/check-newlines-at-eof.sh diff --git a/Meta/check-newlines-at-eof.py b/Meta/check-newlines-at-eof.py new file mode 100755 index 00000000000..d49f5e88bb6 --- /dev/null +++ b/Meta/check-newlines-at-eof.py @@ -0,0 +1,62 @@ +#!/bin/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", + ":!:Libraries/LibC/getopt.cpp", + ":!:Libraries/LibCore/puff.h", + ":!:Libraries/LibCore/puff.cpp", + ":!: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) diff --git a/Meta/check-newlines-at-eof.sh b/Meta/check-newlines-at-eof.sh deleted file mode 100755 index 77147e62825..00000000000 --- a/Meta/check-newlines-at-eof.sh +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/bash - -script_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P) -cd "$script_path/.." || exit 1 - -MISSING_NEWLINE_AT_EOF_ERRORS=() -MORE_THAN_ONE_NEWLINE_AT_EOF_ERRORS=() - -while IFS= read -r f; do - [ -s "$f" ] || continue - - if [ "$(tail -n 1 "$f" | wc -l)" != 1 ]; then - MISSING_NEWLINE_AT_EOF_ERRORS+=( "$f" ) - elif [[ "$(tail -n 1 "$f")" =~ ^[[:space:]]*$ ]]; then - MORE_THAN_ONE_NEWLINE_AT_EOF_ERRORS+=( "$f" ) - fi -done < <(git ls-files -- \ - '*.cpp' \ - '*.h' \ - '*.gml' \ - '*.html' \ - '*.js' \ - '*.css' \ - '*.sh' \ - ':!:Base' \ - ':!:Kernel/FileSystem/ext2_fs.h' \ - ':!:Libraries/LibC/getopt.cpp' \ - ':!:Libraries/LibCore/puff.h' \ - ':!:Libraries/LibCore/puff.cpp' \ - ':!:Libraries/LibELF/exec_elf.h' \ -) - -exit_status=0 -if (( ${#MISSING_NEWLINE_AT_EOF_ERRORS[@]} )); then - echo "Files with no newline at the end: ${MISSING_NEWLINE_AT_EOF_ERRORS[*]}" - exit_status=1 -fi -if (( ${#MORE_THAN_ONE_NEWLINE_AT_EOF_ERRORS[@]} )); then - echo "Files that have blank lines at the end: ${MORE_THAN_ONE_NEWLINE_AT_EOF_ERRORS[*]}" - exit_status=1 -fi -exit "$exit_status" diff --git a/Meta/lint-ci.sh b/Meta/lint-ci.sh index 724bcc0f156..68d379d51ca 100755 --- a/Meta/lint-ci.sh +++ b/Meta/lint-ci.sh @@ -16,7 +16,7 @@ set +e for cmd in \ Meta/check-ak-test-files.sh \ Meta/check-debug-flags.sh \ - Meta/check-newlines-at-eof.sh \ + Meta/check-newlines-at-eof.py \ Meta/check-style.sh \ Meta/lint-executable-resources.sh \ Meta/lint-ipc-ids.sh \