-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgen_package_pyi.sh
More file actions
executable file
·105 lines (91 loc) · 2.79 KB
/
Copy pathgen_package_pyi.sh
File metadata and controls
executable file
·105 lines (91 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env bash
# Regenerate IDE type stubs for three core PyDevices packages into tools/typings/.
#
# Usage:
# ./scripts/gen_package_pyi.sh
# ./scripts/gen_package_pyi.sh --help
#
# Requires the repo-root .venv (mypy / stubgen). Output is committed under
# tools/typings/{displaydev,appdev,multimer}/ for stubPath.
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
PACKAGES=(displaydev appdev multimer)
MODULES=(events keys)
OUT=tools/typings
STUBGEN="${ROOT}/.venv/bin/stubgen"
PYTHON="${ROOT}/.venv/bin/python"
usage() {
cat <<'EOF'
Usage: ./scripts/gen_package_pyi.sh
Regenerate mypy stubgen .pyi trees for displaydev, appdev,
multimer, events, and keys into tools/typings/ (Pylance / pyright stubPath).
(source is gitignored; public API is pygraphics.FrameBuffer).
EOF
}
case "${1:-}" in
--help|-h)
usage
exit 0
;;
"")
;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 1
;;
esac
if [[ ! -x "$STUBGEN" ]]; then
echo "error: missing $STUBGEN — create .venv and pip install mypy" >&2
exit 1
fi
for pkg in "${PACKAGES[@]}"; do
rm -rf "${OUT}/${pkg}"
done
rm -rf "${OUT}/displaysys"
for mod in "${MODULES[@]}"; do
rm -f "${OUT}/${mod}.pyi"
done
HW="$(cd "${ROOT}/../pydevices" 2>/dev/null && pwd || true)"
export PYTHONPATH="${ROOT}/lib/lib:${ROOT}/lib/utils${PYTHONPATH:+:$PYTHONPATH}"
if [[ -n "$HW" ]]; then
export PYTHONPATH="${HW}/lib:${HW}/utils:${HW}/drivers/display:${PYTHONPATH}"
fi
echo "Running stubgen → ${OUT}/ …"
"$STUBGEN" --ignore-errors -o "$OUT" \
-p displaydev -p appdev -p multimer \
-m events -m keys
# Gitignored generated module; public FrameBuffer lives in _framebuf_plus.
# stubgen misses lazy ``multimer.asyncio`` (``__getattr__``); declare it explicitly.
MULTIMER_INIT="${OUT}/multimer/__init__.pyi"
if [[ -f "$MULTIMER_INIT" ]] && ! grep -q '^asyncio:' "$MULTIMER_INIT"; then
"$PYTHON" - "$MULTIMER_INIT" <<'PY'
from pathlib import Path
import sys
path = Path(sys.argv[1])
text = path.read_text(encoding="utf-8")
needle = "\n__all__ = "
if "asyncio: Incomplete" not in text and needle in text:
text = text.replace(
needle,
"\nfrom _typeshed import Incomplete\n\nasyncio: Incomplete\n" + needle,
1,
)
text = "\n".join(
line
for line in text.splitlines()
if line.strip() not in ("# Names in __all__ with no definition:", "# asyncio")
) + ("\n" if text.endswith("\n") else "")
path.write_text(text, encoding="utf-8")
PY
fi
echo
echo "Updated:"
for pkg in "${PACKAGES[@]}"; do
count="$("$PYTHON" -c "from pathlib import Path; print(sum(1 for _ in Path('${OUT}/${pkg}').rglob('*.pyi')))")"
echo " ${OUT}/${pkg}/ (${count} .pyi)"
done
for mod in "${MODULES[@]}"; do
echo " ${OUT}/${mod}.pyi"
done