-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·171 lines (152 loc) · 4.19 KB
/
Copy pathsetup.sh
File metadata and controls
executable file
·171 lines (152 loc) · 4.19 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env bash
setup_script_path() {
if [ -n "${ZSH_VERSION:-}" ]; then
printf '%s\n' "${(%):-%x}"
return
fi
if [ -n "${BASH_VERSION:-}" ]; then
printf '%s\n' "${BASH_SOURCE[0]}"
return
fi
printf '%s\n' "$0"
}
if ! (return 0 2>/dev/null); then
printf '[setup] error: run this script via source, not as an executable\n' >&2
printf '[setup] source "%s"\n' "$(setup_script_path)" >&2
exit 1
fi
ROOT_DIR="$(cd "$(dirname "$(setup_script_path)")" && pwd)"
VENV_DIR="${VENV_DIR:-$ROOT_DIR/.venv}"
PYTHON_BIN="${PYTHON_BIN:-python3}"
INSTALL_SYSTEM_DEPS="${INSTALL_SYSTEM_DEPS:-auto}"
DISTRO_FAMILY=""
log() {
printf '[setup] %s\n' "$1"
}
warn() {
printf '[setup] warning: %s\n' "$1" >&2
}
die() {
printf '[setup] error: %s\n' "$1" >&2
return 1
}
has_command() {
command -v "$1" >/dev/null 2>&1
}
detect_distro_family() {
if [ -z "${DISTRO_FAMILY:-}" ] && [ -f /etc/os-release ]; then
# shellcheck disable=SC1091
. /etc/os-release
local distro_id="${ID:-}"
local distro_like="${ID_LIKE:-}"
case " $distro_id $distro_like " in
*" debian "*|*" ubuntu "*) DISTRO_FAMILY="debian" ;;
*" fedora "*|*" rhel "*|*" rocky "*|*" almalinux "*|*" centos "*) DISTRO_FAMILY="fedora" ;;
*" arch "*|*" archlinux "*|*" manjaro "*) DISTRO_FAMILY="arch" ;;
*) DISTRO_FAMILY="unknown" ;;
esac
fi
printf '%s\n' "${DISTRO_FAMILY:-unknown}"
}
run_privileged() {
if [ "$(id -u)" -eq 0 ]; then
"$@"
return
fi
if has_command sudo; then
sudo "$@"
return
fi
die "sudo is required to install system packages"
}
cleanup_project_artifacts() {
log "Removing build and cache artifacts"
find "$ROOT_DIR" \
\( -path "$ROOT_DIR/.git" -o -path "$VENV_DIR" \) -prune -o \
\( -type d \( -name "__pycache__" -o -name ".pytest_cache" -o -name ".mypy_cache" -o -name ".ruff_cache" -o -name "*.egg-info" \) -exec rm -rf {} + \)
rm -rf "$ROOT_DIR/build" "$ROOT_DIR/dist"
}
install_system_deps() {
if [ "$INSTALL_SYSTEM_DEPS" = "0" ] || [ "$INSTALL_SYSTEM_DEPS" = "false" ]; then
log "Skipping system packages by request"
return
fi
local family
family="$(detect_distro_family)"
log "Installing system packages"
case "$family" in
debian)
run_privileged apt-get update
run_privileged apt-get install -y \
ca-certificates \
python3 \
python3-venv
;;
fedora)
run_privileged dnf install -y \
ca-certificates \
python3
;;
arch)
run_privileged pacman -Sy --noconfirm \
ca-certificates \
python
;;
*)
if [ "$INSTALL_SYSTEM_DEPS" = "auto" ]; then
warn "Unsupported distro for automatic package install: ${ID:-unknown}"
return
fi
die "Unsupported distro for automatic package install: ${ID:-unknown}"
;;
esac
}
ensure_python() {
has_command "$PYTHON_BIN" || die "Python interpreter not found: $PYTHON_BIN"
"$PYTHON_BIN" - <<'PY'
import sys
raise SystemExit(0 if sys.version_info >= (3, 11) else 1)
PY
}
ensure_venv() {
if [ ! -d "$VENV_DIR" ]; then
log "Creating virtual environment at $VENV_DIR"
"$PYTHON_BIN" -m venv "$VENV_DIR"
else
log "Using existing virtual environment at $VENV_DIR"
fi
}
install_project() {
log "Upgrading packaging tools"
PIP_NO_CACHE_DIR=1 "$VENV_DIR/bin/python" -m pip install --upgrade pip setuptools wheel
log "Installing project in editable mode"
PIP_NO_CACHE_DIR=1 "$VENV_DIR/bin/python" -m pip install -e "$ROOT_DIR" --no-build-isolation
}
verify_installation() {
log "Verifying installed entrypoint"
"$VENV_DIR/bin/python" -c "from bootstrap import build_container; build_container()" >/dev/null
}
activate_venv() {
if [ ! -f "$VENV_DIR/bin/activate" ]; then
die "virtual environment activation script not found: $VENV_DIR/bin/activate"
fi
# shellcheck disable=SC1090
. "$VENV_DIR/bin/activate"
hash -r 2>/dev/null || true
log "Virtual environment is active: $VENV_DIR"
}
setup_pipeline() {
set -euo pipefail
cd "$ROOT_DIR"
cleanup_project_artifacts
install_system_deps
ensure_python
ensure_venv
install_project
verify_installation
}
main() {
(set -euo pipefail; setup_pipeline "$@")
activate_venv
}
main "$@"