Skip to content

Commit 359ce58

Browse files
committed
Change venv dir, tweak detection logic
1 parent afa6187 commit 359ce58

File tree

2 files changed

+73
-16
lines changed

2 files changed

+73
-16
lines changed

plugins/pip/venvShellHook.sh

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,71 @@
1-
#!/bin/sh
1+
STATE_FILE="$DEVBOX_PROJECT_ROOT/.devbox/venv_check_completed"
22

3-
if ! [ -d "$VENV_DIR" ]; then
4-
echo "Creating new venv environment in path: '${VENV_DIR}'"
5-
python3 -m venv "$VENV_DIR"
6-
echo "You can activate the virtual environment by running '. \$VENV_DIR/bin/activate' (for fish shell, replace '.' with 'source')" >&2
3+
is_valid_venv() {
4+
[ -f "$1/bin/activate" ] && [ -f "$1/bin/python" ]
5+
}
6+
7+
# Function to check if Python is a symlink to a Devbox Python
8+
is_devbox_python() {
9+
if [ -z "$DEVBOX_PACKAGES_DIR" ]; then
10+
echo "DEVBOX_PACKAGES_DIR is not set. Unable to check for Devbox Python."
11+
return 1
12+
fi
13+
local python_path=$(readlink "$1/bin/python")
14+
echo $python_path
15+
echo $DEVBOX_PACKAGES_DIR
16+
[[ $python_path == $DEVBOX_PACKAGES_DIR/bin/python* ]]
17+
}
18+
19+
# Function to check Python version
20+
check_python_version() {
21+
python_version=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
22+
if [ "$(printf '%s\n' "3.3" "$python_version" | sort -V | head -n1)" = "3.3" ]; then
23+
return 0
24+
else
25+
return 1
26+
fi
27+
}
28+
29+
# Check if we've already run this script
30+
if [ -f "$STATE_FILE" ]; then
31+
exit 0
732
fi
833

34+
# Check Python version
35+
if ! check_python_version; then
36+
echo "\n\033[1;33m========================================\033[0m"
37+
echo "\033[1;33mWARNING: Python version must be > 3.3 to create a virtual environment.\033[0m"
38+
echo "\033[1;33m========================================\033[0m"
39+
touch "$STATE_FILE"
40+
exit 1
41+
fi
42+
43+
# Check if the directory exists
44+
if [ -d "$VENV_DIR" ]; then
45+
if is_valid_venv "$VENV_DIR"; then
46+
if ! is_devbox_python "$VENV_DIR"; then
47+
echo "\n\033[1;33m========================================\033[0m"
48+
echo "\033[1;33mWARNING: Existing virtual environment doesn't use Devbox Python.\033[0m"
49+
echo "\033[1;33m========================================\033[0m"
50+
echo "Virtual environment: $VENV_DIR"
51+
read -p "Do you want to overwrite it? (y/n) " -n 1 -r
52+
echo
53+
if [[ $REPLY =~ ^[Yy]$ ]]; then
54+
echo "Overwriting existing virtual environment..."
55+
rm -rf "$VENV_DIR"
56+
python3 -m venv "$VENV_DIR"
57+
else
58+
echo "Operation cancelled."
59+
touch "$STATE_FILE"
60+
exit 1
61+
fi
62+
fi
63+
else
64+
echo "Directory exists but is not a valid virtual environment. Creating new one..."
65+
rm -rf "$VENV_DIR"
66+
python -m venv "$VENV_DIR"
67+
fi
68+
else
69+
echo "Virtual environment directory doesn't exist. Creating new one..."
70+
python -m venv "$VENV_DIR"
71+
fi

plugins/python.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
{
22
"name": "python",
3-
"version": "0.0.3",
4-
"description": "Python in Devbox works best when used with a virtual environment (vent, virtualenv, etc.). Devbox will automatically create a virtual environment using `venv` for python3 projects, so you can install packages with pip as normal.\nTo activate the environment, run `. $VENV_DIR/bin/activate` or add it to the init_hook of your devbox.json\nTo change where your virtual environment is created, modify the $VENV_DIR environment variable in your init_hook",
3+
"version": "0.0.4",
4+
"description": "Python in Devbox works best when used with a virtual environment (venv, virtualenv, etc.). Devbox will automatically create a virtual environment using `venv` for python3 projects, so you can install packages with pip as normal.\nTo activate the environment, run `. $VENV_DIR/bin/activate` or add it to the init_hook of your devbox.json\nTo change where your virtual environment is created, modify the $VENV_DIR environment variable in your init_hook",
55
"env": {
6-
/*
7-
This is a block comment
8-
*/
9-
"VENV_DIR": "{{ .Virtenv }}/.venv"
6+
"VENV_DIR": "{{ .DevboxProjectDir }}/.venv"
107
},
118
"create_files": {
12-
"{{ .Virtenv }}/bin/venvShellHook.sh": "pip/venvShellHook.sh"
9+
"{{ .Virtenv }}/bin/venvShellHook.sh": "pip/venvShellHook.sh"
1310
},
14-
// this is a line comment above shell
1511
"shell": {
16-
"init_hook": [
17-
"{{ .Virtenv }}/bin/venvShellHook.sh"
18-
]
12+
"init_hook": ["{{ .Virtenv }}/bin/venvShellHook.sh"]
1913
}
2014
}

0 commit comments

Comments
 (0)