-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
230 lines (202 loc) · 5.13 KB
/
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
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
223
224
225
226
227
228
229
230
#
# Copyright (c) 2020-2023 AlertAvert.com. All rights reserved.
#
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Author: Marco Massenzio (marco@alertavert.com)
#
# Utility functions for shell scripting.
#
# Created M. Massenzio, 2015-09-01
# Updated: 2020-04-25
#
# Do not execute this script directly; `source` it instead from your own scripts.
# Prints the absolute path of the file or path; without arguments,
# it prints the absolute path of the current directory.
#
# Usage: abspath [FILE | PATH]
function abspath {
local pathname=${1:-$(pwd)}
local cmd=python3
if [[ ! -x $(which $cmd) ]]; then cmd=python; fi
echo $(${cmd} -c "import os; print(os.path.abspath(\"${pathname}\"))")
}
# Adds the given path to PATH
#
# Usage: addpath PATH
function addpath {
local -r dir="$1"
if [[ ! -d "${dir}" ]]; then
fatal "Cannot add non-existent directory (${dir}) to \$PATH"
return 1
fi
if [[ -z $(echo ${PATH} | grep ${dir}) ]]; then
export PATH=${dir}:${PATH}
fi
}
# Emits the current data in ISO-8960 format, truncated to the nearest minute, no TZ.
#
# Usage: now
function now {
echo $(date +"%Y-%m-%dT%H:%M")
}
# Shared logging method.
#
# Do NOT use directly, prefer using pre-defined level functions:
# `msg`, `errmsg`, `fatal` and `success`.
#
# Usage: log LVL MSG
function log {
local level=${1:-}
shift 1
local msg="$@"
echo "$(now) [${level}] ${msg}"
}
# Emits an INFO message.
#
# Usage: msg MSG1 MSG2 ...
function msg {
log "INFO" "$@"
}
# Emits an ERROR message
#
# Usage: errmsg MSG1 MSG2 ...
function errmsg {
log "ERROR" "$@"
}
# Emits a SUCCESS message
#
# Usage: success MSG
function success {
log "SUCCESS" "$@"
}
# Emits a FATAL message and exits with a non-zero error code.
#
# Uses the special $- argument to check whether the command is invoked
# from an interactive shell or from a script and will, respectively, either
# return a non-zero value, or exit altogether.
#
# Usage: fatal MSG
function fatal {
log "FATAL" "$@"
case "$-" in
*i*) return 1 ;;
*) exit 1 ;;
esac
}
# Wraps the output of the given CMD ($1) by checking the outcome and emitting
# an appropriate log level (INFO or ERROR) depending on the outcome.
#
# If the retcode from the invoked command is non-zero, it will exit.
#
# Usage: wrap CMD [ARGS...]
function wrap {
if [[ $# < 1 ]]; then
errmsg "$0 function must be invoked with at least one argument (the command to execute)"
return 1
fi
local CMD=$1
shift
OUT=$(${CMD} $@ 2>&1)
RES=$?
if [[ $RES != 0 ]]; then
fatal ${OUT}
return 1
fi
msg ${OUT}
return 0
}
# Customization of the `wrap` function for long-running commands that emit a lot
# of stdout: it will just check the exit code and emit the message passed as $1 if
# an error occurs; it will not capture stdout.
#
# Usage: wrap_no_out ERRMSG CMD [ARGS...]
function wrap_no_out {
if [[ $# -lt 2 ]]; then
errmsg "Usage: $0 ERRMSG CMD [ARGS...]"
return 1
fi
local MSG=$1
local CMD=$2
shift 2
msg "Executing ${CMD}..."
${CMD} $@
if [[ $? != 0 ]]; then
fatal ${MSG}
return 1
fi
return 0
}
# Kills the named process, if it exists
#
# Usage: killn PROC-NAME
function killn {
if [[ $# < 1 ]]; then
msg "Usage: killn PROC"
fatal "killn MUST be invoked with the name of the process to kill."
return 1
fi
PID=$(ps aux | grep -v grep | grep -i ${1} | cut -f 1 -d ' ')
if [[ -n ${PID} ]]; then
kill $PID
return $?
fi
errmsg "No Process named ${1} found"
return 1
}
# Looks up a filename in an optional subdirectory, or the current one.
#
# Usage: findfile [--dir DIR] FILE
function findfile {
source $(parse-args dir fname+ -- $@)
echo "dir: $dir fname: $fname" >&2
local where=${dir:-.}
find ${where} -name "${fname}" 2>/dev/null
}
# Creates and initializes a new Python Virtualenv
#
# Usage: newenv NAME
function newenv {
local envname=${1:-}
local reqsdir="${HOME}/.virtualenvs/requirements"
local reqs="${reqsdir}/${envname}.txt"
if [[ -z ${envname} ]]
then
fatal "Virtualenv NAME not provided"
return 1
fi
mkvirtualenv -p $(which python3) ${envname}
wrap_no_out "Failed to upgrade pip" pip install -U pip
if [[ -e ${reqs} ]]
then
wrap pip install -U -r ${reqs}
fi
success "Virtualenv ${envname} created"
}
# Inserts a pause in the progress, and an option to terminate early.
#
# Usage: should_continue
function should_continue {
if read -q "?Paused. Hit 'y' when ready to continue, or 'n' to terminate "; then
echo -e "\nOk, continuing...\n"
else
echo -e "\nAborting\n"
exit 1
fi
}
# Asks an optional question and returns a non-zero value if the user
# responds anything other than `y`.
#
# Should be used in an `if` statement:
#
# if ask "About to remove resource, continue?"; then
# ... do the deed
# fi
# Usage: ask [PROMPT]
function ask {
read -q "?${1:-Proceed?} [y/n] "
res=$?
echo -n "\n"
return $res
}