-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkvenv
executable file
·170 lines (135 loc) · 3.84 KB
/
mkvenv
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
#!/usr/bin/env bash
# Description: Create and maintain python venvs from JSON formatted settings file
# Usage:
# 1. update settings.json with desired settings
# 2. run the script: ./mk_venv
script_dir="$(dirname -- "$(readlink -f "${0}" )")"
settings_file="${script_dir}/settings.json"
# Check for and load settings.json
settings_check() {
if [ ! -f "${settings_file}" ]; then
printf '%s\n' "[ERROR] Can't find settings.json file. Exiting.."
exit 1
else
settings=$(< "${settings_file}")
fi
}
# Query settings.json for venvs
get_venvs() {
echo "${settings}" | python3 -c "
import json
import sys
input = sys.stdin
data = json.load(input)
for k in data.keys():
print(k, end=' ')
"
}
# Query settings.json for venv locations
venv_location() {
echo "${settings}" | python3 -c "
import json
import sys
from pathlib import Path
input = sys.stdin
data = json.load(input)
directory = Path(data['${venv}']['venv_base_dir'])
print(directory.expanduser())
"
}
# Query settings.json for venv prompt
venv_prompt() {
echo "${settings}" | python3 -c "
import json
import sys
input = sys.stdin
data = json.load(input)
prompt = data['${venv}']['venv_prompt']
print(prompt)
"
}
# Query settings.json for venv module requirements
venv_requirements() {
echo "${settings}" | python3 -c "
import json
import sys
input = sys.stdin
data = json.load(input)
for module in data['${venv}']['venv_requirements']:
print(module)
"
}
# Creates a python venv
create_venv () {
if [ ! -f "${venv_path}/bin/activate" ]; then
printf '%s\n' "[INFO] Creating ${venv} venv"
cd "${venv_base_dir}" || exit
python3 -m venv --upgrade-deps --prompt "${venv_prompt}" "${venv}"
fi
}
# Compares new with existing requirements and creates array of modules to uninstall
compare_requirements () {
remove=()
while IFS="" read -r line; do
remove+=("$line")
done < <(venv_requirements | diff "${requirements_file}" - | awk -F' ' '/</ { print $2 }')
}
# Creates or updates requirements.txt
create_requirements () {
if [ ! -f "${requirements_file}" ]; then
printf '%s\n' "[INFO] Creating ${venv} requirements.txt"
venv_requirements > "${requirements_file}"
else
if ! venv_requirements | cmp --silent "${requirements_file}" -; then
compare_requirements
printf '%s\n' "[INFO] Updating ${venv} requirements.txt with changes found in settings.json"
venv_requirements > "${requirements_file}"
fi
fi
}
# Uninstalls python modules
uninstall_module () {
python3 -m pip uninstall --require-virtualenv "${1}"
}
# Upgrades python modules
upgrade_modules () {
python3 -m pip install --require-virtualenv --upgrade -r "${requirements_file}" | \
grep -v 'Requirement already satisfied'
printf '%s\n\n' "[INFO] ${venv} modules up to date!"
}
# Check and load settings
settings_check
# Create array of venvs from settings.json
IFS=" " read -r -a venvs <<< "$(get_venvs)"
# Loop over venvs and create or update the venv and python modules
for venv in "${venvs[@]}"; do
venv_base_dir="$(venv_location)"
venv_prompt="$(venv_prompt)"
venv_path="${venv_base_dir}/${venv}"
requirements_file="${venv_path}/requirements.txt"
printf '%s\n' "[INFO] Working on ${venv} venv"
# Make sure venv_base_dir exists
if [ ! -d "${venv_base_dir}" ]; then
mkdir -p "${venv_base_dir}"
fi
# Make sure venv exists
create_venv
# Make sure requirements_file exists
create_requirements
# Activate venv and install, remove or upgrade modules
if [ ! "$VIRTUAL_ENV" = "${venv_path}" ]; then
# shellcheck source=/dev/null
. "${venv_path}/bin/activate"
fi
# Uninstall modules
if [ ${#remove[@]} -gt 0 ]; then
for module in "${remove[@]}"; do
uninstall_module "${module}"
done
fi
# Upgrade modules
upgrade_modules
# Reset array so we don't uninstall modules in other venvs
remove=()
done
# vim: ft=sh ts=2 sts=2 sw=2 sr et