summaryrefslogtreecommitdiffstats
path: root/db-functions
blob: c4fc8f3de19a2b7946bd8fc23f5acbe27833e4f9 (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#!/bin/bash

# Useful functions
UMASK=""
set_umask () {
	[ "$UMASK" == "" ] && UMASK="$(umask)"
	export UMASK
	umask 002
}

restore_umask () {
	umask $UMASK >/dev/null
}

# set up general environment
WORKDIR=$(mktemp -d /tmp/$(basename $0).XXXXXXXXXX)
LOCKS=()

script_lock() {
	local LOCKDIR="$TMPDIR/.scriptlock.$(basename $0)"
	if ! mkdir "$LOCKDIR" >/dev/null 2>&1 ; then
		local _owner="$(/usr/bin/stat -c %U $LOCKDIR)"
		echo "Error: Script $(basename $0) is already locked by $_owner." >&2
		exit 1
	else
		set_umask
		return 0
	fi
}

script_unlock() {
	local LOCKDIR="$TMPDIR/.scriptlock.$(basename $0)"
	if [ ! -d "$LOCKDIR" ]; then
		echo "Warning: Script $(basename $0) was not locked!" >&2
		restore_umask
		return 1
	else
		rmdir "$LOCKDIR"
		restore_umask
		return 0
	fi
}

cleanup() {
	local l
	local repo
	local arch

	trap - EXIT INT QUIT TERM
	for l in ${LOCKS[@]}; do
		repo=${l%.*}
		arch=${l#*.}
		if [ -d "$TMPDIR/.repolock.$repo.$arch" ]; then
			echo "Removing left over lock from $repo/$arch" >&2
			repo_unlock $repo $arch
		fi
	done
	if [ -d "$TMPDIR/.scriptlock.$(basename $0)" ]; then
		echo "Removing left over lock from $(basename $0)" >&2
		script_unlock
	fi
	rm -rf "$WORKDIR"
	[ "$1" ] && exit $1
}

abort() {
	echo 'Aborting...' >&2
	cleanup 0
}

die() {
	echo "$*" >&2
	cleanup 1
}

trap abort INT QUIT TERM
trap cleanup EXIT


#repo_lock <repo-name> <arch> [timeout]
repo_lock () {
	local LOCKDIR="$TMPDIR/.repolock.$1.$2"
	local _count
	local _trial
	local _timeout
	local _lockblock
	local _owner

	if [ $# -eq 2 ]; then
		_lockblock=true
		_trial=0
	elif [ $# -eq 3 ]; then
		_lockblock=false
		_timeout=$3
		let _trial=$_timeout/$LOCK_DELAY
	fi

	_count=0
	while [ $_count -le $_trial ] || $_lockblock ; do
		if ! mkdir "$LOCKDIR" >/dev/null 2>&1 ; then
			_owner="$(/usr/bin/stat -c %U $LOCKDIR)"
			echo "Warning: Repo $1-$2 is already locked by $_owner. Retrying in $LOCK_DELAY seconds..." >&2
		else
			LOCKS[${#LOCKS[*]}]="$1.$2"
			set_umask
			return 0
		fi
		sleep $LOCK_DELAY
		let _count=$_count+1
	done

	echo "Error: Repo $1-$2 is already locked by $_owner. Giving up!" >&2
	return 1
}

repo_unlock () { #repo_unlock <repo-name> <arch>
	local LOCKDIR="$TMPDIR/.repolock.$1.$2"
	if [ ! -d "$LOCKDIR" ]; then
		echo "Warning: Repo lock $1-$2 was not locked!" >&2
		restore_umask
		return 1
	else
		rmdir "$LOCKDIR"
		restore_umask
		return 0
	fi
}

# usage: _grep_pkginfo pkgfile pattern
_grep_pkginfo() {
	local _ret

	_ret="$(/usr/bin/bsdtar -xOqf "$1" .PKGINFO | /bin/grep -m 1 -E "$2" | /bin/sed 's|\w*\s*=\s*\(.*\)|\1|')"
	echo "$_ret"
}


# Get the package base or name as fallback
getpkgbase() {
	local _base

	_base="$(_grep_pkginfo "$1" "^pkgbase")"
	if [ -z "$_base" ]; then
		getpkgname "$1"
	fi

	echo "$_base"
}

# Get the package name
getpkgname() {
	local _name

	_name="$(_grep_pkginfo "$1" "^pkgname")"
	if [ -z "$_name" ]; then
		echo "Error: Package '$1' has no pkgname in the PKGINFO. Fail!" >&2
		exit 1
	fi

	echo "$_name"
}

# Get the pkgver-pkgrel of this package
getpkgver() {
	local _ver

	_ver="$(_grep_pkginfo "$1" "^pkgver")"
	if [ -z "$_ver" ]; then
		echo "Error: Package '$1' has no pkgver in the PKGINFO. Fail!" >&2
		exit 1
	fi

	echo "$_ver"
}

getpkgfile() {
	if [[ ${#} -ne 1 ]]; then
		echo 'Error: No canonical package found!' >&2
		exit 1
	elif [ ! -f "${1}" ]; then
		echo "Error: Package ${1} not found!" >&2
		exit 1
	fi

	echo ${1}
}

getpkgfiles() {
	local f
	if [ ! -z "$(echo ${@%\.*} | sed "s/ /\n/g" | sort | uniq -D)" ]; then
		echo 'Error: Duplicate packages found!'>&2
		exit 1
	fi

	for f in ${@}; do
		if  [ ! -f "${f}" ]; then
			echo "Error: Package ${f} not found!" >&2
			exit 1
		fi
	done

	echo ${@}
}

#check_pkg_arch pkgfile arch
check_pkg_arch () {
	local _arch
	_arch="$(_grep_pkginfo "$1" "^arch")"

	if [ -z "$_arch" ]; then
		echo "Error: Package '$1' has no arch in the PKGINFO. Fail!" >&2
		return 1
	fi
	if [ "$_arch" = "$2" ]; then
		return 0
	else
		return 1
	fi
}

get_repos_for_host() {
	if [ "$(hostname)" = "sigurd" ]; then
		echo "community community-testing"
	else
		echo "core extra testing"
	fi
}

get_pkgpool_for_host() {
	if [ "$(hostname)" = "sigurd" ]; then
		echo "pool/community"
	else
		echo "pool/packages"
	fi
}

#usage: chk_license ${license[@]}"
chk_license() {
	local l
	local allowed
	for l in "$@"; do
		l="$(echo $l | tr '[:upper:]' '[:lower:]')"
		for allowed in ${ALLOWED_LICENSES[@]}; do
			allowed="$(echo $allowed | tr '[:upper:]' '[:lower:]')"
			if [ "$l" = "$allowed" ]; then
				return 0
			fi
		done
	done

	return 1
}

pkgname_from_src() {
	local tmp
	local a
	tmp=${1##*/}
	tmp=${tmp%$SRCEXT}
	for a in ${ARCHES[@]}; do
		tmp=${tmp%-$a}
	done
	tmp=${tmp%-any}
	echo ${tmp%-*-*}
}

pkgver_from_src() {
	local tmp
	local a
	tmp=${1##*/}
	tmp=${tmp%$SRCEXT}
	for a in ${ARCHES[@]}; do
		tmp=${tmp%-$a}
	done
	tmp=${tmp%-any}
	echo $tmp | sed 's|.*-\(.*-.*\)$|\1|g'
}