Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·281 lines (248 loc) · 8.31 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·281 lines (248 loc) · 8.31 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
#!/usr/bin/env bash
set -euo pipefail
repo_url="${TOCODE_REPO_URL:-https://github.com/buzzer-re/ToCode.git}"
branch="${TOCODE_BRANCH:-main}"
with_dev=false
with_full=false
tocode_bin_dir=""
script_dir="$(cd "$(dirname "$0")" && pwd)"
if [ -n "${TOCODE_INSTALL_DIR:-}" ]; then
install_dir="$TOCODE_INSTALL_DIR"
elif [ -d "$script_dir/.git" ] && [ -f "$script_dir/pyproject.toml" ]; then
install_dir="$script_dir"
else
install_dir="$HOME/ToCode"
fi
usage() {
cat <<'EOF'
Install ToCode on Linux or macOS.
Usage:
./install.sh [options]
Options:
--dir PATH Clone or update ToCode at PATH. Default: this checkout when run from one, otherwise $HOME/ToCode
--repo URL Git repository URL. Default: https://github.com/buzzer-re/ToCode.git
--branch NAME Branch to install. Default: main
--dev Also install development extras in the local checkout
--full, --all Install all decompiler backends, including the angr fallback
-h, --help Show this help
EOF
}
info() {
printf '==> %s\n' "$1"
}
die() {
printf '\nToCode was not installed: %s\n' "$1" >&2
shift
for hint in "$@"; do
printf ' %s\n' "$hint" >&2
done
exit 1
}
find_python() {
if command -v python3 >/dev/null 2>&1; then
command -v python3
return 0
fi
if command -v python >/dev/null 2>&1; then
command -v python
return 0
fi
return 1
}
ensure_pip() {
python_bin="$1"
if "$python_bin" -m pip --version >/dev/null 2>&1; then
return 0
fi
info "Python pip was not found; attempting to bootstrap it"
"$python_bin" -m ensurepip --upgrade --user >/dev/null 2>&1 || true
if "$python_bin" -m pip --version >/dev/null 2>&1; then
return 0
fi
if command -v apt-get >/dev/null 2>&1; then
info "Installing python3-pip with apt"
sudo apt-get update
sudo apt-get install -y python3-pip
elif command -v brew >/dev/null 2>&1; then
info "Installing Python packaging tools with Homebrew"
brew install python
fi
"$python_bin" -m pip --version >/dev/null 2>&1 \
|| die "Python pip could not be installed automatically" \
"Install uv from https://docs.astral.sh/uv/getting-started/installation/ (recommended)," \
"or install python3-pip with your package manager, then run this installer again."
}
pip_install_user() {
python_bin="$1"
requirement="$2"
info "Installing the tocode command with pip for the current user"
if "$python_bin" -m pip install --user --editable "$requirement"; then
return 0
fi
info "Retrying pip install with --break-system-packages for this user install"
"$python_bin" -m pip install --user --break-system-packages --editable "$requirement" \
|| die "pip could not install ToCode" \
"Check the pip output above, or install uv and run this installer again."
}
python_user_bin_dir() {
python_bin="$1"
"$python_bin" -c 'import site; print(site.getuserbase() + "/bin")' 2>/dev/null \
|| printf '%s\n' "$HOME/.local/bin"
}
path_contains() {
case ":$PATH:" in
*":$1:"*) return 0 ;;
*) return 1 ;;
esac
}
shell_rc_file() {
shell_name="$(basename "${SHELL:-}")"
case "$shell_name" in
zsh) printf '%s\n' "$HOME/.zshrc" ;;
bash) printf '%s\n' "$HOME/.bashrc" ;;
ksh) printf '%s\n' "$HOME/.kshrc" ;;
*) printf '%s\n' "$HOME/.profile" ;;
esac
}
ensure_path() {
bin_dir="$1"
[ -n "$bin_dir" ] || return 0
mkdir -p "$bin_dir"
missing_from_path=false
if ! path_contains "$bin_dir"; then
missing_from_path=true
export PATH="$bin_dir:$PATH"
fi
if [ "$missing_from_path" = false ]; then
return 0
fi
rc_file="$(shell_rc_file)"
marker="# ToCode installer: add user Python/uv tools to PATH"
path_line="export PATH=\"$bin_dir:\$PATH\""
touch "$rc_file"
if ! grep -Fqs "$path_line" "$rc_file"; then
{
printf '\n%s\n' "$marker"
printf '%s\n' "$path_line"
} >>"$rc_file"
info "Added $bin_dir to $rc_file"
info "Open a new shell session, or run: source $rc_file"
fi
}
while [ "$#" -gt 0 ]; do
case "$1" in
--dir)
[ "$#" -ge 2 ] || die "--dir requires a path"
install_dir="$2"
shift 2
;;
--repo)
[ "$#" -ge 2 ] || die "--repo requires a URL"
repo_url="$2"
shift 2
;;
--branch)
[ "$#" -ge 2 ] || die "--branch requires a name"
branch="$2"
shift 2
;;
--dev)
with_dev=true
shift
;;
--full|--all)
with_full=true
shift
;;
-h|--help)
usage
exit 0
;;
*)
die "unknown option: $1"
;;
esac
done
command -v git >/dev/null 2>&1 || die "git was not found on PATH" \
"Install it with your package manager (for example: apt install git, brew install git), then run this installer again."
if [ -d "$install_dir/.git" ]; then
if [ "$install_dir" = "$script_dir" ]; then
info "Installing ToCode from this checkout at $install_dir"
else
info "Updating ToCode at $install_dir"
git -C "$install_dir" fetch origin "$branch"
git -C "$install_dir" checkout "$branch"
git -C "$install_dir" pull --ff-only origin "$branch"
fi
elif [ -e "$install_dir" ]; then
die "$install_dir already exists and is not a ToCode Git checkout" \
"Move or delete it, or pick another location with: ./install.sh --dir PATH"
else
info "Cloning ToCode into $install_dir"
git clone --branch "$branch" "$repo_url" "$install_dir"
fi
# Build the list of optional dependency groups to install. "dev" pulls in the
# test tooling; "full"/angr installs the pure-Python fallback backend.
#
# uv sync populates the project's .venv (used by `uv run` / development), while
# uv tool install builds a separate isolated environment for the `tocode`
# command. Runtime extras (angr) must be passed to BOTH or the installed
# command will not see them. dev is build/test tooling, so it only belongs in
# the project venv, not the tool environment.
uv_extras=""
pip_extras=""
tool_extras=""
if [ "$with_dev" = true ]; then
uv_extras="$uv_extras --extra dev"
pip_extras="dev"
fi
if [ "$with_full" = true ]; then
uv_extras="$uv_extras --extra angr"
pip_extras="${pip_extras:+$pip_extras,}angr"
tool_extras="angr"
fi
if command -v uv >/dev/null 2>&1; then
info "Syncing local project environment with uv"
uv --directory "$install_dir" sync --locked $uv_extras
info "Installing the tocode command with uv"
uv tool install --force --editable "${install_dir}${tool_extras:+[$tool_extras]}"
tocode_bin_dir="$(uv tool dir --bin 2>/dev/null || true)"
if ! command -v tocode >/dev/null 2>&1; then
ensure_path "$tocode_bin_dir"
fi
else
python_bin="$(find_python || true)"
[ -n "$python_bin" ] || die "neither uv nor Python 3.10+ was found on PATH" \
"Install uv from https://docs.astral.sh/uv/getting-started/installation/ (recommended)," \
"or install Python 3.10 or newer, then run this installer again."
"$python_bin" -c 'import sys; raise SystemExit(0 if sys.version_info >= (3, 10) else 1)' \
|| die "$python_bin is older than Python 3.10" \
"Install uv from https://docs.astral.sh/uv/getting-started/installation/ (recommended)," \
"or install Python 3.10 or newer, then run this installer again."
ensure_pip "$python_bin"
if [ -n "$pip_extras" ]; then
pip_install_user "$python_bin" "${install_dir}[$pip_extras]"
else
pip_install_user "$python_bin" "$install_dir"
fi
tocode_bin_dir="$(python_user_bin_dir "$python_bin")"
ensure_path "$tocode_bin_dir"
fi
if command -v tocode >/dev/null 2>&1; then
tocode --help >/dev/null \
|| die "tocode is installed but 'tocode --help' failed" \
"Check the output above, or run this installer again."
info "ToCode is installed"
printf 'Run: tocode <binary> -o <output_dir>\n'
elif [ -n "$tocode_bin_dir" ] && [ -x "$tocode_bin_dir/tocode" ]; then
info "ToCode is installed"
printf 'The tocode command lives in %s, which is not visible in this shell yet.\n' "$tocode_bin_dir"
printf 'Open a new shell session (or run: source %s), then run: tocode <binary> -o <output_dir>\n' "$(shell_rc_file)"
else
if [ -n "$tocode_bin_dir" ]; then
die "the install finished, but the tocode command could not be located" \
"Expected it in $tocode_bin_dir - check that directory and add it to your PATH if it is there."
fi
die "the install finished, but the tocode command could not be located" \
"Add the directory containing the tocode command to your PATH manually."
fi