forked from mtusi2026/mtusi2026lab3
feat: lab3 init
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Offline lint for this small Python project: syntax, whitespace, unused imports."""
|
||||
|
||||
import ast
|
||||
from pathlib import Path
|
||||
import sys
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
FILES = [ROOT / "app/main.py", ROOT / "scripts/migrate.py", ROOT / "scripts/lint.py"]
|
||||
|
||||
|
||||
def check(path):
|
||||
source = path.read_text()
|
||||
issues = []
|
||||
try:
|
||||
tree = ast.parse(source, filename=str(path))
|
||||
except SyntaxError as error:
|
||||
return [f"{path}:{error.lineno}: {error.msg}"]
|
||||
used = {node.id for node in ast.walk(tree) if isinstance(node, ast.Name)}
|
||||
for node in ast.walk(tree):
|
||||
if isinstance(node, ast.Import):
|
||||
imports = [(item.asname or item.name.split(".")[0], node.lineno) for item in node.names]
|
||||
elif isinstance(node, ast.ImportFrom):
|
||||
imports = [(item.asname or item.name, node.lineno) for item in node.names]
|
||||
else:
|
||||
continue
|
||||
for name, line in imports:
|
||||
if name not in used:
|
||||
issues.append(f"{path}:{line}: unused import {name}")
|
||||
for number, line in enumerate(source.splitlines(), 1):
|
||||
if line.rstrip() != line:
|
||||
issues.append(f"{path}:{number}: trailing whitespace")
|
||||
if "\t" in line:
|
||||
issues.append(f"{path}:{number}: tab character")
|
||||
return issues
|
||||
|
||||
|
||||
def main():
|
||||
issues = [issue for path in FILES for issue in check(path)]
|
||||
if issues:
|
||||
print("\n".join(issues))
|
||||
return 1
|
||||
print(f"PASS: lint ({len(FILES)} Python files)")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -0,0 +1,16 @@
|
||||
"""Run schema changes once before starting API replicas."""
|
||||
|
||||
from app.main import migrate, pool
|
||||
|
||||
|
||||
def main():
|
||||
pool.open()
|
||||
try:
|
||||
pool.wait(timeout=20)
|
||||
migrate()
|
||||
finally:
|
||||
pool.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --env-file .env restart postgres redis api lb >/dev/null
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
*[!a-fA-F0-9]*|'') echo "Invalid instance id" >&2; exit 2 ;;
|
||||
esac
|
||||
service=$(docker inspect --format '{{ index .Config.Labels "com.docker.compose.service" }}' "$TARGET_INSTANCE_ID")
|
||||
if [ "$service" != api ]; then
|
||||
echo "Target is not a Compose API replica" >&2
|
||||
exit 2
|
||||
fi
|
||||
docker stop -t 1 "$TARGET_INSTANCE_ID" >/dev/null
|
||||
Reference in New Issue
Block a user