forked from HKUDS/nanobot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·283 lines (245 loc) · 7.97 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·283 lines (245 loc) · 7.97 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#!/bin/sh
set -eu
package="nanobot-ai"
main_source="https://github.com/HKUDS/nanobot/archive/refs/heads/main.zip"
install_target="$package"
install_source="PyPI"
dry_run="0"
nanobot_runner=""
nanobot_python=""
info() {
printf '%s\n' "$*"
}
fail() {
printf 'Error: %s\n' "$*" >&2
exit 1
}
install_failure_hint() {
printf '%s\n' "Error: could not install nanobot from $install_source." >&2
printf '%s\n' "If pip mentioned externally-managed-environment, use uv, pipx, or a virtual environment instead of system pip." >&2
printf '%s\n' "You can also run manually:" >&2
printf ' %s\n' "uv tool install --force --upgrade $install_target" >&2
printf ' %s\n' "$python_bin -m venv ~/.nanobot/venv" >&2
printf ' %s\n' "~/.nanobot/venv/bin/python -m pip install --upgrade $install_target" >&2
printf '%s\n' "Then start setup with:" >&2
printf ' %s\n' "nanobot onboard --wizard" >&2
exit 1
}
usage() {
cat <<'EOF'
Usage: install.sh [--dev] [--dry-run]
By default this installs or upgrades nanobot-ai from PyPI.
Use --dev to install from the current main branch on GitHub.
Use --dry-run to print what would happen without installing or starting the wizard.
EOF
}
find_python() {
for candidate in python3 python; do
if command -v "$candidate" >/dev/null 2>&1; then
if "$candidate" - <<'PY' >/dev/null 2>&1
import sys
raise SystemExit(0 if sys.version_info >= (3, 11) else 1)
PY
then
printf '%s\n' "$candidate"
return 0
fi
fi
done
return 1
}
python_is_virtual_env() {
"$python_bin" - <<'PY'
import sys
raise SystemExit(0 if sys.prefix != sys.base_prefix else 1)
PY
}
ensure_pip() {
target_python="$1"
if "$target_python" -m pip --version >/dev/null 2>&1; then
return 0
fi
info "pip was not found for $target_python. Trying ensurepip..."
"$target_python" -m ensurepip --upgrade >/dev/null 2>&1
}
run_nanobot() {
case "$nanobot_runner" in
uv)
uv tool run --from "$install_target" nanobot "$@"
;;
pipx)
pipx run --spec "$install_target" nanobot "$@"
;;
python)
"$nanobot_python" -m nanobot "$@"
;;
*)
fail "nanobot was installed, but no runner was configured"
;;
esac
}
nanobot_try_command() {
case "$nanobot_runner" in
uv)
printf '%s\n' "uv tool run --from $install_target nanobot"
;;
pipx)
printf '%s\n' "pipx run --spec $install_target nanobot"
;;
python)
printf '%s\n' "$nanobot_python -m nanobot"
;;
esac
}
install_with_active_python() {
info "Detected an active virtual environment. Installing into it..."
ensure_pip "$python_bin" || return 1
"$python_bin" -m pip install --upgrade "$install_target" || return 1
nanobot_runner="python"
nanobot_python="$python_bin"
}
install_with_uv() {
info "Installing or upgrading nanobot from $install_source with uv tool..."
uv tool install --python "$python_bin" --force --upgrade "$install_target" || return 1
nanobot_runner="uv"
}
install_with_pipx() {
info "Installing or upgrading nanobot from $install_source with pipx..."
pipx install --python "$python_bin" --force "$install_target" || return 1
nanobot_runner="pipx"
}
write_managed_wrapper() {
bin_dir="${NANOBOT_BIN_DIR:-$HOME/.local/bin}"
wrapper="$bin_dir/nanobot"
mkdir -p "$bin_dir" || return 0
if [ -e "$wrapper" ] && ! grep -q "Generated by nanobot installer" "$wrapper" 2>/dev/null; then
info "Not updating $wrapper because it already exists."
return 0
fi
cat > "$wrapper" <<EOF
#!/bin/sh
# Generated by nanobot installer.
exec "$nanobot_python" -m nanobot "\$@"
EOF
chmod +x "$wrapper" || return 0
if ! command -v nanobot >/dev/null 2>&1; then
info "Installed a nanobot launcher at $wrapper."
info "Add $bin_dir to PATH to run nanobot directly."
fi
}
install_with_managed_venv() {
[ -n "${HOME:-}" ] || fail "HOME is not set; cannot create a managed virtual environment"
venv_dir="${NANOBOT_VENV:-$HOME/.nanobot/venv}"
venv_python="$venv_dir/bin/python"
if [ ! -x "$venv_python" ]; then
info "Creating a dedicated virtual environment at $venv_dir..."
mkdir -p "$(dirname "$venv_dir")"
"$python_bin" -m venv "$venv_dir" || return 1
fi
"$venv_python" - <<'PY' >/dev/null 2>&1 || fail "The managed venv uses Python older than 3.11. Remove it or set NANOBOT_VENV to a new path."
import sys
raise SystemExit(0 if sys.version_info >= (3, 11) else 1)
PY
info "Installing or upgrading nanobot from $install_source in $venv_dir..."
ensure_pip "$venv_python" || return 1
"$venv_python" -m pip install --upgrade "$install_target" || return 1
nanobot_runner="python"
nanobot_python="$venv_python"
write_managed_wrapper
}
while [ "$#" -gt 0 ]; do
case "$1" in
--dev)
install_target="$main_source"
install_source="GitHub main"
;;
--dry-run)
dry_run="1"
;;
-h|--help)
usage
exit 0
;;
*)
fail "Unknown option: $1"
;;
esac
shift
done
python_bin="${PYTHON:-}"
if [ -n "$python_bin" ]; then
command -v "$python_bin" >/dev/null 2>&1 || fail "PYTHON=$python_bin was not found"
"$python_bin" - <<'PY' >/dev/null 2>&1 || fail "nanobot requires Python 3.11 or newer"
import sys
raise SystemExit(0 if sys.version_info >= (3, 11) else 1)
PY
else
python_bin="$(find_python)" || fail "Python 3.11 or newer was not found. Install Python first, then rerun this command."
fi
info "Using Python: $("$python_bin" --version 2>&1)"
if [ "$dry_run" = "1" ]; then
info "Dry run: would install or upgrade nanobot from $install_source."
if python_is_virtual_env; then
info "Dry run: active virtual environment detected; would run: $python_bin -m pip install --upgrade $install_target"
info "Dry run: would run nanobot as: $python_bin -m nanobot"
elif command -v uv >/dev/null 2>&1; then
info "Dry run: would run: uv tool install --python $python_bin --force --upgrade $install_target"
info "Dry run: would run nanobot as: uv tool run --from $install_target nanobot"
elif command -v pipx >/dev/null 2>&1; then
info "Dry run: would run: pipx install --python $python_bin --force $install_target"
info "Dry run: would run nanobot as: pipx run --spec $install_target nanobot"
else
venv_dir="${NANOBOT_VENV:-$HOME/.nanobot/venv}"
info "Dry run: would create or reuse a dedicated virtual environment: $venv_dir"
info "Dry run: would run: $venv_dir/bin/python -m pip install --upgrade $install_target"
info "Dry run: would run nanobot as: $venv_dir/bin/python -m nanobot"
fi
if [ "${NANOBOT_SKIP_WIZARD:-}" = "1" ]; then
info "Dry run: would skip setup wizard because NANOBOT_SKIP_WIZARD=1."
else
info "Dry run: would run the setup wizard."
fi
info "Dry run: no changes made."
exit 0
fi
if python_is_virtual_env; then
install_with_active_python || install_failure_hint
else
installed="0"
if command -v uv >/dev/null 2>&1; then
if install_with_uv; then
installed="1"
else
info "uv tool install failed. Trying the next isolated install method..."
fi
fi
if [ "$installed" != "1" ] && command -v pipx >/dev/null 2>&1; then
if install_with_pipx; then
installed="1"
else
info "pipx install failed. Trying the managed virtual environment..."
fi
fi
if [ "$installed" != "1" ]; then
info "Using a dedicated virtual environment to avoid system pip."
install_with_managed_venv || install_failure_hint
fi
fi
info "Installed nanobot:"
run_nanobot --version
if [ "${NANOBOT_SKIP_WIZARD:-}" = "1" ]; then
info "Skipping setup wizard because NANOBOT_SKIP_WIZARD=1."
info "Run this later: $(nanobot_try_command) onboard --wizard"
exit 0
fi
if [ -t 0 ]; then
info "Starting setup wizard..."
run_nanobot onboard --wizard
elif : 2>/dev/null < /dev/tty; then
info "Starting setup wizard..."
run_nanobot onboard --wizard < /dev/tty
else
info "Skipping setup wizard because no interactive terminal is available."
info "Run this later: $(nanobot_try_command) onboard --wizard"
fi
info "Done. Try: $(nanobot_try_command) agent -m \"Hello!\""