buildrepo.in 4.39 KB
Newer Older
Natanael Copa's avatar
Natanael Copa committed
1 2 3 4 5 6 7
#!/bin/sh

program=${0##*/}

aportsdir=${APORTSDIR:-$HOME/aports}
repodir=${REPODIR:-$HOME/packages}

8 9 10 11 12 13 14 15 16 17 18
if [ -f /etc/abuild.conf ]; then
	. /etc/abuild.conf
fi

if [ -z "$CARCH" ]; then
	machine=$(uname -m)
	case $machine in
	i[3-9]86) CARCH=x86;;
	*) CARCH=$machine;;
	esac
fi
Natanael Copa's avatar
Natanael Copa committed
19 20

usage() {
21 22
	echo "usage: $program [-a APORTSDIR] [-d REPODIR] [-hp] [-l LOGPREFIX ]"
	echo "                [-r DEPREPO] REPOSITORY..."
23

Natanael Copa's avatar
Natanael Copa committed
24 25 26 27 28
	echo "options:"
	echo " -a  Set the aports base dir to APORTSDIR instead of $aportsdir"
	echo " -d  Set destination repository base dir to REPODIR instead of $repodir"
	echo " -h  Show this help and exit"
	echo " -l  Send build to logfile, prefixed by LOGPREFIX"
29
	echo " -k  Keep going, even if packages fails"
Natanael Copa's avatar
Natanael Copa committed
30
	echo " -p  Purge obsolete packages from REPODIR after build"
31
	echo " -r  Dependencies are found in DEPREPO"
Natanael Copa's avatar
Natanael Copa committed
32 33 34
	exit 1
}

35 36 37 38 39 40 41 42 43 44 45 46
is_in() {
	local needle="$1" arg=
	shift
	for arg; do
		[ "$needle" = "$arg" ] && return 0
	done
	return 1
}

check_arch() {
	[ "$1" = "all" ] || [ "$1" = "noarch" ] || is_in "$CARCH" $@
}
Natanael Copa's avatar
Natanael Copa committed
47 48

listpackages() {
49 50
	local repo="$1" i= pkgname= pkgver= pkgrel= subpackage= arch= subpkg= linguas=
	local lang= suffix=
51
	cd "$aportsdir/$repo"
Natanael Copa's avatar
Natanael Copa committed
52
	for i in */APKBUILD; do
53 54 55
		cd "$aportsdir/$repo"/${i%/*} || return 1
		pkgname=
		pkgver=
56
		pkgrel=0
57 58
		subpackages=
		arch=
59
		linguas=
60
		. ./APKBUILD
61 62 63
		suffix="$pkgver-r$pkgrel.apk"

		if ! check_arch $arch; then
64 65 66
			continue
		fi
		for subpkg in $pkgname $subpackages; do
67 68 69 70
			echo ${subpkg%:*}-$suffix
		done
		for lang in $linguas; do
			echo $pkgname-lang-$lang-$suffix
71
		done
Natanael Copa's avatar
Natanael Copa committed
72 73 74
	done
}

75 76 77 78 79 80 81
all_exist() {
	while [ $# -gt 0 ]; do
		[ -e "$1" ] || return 1
		shift 1
	done
	return 0
}
82 83 84

list_needbuild() {
	local repo="$1" i=
Natanael Copa's avatar
Natanael Copa committed
85

86
	cd "$aportsdir/$repo" || return 0
Natanael Copa's avatar
Natanael Copa committed
87

88
	# first we try copy everything possible and find out which we need
89
	# to rebuild. By doing this we might save us for rebuilding
90
	# needed when running 'abuild -R'
91 92
	for apkbuild in */APKBUILD; do
		[ -f "$aportsdir/$repo/$apkbuild" ] || continue
93
		export REPODEST="$repodir"
94
		cd "$aportsdir/$repo"/${apkbuild%/*} || return 1
95 96
		pkgname=
		pkgver=
97
		pkgrel=0
98
		subpackages=
99
		arch=
100 101
		. ./APKBUILD

102
		if ! check_arch $arch; then
103 104 105
			continue
		fi

106 107 108 109 110 111 112 113
		pkgs=
		for subpkg in $pkgname $subpackages; do
			pkgfile=${subpkg%:*}-$pkgver-r$pkgrel.apk
			if ! [ -f "$REPODEST/$repo/$CARCH/$pkgfile" ]; then
				pkgs="$pkgs $pkgfile"
			fi
		done
		if [ -z "$pkgs" ]; then
114 115 116 117
			continue
		fi

		# try link or copy the files if they are in the ports dir
118
		if all_exist $pkgs; then
119
			echo ">>> Copying " $pkgs >&2
120 121
			cp -p -l $pkgs "$repodir/$repo/$CARCH"/ 2>/dev/null \
				|| cp -p $pkgs "$repodir/$repo/$CARCH"/ \
122
				|| echo "$apkbuild"
123
		else
124
			echo "$apkbuild"
125
		fi
Natanael Copa's avatar
Natanael Copa committed
126
	done
127 128 129 130 131 132
}

do_build() {
	local repo="$1" i=
	cd "$aportsdir/$repo" || return 0
	local needbuild="$(list_needbuild $repo)"
133

134
	export REPODEST="$repodir"
135
	export ABUILD_BLACKLIST
136 137 138 139
	# build the postponed packages if any
	if [ -n "$needbuild" ]; then
		for i in $needbuild; do
			cd "$aportsdir/$repo"/${i%/*} || return 1
140 141 142 143
			if ! abuild -k -R; then
				[ -n "$keep_going" ] || return 1
				ABUILD_BLACKLIST="$ABUILD_BLACKLIST $i"
			fi
144 145 146
		done
	fi

147
	# kill old packages in repo
148
	if [ -n "$dopurge" ]; then
149
		echo ">>> Removing old packages from $repo..."
150 151
		local tmp=$(mktemp /tmp/$program-XXXXXX)
		local purgefiles
152
		cd "$repodir/$repo/$CARCH" || return 1
153
		trap 'rm -f "$tmp"; exit 1' INT
154
		( listpackages "$1") >$tmp || return 1
155 156 157 158 159 160
		purge=$(ls *.apk 2>/dev/null | grep -v -w -f $tmp)
		if [ -n "$purge" ]; then
			rm -f $purge
		fi
		rm -f "$tmp"
	fi
161

162 163
	# generate the repository index
	echo ">>> Generating Index for $repo..."
164
	cd "$repodir/$repo/$CARCH"
165 166 167 168 169 170 171
	local deps
	for i in $deprepo; do
		deps="--repo $repodir/$i"
	done
	oldindex=
	if [ -f APKINDEX.tar.gz ]; then
		oldindex="--index APKINDEX.tar.gz"
Natanael Copa's avatar
Natanael Copa committed
172
	fi
173
	tmpindex=$(mktemp).tar.gz
174
	apk index --rewrite-arch $CARCH $oldindex -o $tmpindex \
175
		--description "$repo $(cd $aportsdir && git describe)" \
176
		-- *.apk
177
	abuild-sign $tmpindex && mv $tmpindex APKINDEX.tar.gz
178 179
	chmod 644 APKINDEX.tar.gz
	rm -f tmp.*
Natanael Copa's avatar
Natanael Copa committed
180 181
}

182
while getopts "a:d:hkl:pr:" opt; do
Natanael Copa's avatar
Natanael Copa committed
183 184 185 186
	case "$opt" in
		a) aportsdir=$OPTARG;;
		d) repodir=$OPTARG;;
		h) usage >&2;;
187
		k) keep_going=1;;
Natanael Copa's avatar
Natanael Copa committed
188 189
		l) logprefix=$OPTARG;;
		p) dopurge=1;;
190
		r) deprepo="$deprepo $OPTARG";;
Natanael Copa's avatar
Natanael Copa committed
191 192 193 194 195 196 197 198
	esac
done
shift $(($OPTIND - 1))

[ $# -eq 0 ] && usage >&2

while [ $# -gt 0 ]; do
	if [ -n "$logprefix" ]; then
199
		do_build $1  >$logprefix.$1.log 2>&1 || exit 1
Natanael Copa's avatar
Natanael Copa committed
200
	else
201
		do_build $1 || exit 1
Natanael Copa's avatar
Natanael Copa committed
202
	fi
203
	deprepo="$deprepo $1"
Natanael Copa's avatar
Natanael Copa committed
204 205 206
	shift
done