-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path.envrc
More file actions
106 lines (81 loc) · 3.46 KB
/
.envrc
File metadata and controls
106 lines (81 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# Direnv loader
# See https://direnv.net/man/direnv-stdlib.1.html for special functions
# Initializes and uses a python environment using pyenv
# Based on pyenv setup from https://github.com/direnv/direnv/wiki/Python
# See also https://github.com/direnv/direnv/blob/master/stdlib.sh
use_pyenv() {
if ! has pyenv; then
log_error "Error: pyenv is not installed."
return 1
fi
unset PYENV_VERSION
# Because each python version is prepended to the PATH, add them in reverse order
for ((j = $#; j >= 1; j--)); do
local python_version=${!j}
local pyenv_python=$(pyenv root)/versions/${python_version}/bin/python
local fresh_install=
if [[ ! -f "$pyenv_python" ]]; then
log_status "Installing python $python_version via pyenv"
pyenv install "$python_version"
fi
if [[ ! -x "$pyenv_python" ]]; then
log_error "Error: $pyenv_python can't be executed."
return 1
fi
unset PYTHONHOME
local ve=$($pyenv_python -c "import pkgutil; print('venv' if pkgutil.find_loader('venv') else ('virtualenv' if pkgutil.find_loader('virtualenv') else ''))")
case $ve in
"venv")
VIRTUAL_ENV=$(direnv_layout_dir)/python-$python_version
export VIRTUAL_ENV
if [[ ! -d $VIRTUAL_ENV ]]; then
$pyenv_python -m venv "$VIRTUAL_ENV"
fresh_install=1
fi
PATH_add "$VIRTUAL_ENV"/bin
;;
"virtualenv")
layout_python "$pyenv_python"
;;
*)
log_error "Error: neither venv nor virtualenv are available to ${pyenv_python}."
return 1
;;
esac
# e.g. Given "use pyenv 3.6.9 2.7.16", PYENV_VERSION becomes "3.6.9:2.7.16"
[[ -z "$PYENV_VERSION" ]] && PYENV_VERSION=$python_version || PYENV_VERSION="${python_version}:$PYENV_VERSION"
# If this is a fresh install, make sure pip is up to date
if [[ ! -z $fresh_install ]]; then
pip install --upgrade pip
fi
done
export PYENV_VERSION
}
main() {
# This is the version of python for which you expect to do most of your
# development.
use pyenv 3.7.8
PATH_add "$(pwd)/bin"
# Allow statements and log messages to immediately appear in the container logs
export PYTHONUNBUFFERED="True"
# Do we need to bootstrap this dev environment?
if ! python -c "import flake8" >/dev/null 2>/dev/null ; then
# CHANGEME: If this is your own package, remove the following if-else block
# and just leave the first `pip ... requirements.txt ...` command.
if [[ -f requirements.txt ]]; then
# If there is a requirements.txt (this is no longer sample-python-package
# but an actual package someone built from it), load that.
pip install -r requirements.txt -e ".[dev]"
else
# Otherwise, we're still developing sample-python-package and can load
# its development dependencies.
pip install -e ".[dev]"
fi
fi
# CHANGEME: If your development environment expects any custom environment variables
# (e.g. database URI), you can set them here to ensure users don't have to
# manage things by hand in their own .bashrc/.zshrc file.
SAMPLE_PYTHON_PACKAGE="true"
}
main "$@"