-
Notifications
You must be signed in to change notification settings - Fork 14
/
.env
227 lines (213 loc) · 9.96 KB
/
.env
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# /bin/bash - help gedit syntax highlight do the right thing
# autoenv script (https://github.com/kennethreitz/autoenv)
# Use ". .env -h" for help.
#
# This script is an autoenev script that either activates or creates
# a local virtualenv in '.venv' for (almost) any project. Typical
# project assets like 'setup.py' and requirements files are used to
# populate the virtualenv.
#
# This script is tested with bash, report problems with other shells to
#
# https://github.com/Springerle/py-generic-project/issues
#
# It should work on any Linux, and Mac OSX.
#
_venv_virtualenv="/usr/bin/python3 -m venv"
_venv_py3=true
_venv_choices=('$_venv_py_tk' 3.9 3.8 3.7 3.6 3.5 3)
_venv_pip_req='pip>20'
_venv_bin=bin
_venv_master_url="https://raw.githubusercontent.com/Springerle/py-generic-project/master/.env"
: ${USER:=$USERNAME}
#_venv_temp=/usr/local/bin/virtualenv
#if test -x $_venv_temp; then
# _venv_virtualenv=$_venv_temp
#fi
test "${_venv_py3:0:2}" != '{''{' || _venv_py3=true
if test -n "$SYSTEMROOT" -a -n "$WINDIR"; then
_venv_virtualenv=$(cygpath -au "$(py -3 -c 'import sys; print(sys.executable)')")" -m venv"
_venv_bin=Scripts
elif $_venv_py3; then # Python 3 mode?
# /opt/pyenv, see https://github.com/jhermann/priscilla/tree/master/pyenv
_venv_temp=/opt/pyenv/bin/python3
if test -x $_venv_temp; then
_venv_virtualenv="$_venv_temp -m venv"
fi
# python3 venv in recent Debian / Ubuntu
# prefer a version that has Tkinter installed, if available
_venv_py_tk=$(dpkg-query -W -f '${Version}\n' 'python3*-tk' 2>/dev/null | sort -rV | head -n1 | cut -f-2 -d.)
for _venv_temp in "${_venv_choices[@]}"; do
_venv_temp="/usr/bin/python$(eval echo $_venv_temp)"
if test -x $_venv_temp; then
_venv_virtualenv="$_venv_temp -m venv"
break
fi
done
unset _venv_py_tk
fi
_venv_readlink=readlink
case "$(uname -s)" in
# See http://en.wikipedia.org/wiki/Uname#Examples
Darwin)
_venv_temp="/usr/local/opt/coreutils/libexec/gnubin/readlink"
if test -x $_venv_temp; then
_venv_readlink=$_venv_temp
else
echo "*** No readlink command, do a 'brew install coreutils'..."
return 1
fi
;;
CYGWIN*)
;;
Linux|*)
;;
esac
test -z "$JENKINS_URL" || echo '***' "\$0=$0" "\$BASH_SOURCE=${BASH_SOURCE[0]}"
if test "$0" = "-bash" -o "$(basename -- "$0")" = "bash" -o "$(basename -- "${BASH_SOURCE[0]}")" = ".env"; then
_venv_script=$($_venv_readlink -f ${BASH_SOURCE[0]})
elif test "$(basename -- "$0")" = ".env"; then
_venv_script=$($_venv_readlink -f "$0")
fi
_venv_script="${_venv_script:-/dont-know-really/.env}"
_venv_xtrace=$(set +o | grep xtrace)
set +x
_venv_name="$(basename $(pwd))"
_venv_pip_log() {
if $_venv_verbose; then
cat
else
egrep "Found.existing.installation|Collecting|Installing.collected.packages|Searching.for|Installed|Finished"
fi
}
# command line flags, mostly for CI server usage
_venv_create=false
_venv_develop=false
_venv_verbose=false
while test "${1:0:1}" = "-"; do
case "$1" in
--yes) _venv_create=true ;;
--develop) _venv_develop=true ;;
--verbose | -v) _venv_verbose=true; set -x ;;
--virtualenv) shift; _venv_virtualenv="$1" ;;
--help | -h)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo "usage: . $_venv_script <options>"
echo
echo "Create and/or activate a Python virtual environment. Updates pip and other"
echo "system packages, and recognizes 'dev-requirements.txt' and 'setup.py' or"
echo "'requirements.txt'."
echo
echo "This is an autoenv script (see https://github.com/kennethreitz/autoenv)."
echo
echo "Options:"
echo " --yes create missing venv without prompting"
echo " --develop call 'develop -U' on activation"
echo " -v | --verbose don't filter install logs on terminal"
echo " (full logs in '.venv/install-*.log')"
echo " -h | --help this help message"
echo " --virtualenv PATH set explicit path of virtualenv binary to use"
echo " --update update to the newest master copy"
return 1
;;
--update)
( cd /tmp && $(command which python3 py 2>/dev/null | head -n1) <<EOF
if 1:
try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen
open('$USER-py-env-update.py', 'wb').write(urlopen('$_venv_master_url').read())
EOF
)
grep "unset _venv_xtrace" "/tmp/$USER-py-env-update.py" >/dev/null || return 1
sed -r -e "s/^_venv_py3=.*/_venv_py3=$_venv_py3/" <"/tmp/$USER-py-env-update.py" >.env
git diff .env || :
echo ".env script update OK"
return 0
;;
*) echo "WARNING: Ignored unknown option '$1'" ;;
esac
shift
done
# Outside the tree of the .env script?
if pwd -P | egrep -v '^'$(dirname "$_venv_script")'($|/)' >/dev/null; then
: echo Outside "[$0 $1 ; $_venv_script]"
# Inside the tree of the .env script, but have another local '.env'?
elif test \! -f .env -o "$_venv_script" != "$(pwd -P)/.env"; then
: echo Inside "[$0 $1 ; $_venv_script]"
# Only try virtualenv creation outside of template dirs; the egrep pattern is escaped for hiding it from Jinja2
elif pwd -P | egrep -v '/{''{''.*''}''}(/|$)' >/dev/null || $_venv_create; then
test -f ".env" && _venv_ask=true || _venv_ask=false
# Look for existing venv at common locations
for _venv_base in .venv/$_venv_name .venv venv/$_venv_name venv . ..; do
if test -f "$_venv_base/$_venv_bin/activate"; then
deactivate 2>/dev/null || :
. "$_venv_base/$_venv_bin/activate"
if test -f setup.py; then
$_venv_develop && $_venv_base/$_venv_bin/python setup.py -q develop -U || :
$_venv_base/$_venv_bin/python setup.py --name --version --url | tr -d \\r \
| xargs -d$'\n' printf "*** Activated %s %s @ %s\\n" || :
else
echo "*** Activated $_venv_base"
fi
_venv_ask=false
break
fi
done
if $_venv_ask && test \! -d .venv; then
$_venv_create || { read -p "No virtualenv found, shall I create one for you? [Y/n] " -n 1 -r || REPLY='n'; echo; }
if $_venv_create || echo "$REPLY" | egrep '^[Yy]?$' >/dev/null; then
# Create, activate, and update virtualenv
echo "Calling: $_venv_virtualenv --prompt $_venv_name .venv/"
eval $_venv_virtualenv "--prompt $_venv_name .venv/"
. ".venv/$_venv_bin/activate"
".venv/$_venv_bin/python" -m pip --log ".venv/install-pip.log" install -U "$_venv_pip_req" 2>&1 | _venv_pip_log \
|| echo >&2 "!!! WARN: pip -U pip failed (RC=$?), see .venv/install-pip.log for details"
".venv/$_venv_bin/python" -m pip --log ".venv/install-tools.log" install -U "setuptools>40" "wheel>0.30" 2>&1 | _venv_pip_log \
|| echo >&2 "!!! WARN: pip -U setuptools wheel failed (RC=$?), see .venv/install-tools.log for details"
# Get rid of cruft some older systems produce
".venv/$_venv_bin/python" -m pip --log ".venv/uninstall-distribute.log" uninstall --yes distribute >/dev/null 2>&1 || :
# pypandoc fails when the base package is missing, so we install it here, if possible
if which pandoc >/dev/null 2>&1 ; then
".venv/$_venv_bin/python" -m pip --log ".venv/install-pandoc.log" install pypandoc 2>&1 | _venv_pip_log \
|| echo >&2 "!!! WARN: pip install pypandoc failed (RC=$?), see .venv/install-pandoc.log for details"
fi
# Install development + project dependencies
if test -f dev-requirements.txt; then
".venv/$_venv_bin/python" -m pip --log ".venv/install-dev.log" install -r dev-requirements.txt 2>&1 | _venv_pip_log \
|| echo >&2 "!!! WARN: pip install dev-requirements failed (RC=$?), see .venv/pip-install-dev.log for details"
fi
if test -f setup.py; then
if grep "^# mkvenv: no-deps" "setup.py" >/dev/null; then
echo "*** Not installing setup.py packages as requested"
else
".venv/$_venv_bin/python" setup.py develop -U 2>&1 | _venv_pip_log
fi
echo
".venv/$_venv_bin/python" setup.py --name --version --author --author-email --license --description --url \
| tr -d \\r | xargs -d$'\n' printf "%s %s by %s <%s> [%s]\\n%s\\n%s\\n" || :
else
echo
if test -f requirements.txt; then
echo "*** No 'setup.py' found, installing requirements..."
".venv/$_venv_bin/python" -m pip --log ".venv/pip-install-req.log" install -r requirements.txt 2>&1 | _venv_pip_log
else
echo "*** No 'setup.py' or 'requirements.txt' found, all done."
fi
fi
else
# prevent constant prompting
mkdir -p .venv
fi
fi
if test -n "$SYSTEMROOT" -a -n "$WINDIR" && ! grep .venv/Scripts/activate .venv/Scripts/activate >/dev/null; then
echo >>.venv/Scripts/activate \
'export PATH=$(echo $PATH | sed -r -e '\''s~[\\]~/~g'\'' -e s~C:~/c~); hash -r'
. .venv/Scripts/activate
fi
fi
unset _venv_script _venv_bin _venv_name _venv_ask _venv_create _venv_develop _venv_pip_log _venv_base _venv_py_tk
unset _venv_readlink _venv_temp _venv_virtualenv _venv_verbose _venv_py3 _venv_pip_req _venv_master_url _venv_choices
eval $_venv_xtrace # restore xtrace state
unset _venv_xtrace