forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend-python-format
More file actions
executable file
·63 lines (55 loc) · 1.77 KB
/
Copy pathbackend-python-format
File metadata and controls
executable file
·63 lines (55 loc) · 1.77 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
#!/usr/bin/env bash
set -euo pipefail
# This is the single backend Python formatter authority for hooks and CI.
# An exact version keeps local auto-formatting and CI checks convergent.
readonly BLACK_VERSION="26.5.1"
usage() {
cat >&2 <<'EOF'
Usage: scripts/backend-python-format --write|--check <backend Python files...>
scripts/backend-python-format --version
EOF
}
if [ "${1:-}" = "--version" ]; then
printf '%s\n' "$BLACK_VERSION"
exit 0
fi
mode="${1:-}"
case "$mode" in
--write)
black_args=(--line-length 120 --skip-string-normalization)
;;
--check)
black_args=(--check --line-length 120 --skip-string-normalization)
;;
*)
usage
exit 2
;;
esac
shift
# Empty changed-file selections are a normal no-op in hooks and CI.
if [ "$#" -eq 0 ]; then
exit 0
fi
black_version() {
local output
output="$("$@" --version 2>/dev/null || true)"
if [[ "$output" =~ ^black,\ (version\ )?([^[:space:]]+) ]]; then
printf '%s\n' "${BASH_REMATCH[2]}"
fi
}
formatter=()
if command -v black >/dev/null 2>&1 && [ "$(black_version black)" = "$BLACK_VERSION" ]; then
formatter=(black)
elif command -v python3 >/dev/null 2>&1 && [ "$(black_version python3 -m black)" = "$BLACK_VERSION" ]; then
formatter=(python3 -m black)
elif command -v uvx >/dev/null 2>&1; then
# uvx reuses its cache, so a missing or stale global Black fixes itself once
# without changing the developer's global environment.
formatter=(uvx --from "black==$BLACK_VERSION" black)
else
echo "Backend formatting needs Black $BLACK_VERSION, but the installed tool is missing or stale." >&2
echo "Install uv from https://docs.astral.sh/uv/getting-started/installation/ and retry; no global Black install is required." >&2
exit 1
fi
exec "${formatter[@]}" "${black_args[@]}" "$@"