mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-07-23 18:42:10 +08:00
rm pre-commit-hooks (#38382)
* rm pre-commit-hooks * rm test for the tests * lil more
This commit is contained in:
Executable
+47
@@ -0,0 +1,47 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import math
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
|
||||
def lfs_files(filenames: list[str]) -> set[str]:
|
||||
if not filenames:
|
||||
return set()
|
||||
|
||||
result = subprocess.run(
|
||||
("git", "check-attr", "filter", "-z", "--stdin"),
|
||||
input="\0".join(filenames),
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
fields = result.stdout.rstrip("\0").split("\0") if result.stdout else []
|
||||
return {fields[i] for i in range(0, len(fields), 3) if fields[i + 2] == "lfs"}
|
||||
|
||||
|
||||
def check_added_large_files(filenames: list[str], max_kb: int) -> int:
|
||||
failed = False
|
||||
ignored = lfs_files(filenames)
|
||||
for filename in filenames:
|
||||
if filename in ignored:
|
||||
continue
|
||||
|
||||
size_kb = math.ceil(os.stat(filename).st_size / 1024)
|
||||
if size_kb > max_kb:
|
||||
print(f"{filename} ({size_kb} KB) exceeds {max_kb} KB.")
|
||||
failed = True
|
||||
|
||||
return int(failed)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="Check that tracked files do not exceed a size limit.")
|
||||
parser.add_argument("filenames", nargs="*")
|
||||
parser.add_argument("--maxkb", type=int, default=500, help="maximum allowable size in KiB")
|
||||
args = parser.parse_args()
|
||||
return check_added_large_files(args.filenames, args.maxkb)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
def staged_modes(filenames: list[str]) -> list[tuple[str, str]]:
|
||||
if not filenames:
|
||||
return []
|
||||
|
||||
result = subprocess.run(
|
||||
("git", "ls-files", "-z", "--stage", "--", *filenames),
|
||||
check=True,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
)
|
||||
entries = result.stdout.rstrip("\0").split("\0") if result.stdout else []
|
||||
return [(entry.split(" ", 1)[0], entry.split("\t", 1)[1]) for entry in entries]
|
||||
|
||||
|
||||
def has_shebang(filename: str) -> bool:
|
||||
with open(filename, "rb") as f:
|
||||
return f.read(2) == b"#!"
|
||||
|
||||
|
||||
def check_shebang_scripts_are_executable(filenames: list[str]) -> int:
|
||||
failed = False
|
||||
for mode, filename in staged_modes(filenames):
|
||||
if mode != "100755" and has_shebang(filename):
|
||||
quoted = shlex.quote(filename)
|
||||
print("\n".join((
|
||||
f"{filename}: has a shebang but is not marked executable!",
|
||||
f" If it is supposed to be executable, try: `chmod +x {quoted}`",
|
||||
" If it is not supposed to be executable, double-check its shebang is wanted.\n",
|
||||
)), file=sys.stderr)
|
||||
failed = True
|
||||
|
||||
return int(failed)
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="Check that tracked files with shebangs are executable.")
|
||||
parser.add_argument("filenames", nargs="*")
|
||||
args = parser.parse_args()
|
||||
return check_shebang_scripts_are_executable(args.filenames)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
@@ -46,8 +46,8 @@ function run_tests() {
|
||||
PYTHON_FILES=$2
|
||||
|
||||
run "ruff" ruff check openpilot --quiet
|
||||
run "check_added_large_files" python3 -m pre_commit_hooks.check_added_large_files --enforce-all $ALL_FILES --maxkb=120
|
||||
run "check_shebang_scripts_are_executable" python3 -m pre_commit_hooks.check_shebang_scripts_are_executable $ALL_FILES
|
||||
run "check_added_large_files" $DIR/check_added_large_files.py --maxkb=120 $ALL_FILES
|
||||
run "check_shebang_scripts_are_executable" $DIR/check_shebang_scripts_are_executable.py $ALL_FILES
|
||||
run "check_shebang_format" $DIR/check_shebang_format.sh $ALL_FILES
|
||||
run "check_nomerge_comments" $DIR/check_nomerge_comments.sh $ALL_FILES
|
||||
|
||||
|
||||
Reference in New Issue
Block a user