Skip to content

Commit

Permalink
feat: support python virtualenv (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
sigoden authored Oct 21, 2024
1 parent e098f7f commit d499954
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ functions.json
/tools/code_interpreter.*
/.env
__pycache__
/venv
/.venv
node_modules
/package.json
/package-lock.json
Expand Down
46 changes: 42 additions & 4 deletions Argcfile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ set -e

BIN_DIR=bin
TMP_DIR="cache/tmp"
VENV_DIR=".venv"

LANG_CMDS=( \
"sh:bash" \
Expand Down Expand Up @@ -116,7 +117,13 @@ build-bin@tool() {
_build_win_shim tool $lang > "$bin_file"
else
bin_file="$BIN_DIR/$basename"
ln -s -f "$PWD/scripts/run-tool.$lang" "$bin_file"
if [[ "$lang" == "py" && -d "$VENV_DIR" ]]; then
rm -rf "$bin_file"
_build_py_shim tool $lang > "$bin_file"
chmod +x "$bin_file"
else
ln -s -f "$PWD/scripts/run-tool.$lang" "$bin_file"
fi
fi
echo "Build bin/$basename"
else
Expand Down Expand Up @@ -229,7 +236,13 @@ build-bin@agent() {
_build_win_shim agent $lang > "$bin_file"
else
bin_file="$BIN_DIR/$name"
ln -s -f "$PWD/scripts/run-agent.$lang" "$bin_file"
if [[ "$lang" == "py" && -d "$VENV_DIR" ]]; then
rm -rf "$bin_file"
_build_py_shim tool $lang > "$bin_file"
chmod +x "$bin_file"
else
ln -s -f "$PWD/scripts/run-agent.$lang" "$bin_file"
fi
fi
echo "Build bin/$name"
tool_names_file="$agent_dir/tools.txt"
Expand Down Expand Up @@ -540,7 +553,11 @@ _build_win_shim() {
if [[ "$lang" == "sh" ]]; then
run="\"$(argc --argc-shell-path)\" --noprofile --norc"
else
run="\"$(_normalize_path "$(which $cmd)")\""
if [[ "$cmd" == "python" && -d "$VENV_DIR" ]]; then
run="call \"$(_normalize_path "$PWD/$VENV_DIR/Scripts/activate.bat")\" && python"
else
run="\"$(_normalize_path "$(which $cmd)")\""
fi
fi
cat <<-EOF
@echo off
Expand All @@ -554,12 +571,27 @@ $run "%script_dir%scripts\run-$kind.$lang" "%script_name%" %*
EOF
}
_build_py_shim() {
kind="$1"
lang="$2"
cat <<-'EOF' | sed -e "s|__ROOT_DIR__|$PWD|g" -e "s|__VENV_DIR__|$VENV_DIR|g" -e "s/__KIND__/$kind/g"
#!/usr/bin/env bash
set -e
if [[ -d "__ROOT_DIR__/__VENV_DIR__/bin/activate" ]]; then
source "__ROOT_DIR__/__VENV_DIR__/bin/activate"
fi
python "__ROOT_DIR__/scripts/run-__KIND__.py" "$(basename "$0")" "$@"
EOF
}
_link_tool() {
from="$1"
to="$2.${1##*.}"
rm -rf tools/$to
if _is_win; then
(cd tools && cmd <<< "mklink $to $from" > /dev/null)
(cd tools && cp -f $from $to)
else
(cd tools && ln -s $from $to)
fi
Expand Down Expand Up @@ -601,6 +633,12 @@ _is_win() {
fi
}
_argc_before() {
if [[ -d ".venv/bin/activate" ]]; then
source .venv/bin/activate
fi
}
_choice_tool() {
for item in "${LANG_CMDS[@]}"; do
lang="${item%:*}"
Expand Down

0 comments on commit d499954

Please sign in to comment.