-
Notifications
You must be signed in to change notification settings - Fork 2
/
assert-conda
executable file
·153 lines (134 loc) · 4.01 KB
/
assert-conda
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
#!/bin/bash
# Author: Gokberk Cinbis, 2021
set -e
verbose=0
silent=0
assert_pip=0
assert_pip3=0
function __increment_argi__() # just increment argi variable
{
argi=$((argi+1))
}
function __decrement_argi__() # just decrement argi variable
{
argi=$((argi-1))
}
function __myinfo__()
{
if [ $verbose -eq 1 ]; then
echo "assert-conda::[INFO] $@"
fi
}
function __myfail__()
{
if [ $silent -eq 0 ]; then
echo "assert-conda::[FAIL] $@"
fi
}
function __print_help__ ()
{
echo "assert-conda [-h] [-v]"
echo "Assert that the conda environment is active via PATH."
echo ""
echo "ARGUMENTS"
echo "-h Print help."
echo "-v Enable verbose mode (enables info messages)."
echo "-s Enable silent mode (disables failure messages)."
echo "--pip Additionally assert that pip belongs to the conda environment."
echo "--pip3 Additionally assert that pip3 belongs to the conda environment."
echo
echo "EXIT VALUES"
echo "0 Success, conda is active."
echo "1 Print help / invalid argument(s)."
echo "2 python has not been found in the PATH."
echo "3 conda has not been found in the PATH."
echo "4 conda has been found but python does not belong to the conda directory."
echo "5 conda has been found but pip or pip3 does not belong to the conda directory."
echo ""
exit 1
}
function __main__()
{
local narg=$#
local regexp="${!narg}" # last argument
# see findgrep for a more comprehensive example use of the loop below
argi=0 # argument reading index
for (( argi=1; argi<=$narg; argi++ )); do
if [ "${!argi}" == '-h' ]; then
__print_help__
elif [ "${!argi}" == '-v' ]; then
verbose=1
elif [ "${!argi}" == '-s' ]; then
silent=1
elif [ "${!argi}" == '--pip' ]; then
assert_pip=1
elif [ "${!argi}" == '--pip3' ]; then
assert_pip3=1
else
command echo "Unrecognized argument: {!argi}"
__print_help__
fi
done
# check python
set +e # forgive errors
which_python=`which python`
set -e # forbid errors
__myinfo__ "which python: $which_python"
if [ "$which_python" == "" ]; then
__myfail__ 'python has *not* been found in the PATH'
exit 2
else
__myinfo__ 'python has been found in the PATH.'
fi
# check conda
set +e # forgive errors
which_conda=`which conda`
set -e # forbid errors
__myinfo__ "which conda: $which_conda"
if [ "$which_conda" == "" ]; then
__myfail__ 'conda has *not* been found in the PATH'
exit 3
else
__myinfo__ 'conda has been found in the PATH.'
fi
# conda root
tmp=`dirname $which_conda`
conda_root=`dirname $tmp`
if [ $verbose -eq 1 ]; then
__myinfo__ "conda root: $conda_root"
fi
# check python is conda's python
tmp=${which_python/${conda_root}/}
if [ "$tmp" == "$which_python" ]; then
__myfail__ "python does *not* belong to the (same) conda distribution."
exit 4
else
__myinfo__ "python belongs to the same conda distribution."
fi
# check pip
if [ $assert_pip -eq 1 ]; then
which_pip=`which pip`
tmp=${which_pip/${conda_root}/}
if [ "$tmp" == "$which_pip" ]; then
__myfail__ "pip does *not* belong to the (same) conda distribution."
exit 5
else
__myinfo__ "pip belongs to the same conda distribution."
fi
fi
# check pip3
if [ $assert_pip3 -eq 1 ]; then
which_pip3=`which pip3`
tmp=${which_pip3/${conda_root}/}
if [ "$tmp" == "$which_pip3" ]; then
__myfail__ "pip3 does *not* belong to the (same) conda distribution."
exit 5
else
__myinfo__ "pip3 belongs to the same conda distribution."
fi
fi
exit 0
}
# ok with multiple quoted arguments
__main__ "$@"
exit 0