feat: support python virtualenv (#116)

This commit is contained in:
sigoden
2024-10-21 08:01:27 +08:00
committed by GitHub
parent e098f7f43a
commit d499954dbb
2 changed files with 43 additions and 5 deletions
+1 -1
View File
@@ -9,7 +9,7 @@ functions.json
/tools/code_interpreter.* /tools/code_interpreter.*
/.env /.env
__pycache__ __pycache__
/venv /.venv
node_modules node_modules
/package.json /package.json
/package-lock.json /package-lock.json
+42 -4
View File
@@ -3,6 +3,7 @@ set -e
BIN_DIR=bin BIN_DIR=bin
TMP_DIR="cache/tmp" TMP_DIR="cache/tmp"
VENV_DIR=".venv"
LANG_CMDS=( \ LANG_CMDS=( \
"sh:bash" \ "sh:bash" \
@@ -116,7 +117,13 @@ build-bin@tool() {
_build_win_shim tool $lang > "$bin_file" _build_win_shim tool $lang > "$bin_file"
else else
bin_file="$BIN_DIR/$basename" 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 fi
echo "Build bin/$basename" echo "Build bin/$basename"
else else
@@ -229,7 +236,13 @@ build-bin@agent() {
_build_win_shim agent $lang > "$bin_file" _build_win_shim agent $lang > "$bin_file"
else else
bin_file="$BIN_DIR/$name" 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 fi
echo "Build bin/$name" echo "Build bin/$name"
tool_names_file="$agent_dir/tools.txt" tool_names_file="$agent_dir/tools.txt"
@@ -540,7 +553,11 @@ _build_win_shim() {
if [[ "$lang" == "sh" ]]; then if [[ "$lang" == "sh" ]]; then
run="\"$(argc --argc-shell-path)\" --noprofile --norc" run="\"$(argc --argc-shell-path)\" --noprofile --norc"
else 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 fi
cat <<-EOF cat <<-EOF
@echo off @echo off
@@ -554,12 +571,27 @@ $run "%script_dir%scripts\run-$kind.$lang" "%script_name%" %*
EOF 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() { _link_tool() {
from="$1" from="$1"
to="$2.${1##*.}" to="$2.${1##*.}"
rm -rf tools/$to rm -rf tools/$to
if _is_win; then if _is_win; then
(cd tools && cmd <<< "mklink $to $from" > /dev/null) (cd tools && cp -f $from $to)
else else
(cd tools && ln -s $from $to) (cd tools && ln -s $from $to)
fi fi
@@ -601,6 +633,12 @@ _is_win() {
fi fi
} }
_argc_before() {
if [[ -d ".venv/bin/activate" ]]; then
source .venv/bin/activate
fi
}
_choice_tool() { _choice_tool() {
for item in "${LANG_CMDS[@]}"; do for item in "${LANG_CMDS[@]}"; do
lang="${item%:*}" lang="${item%:*}"