-
Notifications
You must be signed in to change notification settings - Fork 44
/
common.functions
246 lines (218 loc) · 7.07 KB
/
common.functions
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
##
## Copyright (C) 2012 - 2019 XBian
##
## Find us at: http://www.xbian.org http://github.com/xbianonpi/xbian
##
## This Program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2, or (at your option)
## any later version.
##
## This Program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with XBMC; see the file COPYING. If not, see
## <http://www.gnu.org/licenses/>.
##
##
###### /var/jenkins is default home on Slave machines
echo ${WORKSPACE} | grep -q '^${JENKINS_HOME}\|^/var/jenkins\|^/var/lib/jenkins' || unset WORKSPACE
GIT_WRAPPER_DIR=/tmp/.gitbuilder
GIT_WRAPPER=$GIT_WRAPPER_DIR.git
LOCKDIR=dir.LOCK
kill_gitbuilder()
{
rm -fr $GIT_WRAPPER_DIR/.pid
rm -fr $GIT_WRAPPER_DIR/$LOCKDIR
}
trap "echo 'ERROR RETURNED from command'; kill_gitbuilder; exit 1" TERM
do_exit()
{
[ -n "$2" ] && result=$2
echo "$1 error $result"
kill_gitbuilder
exit $result
}
rexp()
{
[ ! -e "$1" ] && return
while read line; do
echo $line | grep -q '^[a-zA-Z]' || continue
vaname=$(echo "$line" | awk -F'=' '{print $1}')
vavalue=${line#*=}
[ -z "$(env | grep -w $vaname)" ] || vavalue="$vavalue "
case $vaname in
config_*)
export "$line"
;;
*)
eval export $vaname=\"$vavalue\$$vaname\"
;;
esac
done < "$1"
}
do_readconfig() {
dir="$1"
rexp "$dir/platforms" || :
if [ -e "$dir/config_pkgver" ]; then
rexp "$dir/config_pkgver"
else
echo "config_deb_version=" > "$dir/config_pkgver"
fi
[ -e "$XBIANROOT/config" ] && rexp $XBIANROOT/config || :
[ -e build/config ] && rexp build/config || :
[ -e "$dir/config" ] && rexp "$dir/config" || :
}
print_log() # level, message, ...
{
LEVEL=$1
shift 1
case $LEVEL in
(eme*)
test -n "$opt_syslog" && logger -t "$opt_prefix" -p daemon.emerge "$*" || :
echo Emergency: "$*" 1>&2 || :
;;
(ale*)
test -n "$opt_syslog" && logger -t "$opt_prefix" -p daemon.alert "$*" || :
echo Alert: "$*" 1>&2 || :
;;
(cri*)
test -n "$opt_syslog" && logger -t "$opt_prefix" -p daemon.crit "$*" || :
echo Critical: "$*" 1>&2 || :
;;
(err*)
test -n "$opt_syslog" && logger -t "$opt_prefix" -p daemon.err "$*" || :
echo Error: "$*" 1>&2 || :
;;
(war*)
test -n "$opt_syslog" && logger -t "$opt_prefix" -p daemon.warning "$*" || :
test -z "$opt_quiet" && echo Warning: "$*" 1>&2 || :
WARNING_COUNT=$(( $WARNING_COUNT + 1 ))
;;
(not*)
test -n "$opt_syslog" && logger -t "$opt_prefix" -p daemon.notice "$*" || :
test -z "$opt_quiet" && echo "$*" || :
;;
(inf*)
# test -n "$opt_syslog" && logger -t "$opt_prefix" -p daemon.info "$*" || :
test -n "$opt_verbose" && echo "$*" || :
;;
(deb*)
# test -n "$opt_syslog" && logger -t "$opt_prefix" -p daemon.debug "$*" || :
test -n "$opt_debug" && echo Debug: "$*" || :
;;
(*)
test -n "$opt_syslog" && logger -t "$opt_prefix" "$*" || :
echo "$*" 1>&2
;;
esac
}
do_run() # [argv]
{
set +e
if [ -n "$opt_dry_run" ]; then
echo "... Running $*"
RC="$?"
else
if [ "$use_silent" = yes ]; then
eval $* 1>/dev/null
else
eval $*
fi
RC="$?"
if [ "$RC" -eq '0' ]; then
print_log debug "$*"
else
print_log warning "$* returned $RC"
fi
fi
[ $RC -gt 0 ] && kill $$
set -e
return $RC
}
check_git()
{
if [ -n "$config_build_env" ] && echo $config_build_env | grep -q ^schroot; then
gitpath=$(eval $config_build_env which git) || :
gitlink=$(eval $config_build_env readlink -f $gitpath 2>/dev/null) || :
if [ "$gitlink" != "$GIT_WRAPPER" ]; then
[ -n "$gitpath" ] || gitpath='/usr/bin/git'
eval $config_build_env cp -f $gitpath $gitpath.old > /dev/null 2>&1 || :
eval $config_build_env rm -f $gitpath; eval $config_build_env ln -s $GIT_WRAPPER $gitpath || :
gitlink=$(eval $config_build_env readlink -f $gitpath 2>/dev/null) || :
if [ "$gitlink" != "$GIT_WRAPPER" ]; then
echo "!!!git inside buildroot must be symlink to $GIT_WRAPPER !!!"
kill_gitbuilder
exit 1
fi
fi
fi
}
git_preloader()
{
[ -z "$SCHROOT_CHROOT_NAME" ] || return 0
check_git
mkdir -p $GIT_WRAPPER_DIR
while ! mkdir $GIT_WRAPPER_DIR/.pid 2>/dev/null; do
echo "another process exists $(cat $GIT_WRAPPER_DIR/.pid/pid)"
echo "wait until process finishes or remove stale $GIT_WRAPPER_DIR/.pid"
sleep 5
done
while sleep 2; do
clones=$(find $GIT_WRAPPER_DIR -iname tmp.\*.new 2>/dev/null)
for f in $clones; do
set --
while read a; do
set -- "$@" "$a"
done < $f
rm -f $f
(
[ -z "$1" ] || cd "$1"; shift
/usr/bin/git "$@" > $f.out 2>&1 || :
)
mv $f.out $f.done || :
done
test -d $GIT_WRAPPER_DIR/$LOCKDIR && rmdir $GIT_WRAPPER_DIR/$LOCKDIR || :
[ -d $GIT_WRAPPER_DIR/.pid ] || break
done & >/dev/null 2>&1 pid=$!
echo $pid > $GIT_WRAPPER_DIR/.pid/pid
echo "#!/bin/sh
f=\$(mktemp -u --suffix=.new --tmpdir=$GIT_WRAPPER_DIR)
while ! mkdir $GIT_WRAPPER_DIR/$LOCKDIR 2> /dev/null ; do sleep 0.3; done
printf '%s\n' \$(pwd) > \$f
printf '%s\n' \"\${@}\" >> \$f
while ! test -e \$f.done; do
sleep 1
done
sleep 1
cat \$f.done
rm -f \$f.done
" > $GIT_WRAPPER
chmod +x $GIT_WRAPPER
return 0
}
do_hooks()
{
[ -n "$XBIANPKGDIR" -a -d "$XBIANPKGDIR/hooks.d" ] || return 0
[ -z "$XBIANARCHDIR" -o -z "$1" ] && exit 1
[ -d "$XBIANARCHDIR/working" ] || return 0
olddir=$(pwd)
preloader=''
[ "$1" = '##withbuildenv##' ] && preloader=$config_build_env && shift
if [ -e $XBIANPKGDIR/hooks.d/$1 ]; then
for f in $(ls $XBIANPKGDIR/hooks.d/$1); do
(
cd "$XBIANARCHDIR/working"
[ -n "$2" ] && rexp $XBIANPKGDIR/$2
echo "### Running hook $f from $1 ###"
do_run $preloader $SHELL -e $XBIANPKGDIR/hooks.d/$1/$f
[ $? -ne 0 ] && res=FAILED || res=OK
echo "### Running hook $f from $1 finished $res ###"
)
done
fi
cd $olddir
}