forked from vmware/likewise-open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
206 lines (182 loc) · 3.69 KB
/
common.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
#!/bin/bash
#
# This file should be sourced.
#
function warn
{
echo "$@" 1>&2
}
function exit_on_error
{
if [ $1 -ne 0 ]; then
if [ -n "$2" ]; then
warn "$2"
else
warn "Exiting on error $1"
fi
exit $1
fi
}
function check_arg
{
if [ -z "$2" ]; then
warn "Missing argument: $1"
exit 1
fi
}
function check_dir_arg
{
check_arg "$1" "$2"
if [ ! -d "$2" ]; then
warn "Missing dir: $2"
exit 1
fi
}
function exists_function
{
if [ "`type -t ${1}`" = "function" ]; then
return 0
else
return 1
fi
}
function get_extension
{
local base=`basename $1`
local ext=`echo "${base}" | perl -e '$_ = <>; s/^.*\.([^.]+)$/$1/; print $_;
'`
if [ -z "$ext" ]; then
warn "No extension found for \"$1\""
exit 1
fi
echo "$ext"
}
function get_rpm_nosignature_option
{
#
# Determine whether platform support --nosignature option
#
rpm --nosignature > /dev/null 2>&1
local rc=$?
if [ $rc -ne 0 ]; then
local RPM_NOSIGNATURE=""
else
local RPM_NOSIGNATURE="--nosignature"
fi
echo "${RPM_NOSIGNATURE}"
}
TIMER_START=0
TIMER_LABEL=""
function timer_start
{
TIMER_LABEL="$1"
TIMER_START=`perl -e 'print time'`
exit_on_error $?
}
function timer_stop
{
if [ "${TIMER_START}" != "0" ]; then
local ELAPSED=`perl -e "print time - $TIMER_START"`
echo ""
if [ -n "${TIMER_LABEL}" ]; then
echo "Elapsed Time ($TIMER_LABEL): $ELAPSED seconds"
else
echo "Elapsed Time: $ELAPSED seconds"
fi
echo ""
TIMER_START=0
TIMER_LABEL=""
fi
}
function set_ctrlc_trap
{
trap 'echo "CTRL-C pressed"; timer_stop ; exit_on_error 13' TERM INT
}
function time_command
{
#
# Execute the real build_loop in a subshell so we can time it even
# if it exits. Note that it is up the caller to make sure that
# set_ctrlc_trap has been called to get timing info on CTRL-C.
#
# Parameters:
#
# 1 - timer label
# 2 - command/function
#
# Returns:
#
# Return or exit code of command/function that was executed.
# That means that a function that exists will not return a code.
#
local _timer_label="$1"
local _timer_command="$2"
shift 2
check_arg _timer_label "${_timer_label}"
check_arg _timer_label "${_timer_command}"
timer_start "${_timer_label}"
( "${_timer_command}" "$@" )
local rc=$?
timer_stop
return $rc
}
function find_one_file
{
local spec="$1"
local files
files=`ls -t ${spec}`
exit_on_error $?
local count
count=`echo $files | wc -w`
exit_on_error $?
if [ $count -ne 1 ]; then
warn "Should have gotten only 1 file:"
warn " $files"
exit 1
fi
echo "$files"
}
function check_is_rhel_21
{
if [ -f /etc/redhat-release ]; then
grep "Red Hat Linux .* Server release 2.1" /etc/redhat-release
if [ $? -eq 0 ]; then
return 0
fi
fi
return 1
}
function get_var_value_from_text
{
# $1 - text
# $2 - var name
echo "$1" | sed -e /^"${2}"=/!d -e s/^"${2}"=// | tail -1
}
function optional_append_variable
{
# Given 3-4 params: <NAME> <true|false> <VALUE> [SEPARATOR]
# appends VALUE to variable NAME depending on true|false.
if [ -z "$1" ]; then
warn "Missising parameter 1 (variable name)."
return 1
fi
case "$2" in
true|false)
;;
*)
warn "Invalid parameter 2 (true or false): '$2'."
return 1
;;
esac
if "$2" ; then
if [ -n "${!1}" ]; then
if [ -n "$4" ]; then
eval "$1=\"${!1}$4$3\""
else
eval "$1=\"${!1} $3\""
fi
else
eval "$1=$3"
fi
fi
}