-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.sh
196 lines (170 loc) · 3.98 KB
/
functions.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
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
###############################################################################
# Script Name : functions.sh
# Description : Functions for use in `.dofiles` scripts
# Args : None
# Author : Doug Emery
# Email : [email protected]
###############################################################################
##
# Set the variable `cmd` to the running script; to be used in messages
if basename "$0" >/dev/null 2>&1
then
cmd=$(basename $0)
else
cmd=$(basename $SHELL)
fi
##
# Alias to create a filename-suitable timestamp. Add the following to your
# script **before** sourcing this file to make the alias available in to your
# script.
#
#
# ```bash
# shopt -s expand_aliases
# source $(dirname $0)/functions.sh
# ```
alias tstamp="date +%Y%m%dT%H%M%S"
##
# Print a message preceded by the name of the current command
msg() {
echo "[$cmd] $1"
}
##
# Print an ERROR message and quit with an error status (`1`)
error() {
msg "ERROR: $1"
exit 1
}
warning() {
msg "WARNING: $1"
}
error_no_exit() {
msg "ERROR: $1"
}
##
# See if the named application is installed (MacOS)
is_installed() {
ii_application="$1"
mdfind "kMDItemKind == 'Application'" | grep -q "$ii_application"
status=$?
return $status
}
##
# Return success status if the application is is running; error status
# otherwise.
#
# argument: the search string for the running application
#
is_running() {
ir_application="$1"
ps -ef | sed /grep/d | grep -q "${ir_application}"
status=$?
return $status
}
homebrew_bin_dir() {
if ! which -s brew
then
error_no_exit "Homebrew not installed or not in PATH; homebrew prefix not found"
return 1
fi
dirname $(which brew)
}
brew_install_python() {
if ! homebrew_bin_dir
then
return 1
else
# something like [email protected]
bip_python_keg=$(brew search /^python@3/ | gsort -Vr | head -1)
fi
PATH=$(homebrew_bin_dir):$PATH
if [[ -n "${bip_python_keg}" ]]
then
# the python executable for [email protected] will be python3.11
bip_python=$(echo ${bip_python_keg} | awk -F '@' '{ print $1 $2 }')
if which -s ${bip_python}
then
msg "Python already installed: $(which ${bip_python})"
else
brew install ${bip_python_keg}
fi
else
error_no_exit "Unable to find hombrew package for any /^python@3/"
return 1
fi
}
update_pyenv() {
if ! which -s brew
then
error_no_exit "Homebrew not found; please install homebrew or add it to your path"
return 1
fi
brew upgrade pyenv
}
latest_pyenv_version() {
if ! which -s pyenv
then
error_no_exit "PYENV not installed"
return 1
fi
if update_pyenv
then
python_version=$(pyenv install --list | egrep "^\s+\d\.\d+\.\d+$" | tail -1)
echo ${python_version}
else
return 1
fi
}
pyenv_install_python() {
python_version=$(latest_pyenv_version)
if [[ -z "${python_version}" ]]
then
error_no_exit "Unable to get latest python version from pyenv"
return 1
fi
if pyenv versions | grep -q ${python_version}
then
msg "Python version ${python_version} already installed"
else
msg "Installing Python version ${python_version}"
pyenv install ${python_version}
fi
}
##
# Find the latest pip3.x
find_pip()3x {
bin_dir=$(homebrew_bin_dir)
if [[ -n "${bin_dir}" ]]
then
fp_pip3x=$(ls ${bin_dir}/pip3.* | gsort -rV | head -1)
if [[ -n "${fp_pip3x}" ]]
then
echo ${fp_pip3x}
else
error_no_exit "Unable to find pip3.x in ${bin_dir}"
return 1
fi
else
error_no_exit "Unable to find Homebrew bin directory"
return 1
fi
}
##
# Find the latest python3.x
find_python()3x {
bin_dir=$(homebrew_bin_dir)
if [[ -n "${bin_dir}" ]]
then
fp_python3x=$(ls ${bin_dir}/python3.* | grep "[0-9]$" | gsort -rV | head -1)
if [[ -n "${fp_python3x}" ]]
then
echo ${fp_python3x}
else
error_no_exit "Unable to find python3.x in ${bin_dir}"
return 1
fi
else
error_no_exit "Unable to find Homebrew bin directory"
return 1
fi
}