-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon-functions.sh
executable file
·134 lines (115 loc) · 2.33 KB
/
common-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
#!/bin/bash
#
# Common functions used in scripts.
#
# ensure the script is sourced
if [ "${BASH_SOURCE[0]}" -ef "$0" ]
then
echo "Hey, you should source this script, not execute it!"
exit 1
fi
#
# reporting
#
export RED='\033[1;31m'
export WHITE='\033[1;37m'
export YELLOW='\033[1;33m'
export NC='\033[0m'
if [ "$BATCH_MODE" == "yes" ] ; then
export ERROR="[error]"
export WARNING="[warning]"
export INFO="[info]"
export DEBUG_INFO="[debug]"
else
export ERROR="${RED}[error]${NC}"
export WARNING="${YELLOW}[warning]${NC}"
export INFO="${WHITE}[info]${NC}"
export DEBUG_INFO="${WHITE}[debug]${NC}"
fi
function error() {
echo -e "${ERROR} $@"
}
function warn() {
echo -e "${WARNING} $@"
}
function info() {
echo -e "${INFO} $@"
}
function info_n() {
echo -n -e "${INFO} $@"
}
function debug() {
if [ "${DEBUG}" == "yes" ] ; then
echo -e "${DEBUG_INFO} $@"
fi
}
#
# functions
#
# $1 = NAME, $2 = EXECUTABLE
function checkExecutable() {
if [ "$2" == "" ] ; then
error "$1 variable for executable not set."
exit 1
fi
if [ ! -x "$2" ] ; then
error "$1 not found at: $2"
exit 1
fi
}
# $1 = NAME, $2 = FILE
function checkFile() {
if [ "$2" == "" ] ; then
error "$1 variable for file not set."
exit 1
fi
if [ ! -f "$2" ] ; then
if [ "$3" == "clean" ] ; then
touch "$2"
else
error "$1 not found at: $2"
exit 1
fi
else
if [ "$3" == "clean" ] ; then
info "$1 recreated, now empty"
rm -f "$2"
touch "$2"
fi
fi
}
# $1 = NAME, $2 = FILE
function checkDirectory() {
if [ "$2" == "" ] ; then
error "$1 directory variable not set."
exit 1
fi
if [ ! -d "$2" ] ; then
if [ "$3" == "create" ] || [ "$3" == "recreate" ] ; then
info "$1: directory does not exist, creating it"
mkdir -p "$2"
else
error "$1: directory $2 does not exist."
exit 1
fi
else
if [ "$3" == "recreate" ] ; then
info "$1: exists, recreating it"
rm -rf "$2"
mkdir -p "$2"
fi
fi
}
#
# default constants
#
DATA_DIR="${BASE_DIR}/data"
MODEL_DIR="${BASE_DIR}/models"
MODEL_FILE_DIR="${MODEL_DIR}/file"
MODEL_MAP_DIR="${MODEL_DIR}/map"
MODEL_FILE_MAP_DIR="${MODEL_DIR}/file-map"
MODEL_FILE_INTERFACE_DIR="${MODEL_DIR}/file-interface"
MODEL_MAP_INTERFACE_DIR="${MODEL_DIR}/map-interface"
MODEL_FILE_MAP_INTERFACE_DIR="${MODEL_DIR}/file-map-interface"
MAP_FILE="${BASE_DIR}/module-file-function-map.csv"
# end