diff options
author | Thaddeus Crews <repiteo@outlook.com> | 2024-06-24 12:20:55 -0500 |
---|---|---|
committer | Thaddeus Crews <repiteo@outlook.com> | 2024-06-24 15:43:55 -0500 |
commit | e0d363aad8e2204fe8ca4b6937ef2ad74a7b293b (patch) | |
tree | c8300e4864984e9157ba8502ee019c4cd7226128 /misc/scripts | |
parent | c414c2b37d0563456a2fe194b16f8c4aa442e865 (diff) | |
download | redot-cpp-e0d363aad8e2204fe8ca4b6937ef2ad74a7b293b.tar.gz |
Integrate `.pre-commit-config.yaml`
Diffstat (limited to 'misc/scripts')
-rwxr-xr-x | misc/scripts/black_format.sh | 25 | ||||
-rwxr-xr-x | misc/scripts/check_ci_log.py | 6 | ||||
-rwxr-xr-x | misc/scripts/check_get_file_list.py | 6 | ||||
-rwxr-xr-x | misc/scripts/clang_format.sh | 38 | ||||
-rw-r--r-- | misc/scripts/codespell.sh | 5 | ||||
-rwxr-xr-x | misc/scripts/copyright_headers.py | 99 | ||||
-rw-r--r-- | misc/scripts/file_format.py | 46 | ||||
-rwxr-xr-x | misc/scripts/file_format.sh | 41 | ||||
-rw-r--r-- | misc/scripts/header_guards.py | 127 | ||||
-rwxr-xr-x | misc/scripts/header_guards.sh | 60 | ||||
-rw-r--r-- | misc/scripts/mypy.ini | 11 | ||||
-rwxr-xr-x | misc/scripts/mypy_check.sh | 6 |
12 files changed, 230 insertions, 240 deletions
diff --git a/misc/scripts/black_format.sh b/misc/scripts/black_format.sh deleted file mode 100755 index 99343f1..0000000 --- a/misc/scripts/black_format.sh +++ /dev/null @@ -1,25 +0,0 @@ -#!/usr/bin/env bash - -# This script runs black on all Python files in the repo. - -set -uo pipefail - -# Apply black. -echo -e "Formatting Python files..." -PY_FILES=$(git ls-files -- '*SConstruct' '*SCsub' '*.py' ':!:.git/*' ':!:thirdparty/*') -black -l 120 $PY_FILES - -diff=$(git diff --color) - -# If no patch has been generated all is OK, clean up, and exit. -if [ -z "$diff" ] ; then - printf "Files in this commit comply with the black style rules.\n" - exit 0 -fi - -# A patch has been created, notify the user, clean up, and exit. -printf "\n*** The following differences were found between the code " -printf "and the formatting rules:\n\n" -echo "$diff" -printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n" -exit 1 diff --git a/misc/scripts/check_ci_log.py b/misc/scripts/check_ci_log.py index 2c75b83..d024a3e 100755 --- a/misc/scripts/check_ci_log.py +++ b/misc/scripts/check_ci_log.py @@ -9,8 +9,8 @@ if len(sys.argv) < 2: fname = sys.argv[1] -fileread = open(fname.strip(), "r") -file_contents = fileread.read() +with open(fname.strip(), "r", encoding="utf-8") as fileread: + file_contents = fileread.read() # If find "ERROR: AddressSanitizer:", then happens invalid read or write # This is critical bug, so we need to fix this as fast as possible @@ -25,6 +25,8 @@ if ( file_contents.find("Program crashed with signal") != -1 or file_contents.find("Dumping the backtrace") != -1 or file_contents.find("Segmentation fault (core dumped)") != -1 + or file_contents.find("Aborted (core dumped)") != -1 + or file_contents.find("terminate called without an active exception") != -1 ): print("FATAL ERROR: Godot has been crashed.") sys.exit(52) diff --git a/misc/scripts/check_get_file_list.py b/misc/scripts/check_get_file_list.py index d536a7a..33bc6b6 100755 --- a/misc/scripts/check_get_file_list.py +++ b/misc/scripts/check_get_file_list.py @@ -1,12 +1,12 @@ #!/usr/bin/env python -import os, sys - +import os +import sys from pathlib import Path sys.path.insert(1, os.path.join(os.path.dirname(__file__), "..", "..")) -from binding_generator import get_file_list, generate_bindings +from binding_generator import generate_bindings, get_file_list api_filepath = "gdextension/extension_api.json" bits = "64" diff --git a/misc/scripts/clang_format.sh b/misc/scripts/clang_format.sh deleted file mode 100755 index a5c07b9..0000000 --- a/misc/scripts/clang_format.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/usr/bin/env bash - -# This script runs clang-format and fixes copyright headers on all relevant files in the repo. -# This is the primary script responsible for fixing style violations. - -set -uo pipefail - -# Loops through all code files tracked by Git. -git ls-files -- '*.c' '*.h' '*.cpp' '*.hpp' '*.cc' '*.hh' '*.cxx' '*.m' '*.mm' '*.inc' | -while read -r f; do - # Run clang-format. - clang-format --Wno-error=unknown -i "$f" - - # Fix copyright headers, but not all files get them. - if [[ "$f" == *"inc" ]]; then - continue - elif [[ "$f" == *"glsl" ]]; then - continue - elif [[ "$f" == "test/"* ]]; then - continue - fi - - python misc/scripts/copyright_headers.py "$f" -done - -diff=$(git diff --color) - -# If no patch has been generated all is OK, clean up, and exit. -if [ -z "$diff" ] ; then - printf "Files in this commit comply with the clang-tidy style rules.\n" - exit 0 -fi - -# A patch has been created, notify the user, clean up, and exit. -printf "\n*** The following changes have been made to comply with the formatting rules:\n\n" -echo "$diff" -printf "\n*** Please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n" -exit 1 diff --git a/misc/scripts/codespell.sh b/misc/scripts/codespell.sh deleted file mode 100644 index 2822c64..0000000 --- a/misc/scripts/codespell.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -SKIP_LIST="./thirdparty,*.gen.*,*.po,*.pot,package-lock.json,./core/string/locales.h,./DONORS.md,./misc/scripts/codespell.sh" -IGNORE_LIST="ba,childs,curvelinear,expct,fave,findn,gird,inout,lod,nd,numer,ois,ro,statics,te,varn" - -codespell -w -q 3 -S "${SKIP_LIST}" -L "${IGNORE_LIST}" diff --git a/misc/scripts/copyright_headers.py b/misc/scripts/copyright_headers.py index a5e2f0c..2b1201b 100755 --- a/misc/scripts/copyright_headers.py +++ b/misc/scripts/copyright_headers.py @@ -1,6 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import os import sys header = """\ @@ -35,61 +36,61 @@ header = """\ /**************************************************************************/ """ -fname = sys.argv[1] +if len(sys.argv) < 2: + print("Invalid usage of copyright_headers.py, it should be called with a path to one or multiple files.") + sys.exit(1) -# Handle replacing $filename with actual filename and keep alignment -fsingle = fname.strip() -if fsingle.find("/") != -1: - fsingle = fsingle[fsingle.rfind("/") + 1 :] -rep_fl = "$filename" -rep_fi = fsingle -len_fl = len(rep_fl) -len_fi = len(rep_fi) -# Pad with spaces to keep alignment -if len_fi < len_fl: - for x in range(len_fl - len_fi): - rep_fi += " " -elif len_fl < len_fi: - for x in range(len_fi - len_fl): - rep_fl += " " -if header.find(rep_fl) != -1: - text = header.replace(rep_fl, rep_fi) -else: - text = header.replace("$filename", fsingle) -text += "\n" +for f in sys.argv[1:]: + fname = f -# We now have the proper header, so we want to ignore the one in the original file -# and potentially empty lines and badly formatted lines, while keeping comments that -# come after the header, and then keep everything non-header unchanged. -# To do so, we skip empty lines that may be at the top in a first pass. -# In a second pass, we skip all consecutive comment lines starting with "/*", -# then we can append the rest (step 2). + # Handle replacing $filename with actual filename and keep alignment + fsingle = os.path.basename(fname.strip()) + rep_fl = "$filename" + rep_fi = fsingle + len_fl = len(rep_fl) + len_fi = len(rep_fi) + # Pad with spaces to keep alignment + if len_fi < len_fl: + for x in range(len_fl - len_fi): + rep_fi += " " + elif len_fl < len_fi: + for x in range(len_fi - len_fl): + rep_fl += " " + if header.find(rep_fl) != -1: + text = header.replace(rep_fl, rep_fi) + else: + text = header.replace("$filename", fsingle) + text += "\n" -fileread = open(fname.strip(), "r") -line = fileread.readline() -header_done = False + # We now have the proper header, so we want to ignore the one in the original file + # and potentially empty lines and badly formatted lines, while keeping comments that + # come after the header, and then keep everything non-header unchanged. + # To do so, we skip empty lines that may be at the top in a first pass. + # In a second pass, we skip all consecutive comment lines starting with "/*", + # then we can append the rest (step 2). -while line.strip() == "": # Skip empty lines at the top - line = fileread.readline() + with open(fname.strip(), "r", encoding="utf-8") as fileread: + line = fileread.readline() + header_done = False -if line.find("/**********") == -1: # Godot header starts this way - # Maybe starting with a non-Godot comment, abort header magic - header_done = True + while line.strip() == "" and line != "": # Skip empty lines at the top + line = fileread.readline() -while not header_done: # Handle header now - if line.find("/*") != 0: # No more starting with a comment - header_done = True - if line.strip() != "": - text += line - line = fileread.readline() + if line.find("/**********") == -1: # Godot header starts this way + # Maybe starting with a non-Godot comment, abort header magic + header_done = True -while line != "": # Dump everything until EOF - text += line - line = fileread.readline() + while not header_done: # Handle header now + if line.find("/*") != 0: # No more starting with a comment + header_done = True + if line.strip() != "": + text += line + line = fileread.readline() -fileread.close() + while line != "": # Dump everything until EOF + text += line + line = fileread.readline() -# Write -filewrite = open(fname.strip(), "w") -filewrite.write(text) -filewrite.close() + # Write + with open(fname.strip(), "w", encoding="utf-8", newline="\n") as filewrite: + filewrite.write(text) diff --git a/misc/scripts/file_format.py b/misc/scripts/file_format.py new file mode 100644 index 0000000..2ca78be --- /dev/null +++ b/misc/scripts/file_format.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys + +if len(sys.argv) < 2: + print("Invalid usage of file_format.py, it should be called with a path to one or multiple files.") + sys.exit(1) + +BOM = b"\xef\xbb\xbf" + +changed = [] +invalid = [] + +for file in sys.argv[1:]: + try: + with open(file, "rt", encoding="utf-8") as f: + original = f.read() + except UnicodeDecodeError: + invalid.append(file) + continue + + if original == "": + continue + + revamp = "\n".join([line.rstrip("\n\r\t ") for line in original.splitlines(True)]).rstrip("\n") + "\n" + + new_raw = revamp.encode(encoding="utf-8") + if new_raw.startswith(BOM): + new_raw = new_raw[len(BOM) :] + + with open(file, "rb") as f: + old_raw = f.read() + + if old_raw != new_raw: + changed.append(file) + with open(file, "wb") as f: + f.write(new_raw) + +if changed: + for file in changed: + print(f"FIXED: {file}") +if invalid: + for file in invalid: + print(f"REQUIRES MANUAL CHANGES: {file}") + sys.exit(1) diff --git a/misc/scripts/file_format.sh b/misc/scripts/file_format.sh deleted file mode 100755 index 6621af5..0000000 --- a/misc/scripts/file_format.sh +++ /dev/null @@ -1,41 +0,0 @@ -#!/usr/bin/env bash - -# This script ensures proper POSIX text file formatting and a few other things. -# This is supplementary to clang_format.sh and black_format.sh, but should be -# run before them. - -# We need dos2unix and recode. -if [ ! -x "$(command -v dos2unix)" -o ! -x "$(command -v recode)" ]; then - printf "Install 'dos2unix' and 'recode' to use this script.\n" -fi - -set -uo pipefail -IFS=$'\n\t' - -# Loops through all text files tracked by Git. -git grep -zIl '' | -while IFS= read -rd '' f; do - # Ensure that files are UTF-8 formatted. - recode UTF-8 "$f" 2> /dev/null - # Ensure that files have LF line endings and do not contain a BOM. - dos2unix "$f" 2> /dev/null - # Remove trailing space characters and ensures that files end - # with newline characters. -l option handles newlines conveniently. - perl -i -ple 's/\s*$//g' "$f" -done - -diff=$(git diff --color) - -# If no patch has been generated all is OK, clean up, and exit. -if [ -z "$diff" ] ; then - printf "Files in this commit comply with the formatting rules.\n" - rm -f patch.patch - exit 0 -fi - -# A patch has been created, notify the user, clean up, and exit. -printf "\n*** The following differences were found between the code " -printf "and the formatting rules:\n\n" -echo "$diff" -printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n" -exit 1 diff --git a/misc/scripts/header_guards.py b/misc/scripts/header_guards.py new file mode 100644 index 0000000..c99c422 --- /dev/null +++ b/misc/scripts/header_guards.py @@ -0,0 +1,127 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import sys +from pathlib import Path + +if len(sys.argv) < 2: + print("Invalid usage of header_guards.py, it should be called with a path to one or multiple files.") + sys.exit(1) + +HEADER_CHECK_OFFSET = 30 +HEADER_BEGIN_OFFSET = 31 +HEADER_END_OFFSET = -1 + +changed = [] +invalid = [] + +for file in sys.argv[1:]: + with open(file, "rt", encoding="utf-8", newline="\n") as f: + lines = f.readlines() + + if len(lines) <= HEADER_BEGIN_OFFSET: + continue # Most likely a dummy file. + + if lines[HEADER_CHECK_OFFSET].startswith("#import"): + continue # Early catch obj-c file. + + name = f"GODOT_{Path(file).name}".upper().replace(".", "_").replace("-", "_").replace(" ", "_") + + HEADER_CHECK = f"#ifndef {name}\n" + HEADER_BEGIN = f"#define {name}\n" + HEADER_END = f"#endif // {name}\n" + + if ( + lines[HEADER_CHECK_OFFSET] == HEADER_CHECK + and lines[HEADER_BEGIN_OFFSET] == HEADER_BEGIN + and lines[HEADER_END_OFFSET] == HEADER_END + ): + continue + + # Guards might exist but with the wrong names. + if ( + lines[HEADER_CHECK_OFFSET].startswith("#ifndef") + and lines[HEADER_BEGIN_OFFSET].startswith("#define") + and lines[HEADER_END_OFFSET].startswith("#endif") + ): + lines[HEADER_CHECK_OFFSET] = HEADER_CHECK + lines[HEADER_BEGIN_OFFSET] = HEADER_BEGIN + lines[HEADER_END_OFFSET] = HEADER_END + with open(file, "wt", encoding="utf-8", newline="\n") as f: + f.writelines(lines) + changed.append(file) + continue + + header_check = -1 + header_begin = -1 + header_end = -1 + pragma_once = -1 + objc = False + + for idx, line in enumerate(lines): + if not line.startswith("#"): + continue + elif line.startswith("#ifndef") and header_check == -1: + header_check = idx + elif line.startswith("#define") and header_begin == -1: + header_begin = idx + elif line.startswith("#endif") and header_end == -1: + header_end = idx + elif line.startswith("#pragma once"): + pragma_once = idx + break + elif line.startswith("#import"): + objc = True + break + + if objc: + continue + + if pragma_once != -1: + lines.pop(pragma_once) + lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK) + lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN) + lines.append("\n") + lines.append(HEADER_END) + with open(file, "wt", encoding="utf-8", newline="\n") as f: + f.writelines(lines) + changed.append(file) + continue + + if header_check == -1 and header_begin == -1 and header_end == -1: + # Guards simply didn't exist + lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK) + lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN) + lines.append("\n") + lines.append(HEADER_END) + with open(file, "wt", encoding="utf-8", newline="\n") as f: + f.writelines(lines) + changed.append(file) + continue + + if header_check != -1 and header_begin != -1 and header_end != -1: + # All prepends "found", see if we can salvage this. + if header_check == header_begin - 1 and header_begin < header_end: + lines.pop(header_check) + lines.pop(header_begin - 1) + lines.pop(header_end - 2) + if lines[header_end - 3] == "\n": + lines.pop(header_end - 3) + lines.insert(HEADER_CHECK_OFFSET, HEADER_CHECK) + lines.insert(HEADER_BEGIN_OFFSET, HEADER_BEGIN) + lines.append("\n") + lines.append(HEADER_END) + with open(file, "wt", encoding="utf-8", newline="\n") as f: + f.writelines(lines) + changed.append(file) + continue + + invalid.append(file) + +if changed: + for file in changed: + print(f"FIXED: {file}") +if invalid: + for file in invalid: + print(f"REQUIRES MANUAL CHANGES: {file}") + sys.exit(1) diff --git a/misc/scripts/header_guards.sh b/misc/scripts/header_guards.sh deleted file mode 100755 index 7cea339..0000000 --- a/misc/scripts/header_guards.sh +++ /dev/null @@ -1,60 +0,0 @@ -#!/bin/bash - -if [ ! -f "SConstruct" ]; then - echo "Warning: This script is intended to be run from the root of the Godot repository." - echo "Some of the paths checks may not work as intended from a different folder." -fi - -files_invalid_guard="" - -for file in $(find . -name "*.hpp" -print); do - # Skip generated files. - if [[ "$file" == "./gen/"* || "$file" == "./include/gen/"* ]]; then continue; fi - # Skip the test project. - if [[ "$file" == "./test/"* ]]; then continue; fi - - bname=$(basename $file .hpp) - - # NOTE: The "GODOT_CPP_" prefix is already used by the generated - # bindings, so we can't use that. We'll use "GODOT_" instead. - prefix="GODOT_" - - # ^^ is bash builtin for UPPERCASE. - guard="${prefix}${bname^^}_HPP" - - # Replaces guards to use computed name. - # We also add some \n to make sure there's a proper separation. - sed -i $file -e "0,/ifndef/s/#ifndef.*/\n#ifndef $guard/" - sed -i $file -e "0,/define/s/#define.*/#define $guard\n/" - sed -i $file -e "$ s/#endif.*/\n#endif \/\/ $guard/" - # Removes redundant \n added before, if they weren't needed. - sed -i $file -e "/^$/N;/^\n$/D" - - # Check that first ifndef (should be header guard) is at the expected position. - # If not it can mean we have some code before the guard that should be after. - # "31" is the expected line with the copyright header. - first_ifndef=$(grep -n -m 1 "ifndef" $file | sed 's/\([0-9]*\).*/\1/') - if [[ "$first_ifndef" != "31" ]]; then - files_invalid_guard+="$file\n" - fi -done - -if [[ ! -z "$files_invalid_guard" ]]; then - echo -e "The following files were found to have potentially invalid header guard:\n" - echo -e "$files_invalid_guard" -fi - -diff=$(git diff --color) - -# If no diff has been generated all is OK, clean up, and exit. -if [ -z "$diff" ] ; then - printf "Files in this commit comply with the header guards formatting rules.\n" - exit 0 -fi - -# A diff has been created, notify the user, clean up, and exit. -printf "\n*** The following differences were found between the code " -printf "and the header guards formatting rules:\n\n" -echo "$diff" -printf "\n*** Aborting, please fix your commit(s) with 'git commit --amend' or 'git rebase -i <hash>'\n" -exit 1 diff --git a/misc/scripts/mypy.ini b/misc/scripts/mypy.ini deleted file mode 100644 index c1ea695..0000000 --- a/misc/scripts/mypy.ini +++ /dev/null @@ -1,11 +0,0 @@ -[mypy] -ignore_missing_imports = true -disallow_any_generics = True -pretty = True -show_column_numbers = True -warn_redundant_casts = True -warn_return_any = True -warn_unreachable = True - -namespace_packages = True -explicit_package_bases = True diff --git a/misc/scripts/mypy_check.sh b/misc/scripts/mypy_check.sh deleted file mode 100755 index 2a06486..0000000 --- a/misc/scripts/mypy_check.sh +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env bash - -set -uo pipefail - -echo -e "Python: mypy static analysis..." -mypy --config-file=./misc/scripts/mypy.ini . |