mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-08-20 18:03:46 +08:00
sunnypilot v2026.003.000 release
date: 2026-08-19T09:43:43
master commit: ba29a38507
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())
|
||||
Executable
+38
@@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3
|
||||
import argparse
|
||||
import tokenize
|
||||
|
||||
|
||||
# TODO: remove this once https://github.com/astral-sh/ruff/issues/8705 is closed
|
||||
def check_indentation(filename: str, indent_width: int = 2) -> bool:
|
||||
failed = False
|
||||
indent_stack = [0]
|
||||
|
||||
with tokenize.open(filename) as f:
|
||||
tokens = tokenize.generate_tokens(f.readline)
|
||||
for token in tokens:
|
||||
if token.type == tokenize.INDENT:
|
||||
indentation = token.string
|
||||
width = len(indentation)
|
||||
expected = indent_stack[-1] + indent_width
|
||||
|
||||
if indentation != " " * expected:
|
||||
found = "indentation containing tabs" if "\t" in indentation else f"{width} spaces"
|
||||
print(f"{filename}:{token.start[0]}:1: expected {expected} spaces, found {found}")
|
||||
failed = True
|
||||
indent_stack.append(width)
|
||||
elif token.type == tokenize.DEDENT:
|
||||
indent_stack.pop()
|
||||
|
||||
return failed
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description="Check Python block indentation.")
|
||||
parser.add_argument("filenames", nargs="+")
|
||||
args = parser.parse_args()
|
||||
|
||||
failed = False
|
||||
for filename in args.filenames:
|
||||
failed |= check_indentation(filename)
|
||||
raise SystemExit(failed)
|
||||
Executable
+10
@@ -0,0 +1,10 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
FAIL=0
|
||||
|
||||
if grep -n '\(#\|//\)\([[:space:]]*\)NOMERGE' $@; then
|
||||
echo -e "NOMERGE comments found! Remove them before merging\n"
|
||||
FAIL=1
|
||||
fi
|
||||
|
||||
exit $FAIL
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
FAIL=0
|
||||
|
||||
if grep '^#!.*python' $@ | grep -v '#!/usr/bin/env python3$'; then
|
||||
echo -e "Invalid shebang! Must use '#!/usr/bin/env python3'\n"
|
||||
FAIL=1
|
||||
fi
|
||||
|
||||
if grep '^#!.*bash' $@ | grep -v '#!/usr/bin/env bash$'; then
|
||||
echo -e "Invalid shebang! Must use '#!/usr/bin/env bash'"
|
||||
FAIL=1
|
||||
fi
|
||||
|
||||
exit $FAIL
|
||||
+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())
|
||||
Executable
+117
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env bash
|
||||
set -e
|
||||
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
UNDERLINE='\033[4m'
|
||||
BOLD='\033[1m'
|
||||
NC='\033[0m'
|
||||
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
|
||||
ROOT="$DIR/../../"
|
||||
cd $ROOT
|
||||
|
||||
FAILED=0
|
||||
|
||||
function run() {
|
||||
shopt -s extglob
|
||||
case $1 in
|
||||
$SKIP | $RUN ) return 0 ;;
|
||||
esac
|
||||
|
||||
echo -en "$1"
|
||||
|
||||
for ((i=0; i<$((50 - ${#1})); i++)); do
|
||||
echo -n "."
|
||||
done
|
||||
|
||||
shift 1;
|
||||
CMD="$@"
|
||||
|
||||
set +e
|
||||
log="$((eval "$CMD" ) 2>&1)"
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo -e "[${GREEN}✔${NC}]"
|
||||
else
|
||||
echo -e "[${RED}✗${NC}]"
|
||||
echo "$log"
|
||||
FAILED=1
|
||||
fi
|
||||
set -e
|
||||
}
|
||||
|
||||
function run_tests() {
|
||||
ALL_FILES=$1
|
||||
PYTHON_FILES=$2
|
||||
|
||||
run "ruff" ruff check openpilot --quiet
|
||||
run "check_indentation" $DIR/check_indentation.py $PYTHON_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
|
||||
|
||||
if [[ -z "$FAST" ]]; then
|
||||
run "ty" ty check openpilot
|
||||
run "codespell" codespell $ALL_FILES --ignore-words=$ROOT/.codespellignore
|
||||
fi
|
||||
|
||||
return $FAILED
|
||||
}
|
||||
|
||||
function help() {
|
||||
echo "A fast linter"
|
||||
echo ""
|
||||
echo -e "${BOLD}${UNDERLINE}Usage:${NC} op lint [TESTS] [OPTIONS]"
|
||||
echo ""
|
||||
echo -e "${BOLD}${UNDERLINE}Tests:${NC}"
|
||||
echo -e " ${BOLD}ruff${NC}"
|
||||
echo -e " ${BOLD}check_indentation${NC}"
|
||||
echo -e " ${BOLD}ty${NC}"
|
||||
echo -e " ${BOLD}codespell${NC}"
|
||||
echo -e " ${BOLD}check_added_large_files${NC}"
|
||||
echo -e " ${BOLD}check_shebang_scripts_are_executable${NC}"
|
||||
echo ""
|
||||
echo -e "${BOLD}${UNDERLINE}Options:${NC}"
|
||||
echo -e " ${BOLD}-f, --fast${NC}"
|
||||
echo " Skip slow tests"
|
||||
echo -e " ${BOLD}-s, --skip${NC}"
|
||||
echo " Specify tests to skip separated by spaces"
|
||||
echo ""
|
||||
echo -e "${BOLD}${UNDERLINE}Examples:${NC}"
|
||||
echo " op lint ty ruff"
|
||||
echo " Only run the ty and ruff tests"
|
||||
echo ""
|
||||
echo " op lint --skip ty ruff"
|
||||
echo " Skip the ty and ruff tests"
|
||||
echo ""
|
||||
echo " op lint"
|
||||
echo " Run all the tests"
|
||||
}
|
||||
|
||||
SKIP=""
|
||||
RUN=""
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
-f | --fast ) shift 1; FAST="1" ;;
|
||||
-s | --skip ) shift 1; SKIP=" " ;;
|
||||
-h | --help | -help | --h ) help; exit 0 ;;
|
||||
* ) if [[ -n $SKIP ]]; then SKIP+="$1 "; else RUN+="$1 "; fi; shift 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
RUN=$([ -z "$RUN" ] && echo "" || echo "!($(echo $RUN | sed 's/ /|/g'))")
|
||||
SKIP="@($(echo $SKIP | sed 's/ /|/g'))"
|
||||
|
||||
IGNORED_DIRS="^openpilot/third_party/.*"
|
||||
GIT_FILES="$(git ls-files openpilot | grep -vE "$IGNORED_DIRS")"
|
||||
ALL_FILES=""
|
||||
for f in $GIT_FILES; do
|
||||
if [[ -f $f ]]; then
|
||||
ALL_FILES+="$f"$'\n'
|
||||
fi
|
||||
done
|
||||
PYTHON_FILES=$(echo "$ALL_FILES" | grep --color=never '.py$' || true)
|
||||
|
||||
run_tests "$ALL_FILES" "$PYTHON_FILES"
|
||||
Reference in New Issue
Block a user