-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuncs
More file actions
90 lines (78 loc) · 2.04 KB
/
Copy pathfuncs
File metadata and controls
90 lines (78 loc) · 2.04 KB
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
#!/bin/bash
# Read a Markdown document, that use `###` to describe its
# version number.
#
function read_app_version {
CONF=$1
if [ -f "${CONF}" ]; then
cat ${CONF} | grep '^###' | head -n 1 | awk '{print $2}'
else
echo "0.0.1"
fi
}
# The helper function to clean up unused comments and empty lines
# of given file (as 1st argument), and output to stdout.
#
function cat_conf_clearly {
cat $1 | sed -e 's/^ *//' | sed -e 's/ *$//' | sed 's/#.*$//g' | grep -v "^$"
}
# The helper function is to read any configuration file, and
# remove commented lines and blank lines.
#
# By default, the output is a TEXT with '\n' as delimiter,
# but the function also allows to replace '\n' with other
# preferred delimiter in 2nd parameter, e.g. '\t', ' '.
#
function read_conf {
FILENAME=$1
DELIMITER=$2
TMP=$(mktemp /tmp/XXXXXX)
cat_conf_clearly ${FILENAME} > ${TMP}
if [ "" == "${DELIMITER}" ]; then
cat ${TMP}
else
cat ${TMP} | tr '\n' '\t' | sed 's/\t$//g' | tr '\t' "${DELIMITER}"
fi
rm -f ${TMP}
}
# Check the required command-line tools before running
# the script.
#
function check_tool_prerequisites {
for p in "$@"; do
if [ "" == "$(which $p)" ]; then
ERR "check_tool_prerequisites: missing $p"
exit 1
else
DBG "check_tool_prerequisites: $(LIGHT_GREEN $p)"
fi
done
}
# Check the python2/python3 package is installed or not
#
function check_python_package {
local PYTHON="python"
local PIP="pip"
local MISSING=""
[ "true" == "${USING_PYTHON3}" ] && PYTHON="python3" && PIP="pip3"
for p in "$@"; do
INFO "checking python package: $(LIGHT_GREEN ${p})"
${PYTHON} -c "import ${p}" 2>&1 > /dev/null
[ "0" != "$?" ] && ERR "missing ${p}, please use pip to install: ${PIP} install ${p}" && MISSING="${MISSING} $p"
done
[ "" != "${MISSING}" ] && return 1
return 0
}
function RUN_CMD_STREAMING {
INFO "$(PURPLE $@)"
$@ 2>&1 | awk '{printf "\t\t%s\n", $0}'
}
function RUN_CMD {
TMP=$(mktemp /tmp/XXXXXX)
INFO "$(PURPLE $@)"
$@ > ${TMP} 2>&1
if [ "0" != "$?" ]; then
cat ${TMP} | awk '{printf "\t\t%s\n", $0}'
fi
rm -f ${TMP}
}