-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.bashrc.greenvenv
222 lines (192 loc) · 5.97 KB
/
.bashrc.greenvenv
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
#!/bin/bash
function greenvenv_loadconsts()
{
# Terminal colors
GREENVENV_CLR_BLACK=$(tput setaf 0)
GREENVENV_CLR_RED=$(tput setaf 1)
GREENVENV_CLR_GREEN=$(tput setaf 2)
GREENVENV_CLR_YELLOW=$(tput setaf 3)
GREENVENV_CLR_LIME_YELLOW=$(tput setaf 190)
GREENVENV_CLR_POWDER_BLUE=$(tput setaf 153)
GREENVENV_CLR_BLUE=$(tput setaf 4)
GREENVENV_CLR_MAGENTA=$(tput setaf 5)
GREENVENV_CLR_CYAN=$(tput setaf 6)
GREENVENV_CLR_WHITE=$(tput setaf 7)
GREENVENV_CLR_BRIGHT=$(tput bold)
GREENVENV_CLR_NORMAL=$(tput sgr0)
GREENVENV_CLR_BLINK=$(tput blink)
GREENVENV_CLR_REVERSE=$(tput smso)
GREENVENV_CLR_UNDERLINE=$(tput smul)
GREENVENV_CLR_BOLD=$(tput bold)
GREENVENV_TXT_INDEXING='When you could not found what you are looking for you might to update index: "sudo updatedb" '
GREENVENV_TXT_BYE="Bye!"
}
# Prints a line with color using terminal codes
function greenvenv_print()
{
printf "%s\n" "${!2}$1${GREENVENV_CLR_NORMAL}"
}
function greenvenv_find()
{
echo ""
}
function greenvenv_cleanup()
{
for var in "${!GREENVENV_@}"; do
unset ${var}
done
}
function rebuild()
{
greenvenv_loadconsts
echo $VIRTUAL_ENV
if [ ! "$(type -t deactivate)" = "function" ]; then
greenvenv_print 'Are you sure you properly activated the env?' GREENVENV_CLR_RED
return
fi
greenvenv_venvpath=$VIRTUAL_ENV
greenvenv_temp="tmp-requirements-$(echo $VIRTUAL_ENV | sed -e 's/[^A-Za-z0-9._-]/-/g').txt"
echo $greenvenv_temp
# first we remove to be sure that it is really new result
#rm $greenvenv_temp
$greenvenv_venvpath/bin/python -m pip freeze > $greenvenv_temp
if [[ -z $greenvenv_temp ]]; then
greenvenv_print 'Something did not work. Requirements not found or you installed nothing.' GREENVENV_CLR_RED
return
fi
deactivate
echo $greenvenv_venvpath
echo "SOOORRY here we need to some work for the future, to let you create a venv"
return
mv $greenvenv_venvpath "${greenvenv_venvpath}.backup"
#virtualenv -p python3 name
#python3 -m venv /path/to/new/virtual/environment
pip install -r /tmp/requirements.txt
rm $greenvenv_temp
}
function greenvenv()
{
# Get the options
TEMP=$(getopt :hiar "$@")
eval set -- "$TEMP"
while true ; do
case "$1" in
-h)
echo "You will get help here in the future"
break
;;
-i)
echo "Start reindexing for a better search"
updatedb
break
;;
-r)
echo "Start rebuilding the env: 1 rename -> 2 new with same name $2"
rebuild
break
;;
-a)
echo "This are the aliases (only correct after activate venv)"
greenvenv_loadconsts
greenvenv_loadalias
break
;;
*)
echo "Nothing done for $1 $option"
break ;;
--) shift ; break ;;
esac
done
}
function chenv()
{
greenvenv_loadconsts
#locate bases on a database which is daily updated. to manually update: sudo updatedb
files=(`locate --regex "bin/activate"$ | sed 's/bin\/activate$//'`)
PS3='Select a env to activate, or 0 to exit: '
select file in "${files[@]}"; do
if [[ $REPLY == "0" ]]; then
greenvenv_print $GREENVENV_TXT_INDEXING GREENVENV_CLR_YELLOW
echo $GREENVENV_TXT_BYE
break
elif [[ -z $file ]]; then
greenvenv_print $GREENVENV_TXT_INDEXING GREENVENV_CLR_YELLOW
greenvenv_print 'Invalid choice, try again' GREENVENV_CLR_RED
break
else
echo 'Deactivate current env..'
deactivate
greenvenv_print 'Activate choosen environment.. Enjoy' GREENVENV_CLR_GREEN
source "${file}bin/activate"
break
fi
done
greenvenv_cleanup
}
function greenvenv_loadalias
{
declare -A aliase
alias_restart="python ${file}manage.py makemigrations && python ${file}manage.py migrate && python ${file}manage.py collectstatic --no-input"
if [[ ! -z "$cmd_restart" ]]; then
alias_restart+=" && ${cmd_restart}"
if [[ ! -z "$cmd_restart_beep" ]]; then
alias_restart+=' && echo -e "\07" && echo -e "\007" '
fi
fi
aliase=( ['collectstatic']="python ${file}manage.py collectstatic --no-input" ['restart']="${alias_restart}" ['requirements']="pip3 freeze > ${file}requirements.txt")
#parse aliases from the file pyvenv.cfg
while IFS= read -r words; do
words="${words:6}"
temp="${words#*=}"
temp="${temp%\"}"
temp="${temp#\"}"
aliase["${words%%=*}"]="${temp}"
done < <(cat ${venv_cfg_filepath} | awk -F= '/^alias (.*)/{print $0}')
for cmd in "${!aliase[@]}"; do
echo "- Alias '${GREENVENV_CLR_BOLD}$cmd${GREENVENV_CLR_NORMAL}': \"${aliase[$cmd]}\""
alias $cmd="${aliase[$cmd]}"
done
echo "- More Aliases you can register directly in the pyvenv"
}
function chdjango()
{
greenvenv_loadconsts
#locate bases on a database which is daily updated. to manually update: sudo updatedb
files=(`locate --regex "/home/.*/manage.py"$ | sed 's/manage.py$//'`)
PS3='Select a django project, or 0 to exit: '
select file in "${files[@]}"; do
echo ""
if [[ $REPLY == "0" ]]; then
greenvenv_print $GREENVENV_TXT_INDEXING GREENVENV_CLR_YELLOW
echo $GREENVENV_TXT_BYE
break
elif [[ -z $file ]]; then
greenvenv_print $GREENVENV_TXT_INDEXING GREENVENV_CLR_YELLOW
greenvenv_print 'Invalid choice, try again' GREENVENV_CLR_RED
break
else
#change to the directory of the project
cd "${file}"
#based on https://stackoverflow.com/questions/45216663/how-to-automatically-activate-virtualenvs-when-cding-into-a-directory
venv_cfg_filepath=$(find . -maxdepth 2 -type f -name 'pyvenv.cfg' 2> /dev/null)
if [[ -z "$venv_cfg_filepath" ]]; then
greenvenv_print "no config file found. Please locate pyvenv.cfg in the same directory" GREENVENV_CLR_RED
return # no config file found
fi
source ${venv_cfg_filepath}
#echo "config file found $venv_cfg_filepath and the path to the env -$venv_filepath<"
if [ -d "$venv" ]; then
if [[ ! -z "$VIRTUAL_ENV" ]]; then
[ "$(type -t deactivate)" = "function" ] && echo "- Deactivate current one" && deactivate nondestructive
fi
greenvenv_print "- Activate $venv" GREENVENV_CLR_GREEN
source "${venv}"/bin/activate
greenvenv_loadalias
else
greenvenv_print "$venv is not a directory or readable ${venv_cfg_filepath}" GREENVENV_CLR_RED
fi
break
fi
done
greenvenv_cleanup
}