Skip to content

Commit 1daaef4

Browse files
committed
cleanup, add gitignore
1 parent 796706e commit 1daaef4

File tree

1 file changed

+14
-52
lines changed

1 file changed

+14
-52
lines changed

plugins/pip/venvShellHook.sh

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,18 @@
1+
#!/bin/sh
12
set -eu
23
STATE_FILE="$DEVBOX_PROJECT_ROOT/.devbox/venv_check_completed"
3-
echo $STATE_FILE
44

55
is_valid_venv() {
66
[ -f "$1/bin/activate" ] && [ -f "$1/bin/python" ]
77
}
88

9-
# Function to check if Python is a symlink to a Devbox Python
10-
is_devbox_python() {
11-
if [ -z "$DEVBOX_PACKAGES_DIR" ]; then
12-
echo "DEVBOX_PACKAGES_DIR is not set. Unable to check for Devbox Python."
13-
return 1
14-
fi
15-
local python_path="$1/bin/python"
16-
local link_target
17-
18-
while true; do
19-
if [ ! -L "$python_path" ]; then
20-
# Not a symlink, we're done
21-
break
22-
fi
23-
24-
link_target=$(readlink "$python_path")
25-
echo "Checking symlink: $link_target"
26-
27-
if [[ "$link_target" == /* ]]; then
28-
# Absolute path, we're done
29-
python_path="$link_target"
30-
break
31-
elif [[ "$link_target" == python* ]] || [[ "$link_target" == ./* ]] || [[ "$link_target" == ../* ]]; then
32-
# Relative path or python symlink, continue resolving
33-
python_path=$(dirname "$python_path")/"$link_target"
34-
else
35-
# Unexpected format, stop here
36-
break
37-
fi
38-
done
39-
40-
[[ $python_path == $DEVBOX_PACKAGES_DIR/* ]]
9+
is_devbox_venv() {
10+
[ "$1/bin/python" -ef "$DEVBOX_PACKAGES_DIR/bin/python" ]
4111
}
4212

43-
# Function to check Python version
44-
check_python_version() {
45-
python_version=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
46-
if [ "$(printf '%s\n' "3.3" "$python_version" | sort -V | head -n1)" = "3.3" ]; then
47-
return 0
48-
else
49-
return 1
50-
fi
13+
create_venv() {
14+
python -m venv "$VENV_DIR" --clear
15+
echo "*\n.*" >> "$VENV_DIR/.gitignore"
5116
}
5217

5318
# Check if we've already run this script
@@ -56,8 +21,8 @@ if [ -f "$STATE_FILE" ]; then
5621
exit 0
5722
fi
5823

59-
# Check Python version
60-
if ! check_python_version; then
24+
# Check that Python version suports venv
25+
if ! python -c "import venv" &>/dev/null; then
6126
echo "\033[1;33mWARNING: Python version must be > 3.3 to create a virtual environment.\033[0m"
6227
touch "$STATE_FILE"
6328
exit 1
@@ -66,27 +31,24 @@ fi
6631
# Check if the directory exists
6732
if [ -d "$VENV_DIR" ]; then
6833
if is_valid_venv "$VENV_DIR"; then
69-
if ! is_devbox_python "$VENV_DIR"; then
34+
if ! is_devbox_venv "$VENV_DIR"; then
7035
echo "\033[1;33mWARNING: Virtual environment at $VENV_DIR doesn't use Devbox Python.\033[0m"
71-
echo "Virtual environment: $VENV_DIR"
7236
read -p "Do you want to overwrite it? (y/n) " -n 1 -r
7337
echo
7438
if [[ $REPLY =~ ^[Yy]$ ]]; then
7539
echo "Overwriting existing virtual environment..."
76-
rm -rf "$VENV_DIR"
77-
python3 -m venv "$VENV_DIR"
40+
create_venv
7841
else
79-
echo "Using existing virtual environment. We recommend changing \$VENV_DIR"
42+
echo "Using your existing virtual environment. We recommend changing \$VENV_DIR to a different location"
8043
touch "$STATE_FILE"
8144
exit 1
8245
fi
8346
fi
8447
else
85-
echo "Directory exists but is not a valid virtual environment. Creating new one..."
86-
rm -rf "$VENV_DIR"
87-
python -m venv "$VENV_DIR"
48+
echo "Directory exists but is not a valid virtual environment. Creating a new one..."
49+
create_venv
8850
fi
8951
else
9052
echo "Virtual environment directory doesn't exist. Creating new one..."
91-
python -m venv "$VENV_DIR"
53+
create_venv
9254
fi

0 commit comments

Comments
 (0)