aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsigoden <sigoden@gmail.com>2024-10-21 08:01:27 +0800
committerGitHub <noreply@github.com>2024-10-21 08:01:27 +0800
commitd499954dbbfd9e4d9911d179092385ac0d161e56 (patch)
treef2cbbac0a78bd3d0e3f8587eb2cd631294d68c32
parente098f7f43a7e8e4e85149bc5d24aa854a98cec66 (diff)
downloadllm-functions-docker-d499954dbbfd9e4d9911d179092385ac0d161e56.tar.gz
feat: support python virtualenv (#116)
-rw-r--r--.gitignore2
-rw-r--r--Argcfile.sh46
2 files changed, 43 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index bdfee81..b9a3d65 100644
--- a/.gitignore
+++ b/.gitignore
@@ -9,7 +9,7 @@ functions.json
/tools/code_interpreter.*
/.env
__pycache__
-/venv
+/.venv
node_modules
/package.json
/package-lock.json
diff --git a/Argcfile.sh b/Argcfile.sh
index 1f1f770..a84464a 100644
--- a/Argcfile.sh
+++ b/Argcfile.sh
@@ -3,6 +3,7 @@ set -e
BIN_DIR=bin
TMP_DIR="cache/tmp"
+VENV_DIR=".venv"
LANG_CMDS=( \
"sh:bash" \
@@ -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
@@ -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"
@@ -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
@@ -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
@@ -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%:*}"