-
Notifications
You must be signed in to change notification settings - Fork 337
/
bash_utils.sh
157 lines (139 loc) · 3.71 KB
/
bash_utils.sh
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
#!/usr/bin/env bash
# a collection of bash utils
# ====================================================
function get_after_last_slash(){
ret=$(echo $1 | sed 's:.*/::')
echo $ret
}
function get_virtualenv_name(){
cmd_out=$(printenv | grep VIRTUAL_ENV)
virtual_env_name=$(get_after_last_slash $cmd_out)
echo $virtual_env_name
}
function print_blue(){
printf "\033[34;1m"
printf "$@ \n"
printf "\033[0m"
}
function make_dir(){
if [ ! -d $1 ]; then
mkdir -p $1
fi
}
function make_buid_dir(){
make_dir build
}
function check_package(){
package_name=$1
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $package_name |grep "install ok installed")
#echo "checking for $package_name: $PKG_OK"
if [ "" == "$PKG_OK" ]; then
#echo "$package_name is not installed"
echo 1
else
echo 0
fi
}
function install_package(){
do_install=$(check_package $1)
if [ $do_install -eq 1 ] ; then
sudo apt-get install -y $1
fi
}
function check_pip_package(){
package_name=$1
PKG_OK=$(python3 -m pip list |grep $package_name)
#echo "checking for $package_name: $PKG_OK"
if [ "" == "$PKG_OK" ]; then
#echo "$package_name is not installed"
echo 1
else
echo 0
fi
}
function check_pip_package2(){
package_name=$1
if python3 -c "import "$package_name &> /dev/null; then
echo 0
else
#echo "$package_name is not installed"
echo 1
fi
}
function install_pip_package(){
do_install=$(check_pip_package $1)
virtual_env=$(get_virtualenv_name)
if [ $do_install -eq 1 ] ; then
if [ "" == "$virtual_env" ]; then
pip3 install --user $1
else
pip3 install $1 # if you are in a virtual environment the option `--user` will install make pip3 install things outside the env
fi
fi
}
function install_pip_packages(){
for var in "$@"; do
install_pip_package "$var"
done
}
function set_git_modules() {
#print_blue "setting up git submodules"
git submodule init --
git submodule sync --recursive
git submodule update --init --recursive
}
function update_git_modules() {
git submodule update --recursive --remote
}
function pause(){
read -s -n 1 -p "Press any key to continue . . ."
echo ""
}
function check_conda(){
# check that conda is activated
if ! command -v conda &> /dev/null; then
echo "ERROR: conda could not be found! did you installed/activated conda?"
echo 1
else
echo 0
fi
}
function gdrive_download () {
if gdown -V >/dev/null 2>&1; then
echo "" #"gdown is found in PATH"
else
if [[ -f $HOME/.local/bin/gdown ]]; then
export PATH=$HOME/.local/bin:$PATH
fi
fi
gdown https://drive.google.com/uc?id=$1
}
function extract_version(){
#version=$(echo $1 | sed 's/[^0-9]*//g')
#version=$(echo $1 | sed 's/[[:alpha:]|(|[:space:]]//g')
version=$(echo $1 | sed 's/[[:alpha:]|(|[:space:]]//g' | sed s/://g)
echo $version
}
function get_usable_cuda_version(){
version="$1"
if [[ "$version" != *"cuda"* ]]; then
version="cuda-${version}"
fi
# check if we have two dots in the version, check if the folder exists otherwise remove last dot
if [[ $version =~ ^[a-zA-Z0-9-]+\.[0-9]+\.[0-9]+$ ]]; then
if [ ! -d /usr/local/$version ]; then
version="${version%.*}" # remove last dot
fi
fi
echo $version
}
function brew_install(){
if brew ls --versions $1 > /dev/null; then
# The package is installed
echo $1 is already installed!
else
# The package is not installed
brew install $1
fi
}
# ====================================================