1
+ #! /bin/sh
1
2
set -eu
2
3
STATE_FILE=" $DEVBOX_PROJECT_ROOT /.devbox/venv_check_completed"
3
- echo $STATE_FILE
4
4
5
5
is_valid_venv () {
6
6
[ -f " $1 /bin/activate" ] && [ -f " $1 /bin/python" ]
7
7
}
8
8
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" ]
41
11
}
42
12
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"
51
16
}
52
17
53
18
# Check if we've already run this script
@@ -56,8 +21,8 @@ if [ -f "$STATE_FILE" ]; then
56
21
exit 0
57
22
fi
58
23
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
61
26
echo " \033[1;33mWARNING: Python version must be > 3.3 to create a virtual environment.\033[0m"
62
27
touch " $STATE_FILE "
63
28
exit 1
66
31
# Check if the directory exists
67
32
if [ -d " $VENV_DIR " ]; then
68
33
if is_valid_venv " $VENV_DIR " ; then
69
- if ! is_devbox_python " $VENV_DIR " ; then
34
+ if ! is_devbox_venv " $VENV_DIR " ; then
70
35
echo " \033[1;33mWARNING: Virtual environment at $VENV_DIR doesn't use Devbox Python.\033[0m"
71
- echo " Virtual environment: $VENV_DIR "
72
36
read -p " Do you want to overwrite it? (y/n) " -n 1 -r
73
37
echo
74
38
if [[ $REPLY =~ ^[Yy]$ ]]; then
75
39
echo " Overwriting existing virtual environment..."
76
- rm -rf " $VENV_DIR "
77
- python3 -m venv " $VENV_DIR "
40
+ create_venv
78
41
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 "
80
43
touch " $STATE_FILE "
81
44
exit 1
82
45
fi
83
46
fi
84
47
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
88
50
fi
89
51
else
90
52
echo " Virtual environment directory doesn't exist. Creating new one..."
91
- python -m venv " $VENV_DIR "
53
+ create_venv
92
54
fi
0 commit comments