This repository was archived by the owner on May 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpyinit
More file actions
executable file
·266 lines (226 loc) · 6.73 KB
/
pyinit
File metadata and controls
executable file
·266 lines (226 loc) · 6.73 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/bin/bash -e
# pyinit
#
# This script will initialize a new git project in the current folder
# create a virtalenv and configure autoenv.
#
# Please install git, virtualenv, and autoenv, activate autoenv before using this script!
# see https://pypi.python.org/pypi/autoenv for instruction on how to install and activate autoenv
#
# Check my github: https://github.com/provonet/pyinit for documentation and new releases
# Johan Bakker
# Set CLICOLOR if you want Ansi Colors in iTerm2
export CLICOLOR=1
function help {
cat << EOF
USAGE: pyinit [OPTION]... [python][python2][python3][pypy][pypy3]
-a skip autoenv init
-d enable debugging
-h show this help text and exit
-g skip git init
-r remove virtualenv
-u upgrade virtualenv
-v show version and exit
EOF
exit 0
}
function install_requirements {
if ls requirements*.txt > /dev/null 2>&1; then
source "${VENV}/${_projectdir}/bin/activate"
pip install --upgrade setuptools
for _requirement_file in requirements*.txt; do
# respect ordering in requirement file
for _requirement in $(cat "${_requirement_file}"|grep -v '^#'); do
echo "installing: ${_requirement}"
pip install ${_requirement}
done
done
fi
}
function python_virtualenv {
local _python=$1
local _projectdir=$2
local _requirement=''
if [[ ! -d ${VENV} ]]; then
mkdir ${VENV}
fi
# Create virtualenv and install requirements
if [[ ! -d "${VENV}/${_projectdir}" ]]; then
printf '\e[92mcreating virtualenv\e[39m\n'
virtualenv -p ${_python} "${VENV}/${_projectdir}"
install_requirements
virtualenv -p ${_python} --relocatable "${VENV}/${_projectdir}"
fi
}
function _remove_virtualenv_dir {
local _projectdir=$1
if [[ -d "${VENV}/${_projectdir}" ]]; then
rm -rf "${VENV}/${_projectdir}"
rm -d ${VENV}
fi
}
function _remove_autoenv_dir {
local _projectdir=$1
local _envfile=${AUTOENV_ENV_FILENAME:-.env}
local _authfile=${AUTOENV_AUTH_FILE:-~/.autoenv_authorized}
if [[ -L .env ]]; then
set +e
hash=($(shasum "$(pwd -P)/${_envfile}"))
rm -f "${_envfile}"
if grep -q ${hash[0]} "${_authfile}"; then
cp "${_authfile}" "${_authfile}.old"
grep -v ${hash[0]} "${_authfile}.old" > "${_authfile}"
fi
set -e
fi
}
function remove_virtualenv {
local _projectdir=$1
printf '\e[92mremoving virtualenv\e[39m\n'
_remove_autoenv_dir $_projectdir
_remove_virtualenv_dir $_projectdir
}
function update_virtualenv {
local _projectdir=$1
local _python_version=$(env python -V 2>&1)
printf '\e[92mupdating virtualenv\e[39m\n'
source "${VENV}/${_projectdir}/bin/activate"
install_requirements
if [[ $_python_version =~ ' 2.' ]]; then
virtualenv -p python2 --relocatable "${VENV}/${_projectdir}"
elif [[ $_python_version =~ ' 3.' ]]; then
virtualenv -p python3 --relocatable "${VENV}/${_projectdir}"
else
virtualenv --relocatable "${VENV}/${_projectdir}"
fi
}
function python_autoenv {
local _projectdir=$1
local _envfile="${AUTOENV_ENV_FILENAME:-.env}"
local _authfile=${AUTOENV_AUTH_FILE:-~/.autoenv_authorized}
# Add autoenv symlink
if [[ ! -L ${_envfile} ]]; then
printf '\e[92menable autoenv\e[39m\n'
touch ${_authfile}
ln -s "${VENV}/${_projectdir}/bin/activate" "${_envfile}"
hash=($(shasum "$(pwd -P)/${_envfile}"))
if ! grep -q ${hash[0]} "${_authfile}"; then
echo "${hash[1]}:${hash[0]}" >> "${_authfile}"
fi
fi
}
function gitinit {
local _envfile="${AUTOENV_ENV_FILENAME:-.env}"
if [[ ! -f .gitignore ]]; then
touch .gitignore
fi
# Add .env directory to .gitignore
if ! grep -q "^${_envfile}$" .gitignore; then
echo "${_envfile}" >> .gitignore
fi
# Add virtualenv directory to .gitignore
if ! grep -q "^${VENV}" .gitignore; then
echo "${VENV}" >> .gitignore
fi
# Add .idea (pycharm) directory to .gitignore
if ! grep -q '^\.idea$' .gitignore; then
echo '.idea' >> .gitignore
fi
# Add *.pyc files to .gitignore
if ! grep -q '^\*\.pyc' .gitignore; then
echo '*.pyc' >> .gitignore
fi
if [[ ! -d .git ]]; then
printf '\e[92minitializing git project\e[39m\n'
git init
git add .gitignore
git commit -m 'initial commit' .gitignore
fi
}
# Main
PYTHON=${@: -1}
PROJECT=$(basename "$PWD")
_migrate=false
_remove=false
_update=false
_skipgit=false
_skipautoenv=false
while getopts ":hvgardu" OPTION; do
case $OPTION in
h) help
exit 1;;
v) echo 'pyinit 0.3.1'
exit 0;;
g) _skipgit=true;;
a) _skipautoenv=true;;
r) _remove=true;;
d) set -x;;
u) _update=true;;
\?) exit 1;;
esac
done
if [[ -d .virtualenv ]]; then
printf '\e[33mLegacy virtualenv folder detected\e[39m\n'
VENV='.virtualenv'
else
VENV='.venv'
fi
printf '\e[96mpyinit:\e[39m\n'
if [[ "${_remove}" == "true" ]]; then
remove_virtualenv "${PROJECT}"
exit 0
fi
if [[ "${_update}" == "true" ]]; then
update_virtualenv "${PROJECT}"
exit 0
fi
if [[ ! ${PYTHON} =~ ^python[23]$ ]]; then
if [[ -f requirements.txt ]]; then
if grep -qE '^#\s*pyinit:\s*python2' requirements.txt; then
PYTHON=$(basename $(which -a python2 || which python))
elif grep -qE '^#\s*pyinit:\s*python3' requirements.txt; then
PYTHON=$(basename $(which -a python3 || which python))
elif grep -qE '^#\s*pyinit:\s*pypy3' requirements.txt; then
PYTHON=pypy3
elif grep -qE '^#\s*pyinit:\s*pypy' requirements.txt; then
PYTHON=pypy
elif grep -qE '^#\s*pyinit:\s*python' requirements.txt; then
PYTHON=python
fi
fi
if [[ ! ${PYTHON} =~ ^(python|pypy) ]]; then
printf "\e[91mno python version specified! use command line parameter or set python version in requirements.txt\e[39m\n"
exit 1
elif [[ ! ${PYTHON} =~ ^(python|pypi)[23]?$ ]]; then
printf "\e[91minvalid python version! use python, python2 or python3\e[39m\n"
exit 1
fi
fi
# find path
PYTHON=$(ls -1 $(which ${PYTHON})|sort -nr| head -n1)
if ! which virtualenv >/dev/null; then
printf '\e[91mplease install "virtualenv" before using this script!\e[39m\n'
help
exit 1
fi
python_virtualenv "${PYTHON}" "${PROJECT}"
if [[ "${_skipautoenv}" == "false" ]]; then
if ! which shasum >/dev/null; then
printf '\e[91mplease install "shasum" before using this script!\e[39m\n'
help
exit 1
fi
python_autoenv "${PROJECT}"
else
printf '\e[32mskipping autoenv initialization\e[39m\n'
fi
if [[ "${_skipgit}" == "false" ]]; then
gitinit
else
printf '\e[32mskipping git initialization\e[39m\n'
fi
if [[ ${_skipautoenv} == "false" ]]; then
printf '\e[93muse "cd ." to enable the new virtualenv\e[39m\n'
else
printf "\e[93muse \"source '${VENV}/${PROJECT}/bin/activate'\" to enable the new virtualenv\e[39m\n"
fi