mirror of https://github.com/Chizi123/Arch-autobuild-repo.git

Joel Grunbaum
2020-11-30 9e5affc4b5def98e5e959d870168831fb1bc1a30
commit | author | age
8ea4ef 1 #!/bin/bash
f6854c 2 #A basic bash script to automate the building of arch packages
dc0641 3 # Usage: main.sh init|check|add|remove|build_all
8ea4ef 4
e6f0af 5 source $(dirname "$(realpath $0)")/vars.sh
8ea4ef 6
54808c 7 ERRORFILE=$(mktemp)
C 8 WAITLIST=$(mktemp)
9 WAITLIST_LCK=$(mktemp)
10
8ea4ef 11 #Helper for finding newest and oldest files
JG 12 #Sourced from stack overflow
f6854c 13 # Usage: newold_matching_file [n/o] [filename]
8ea4ef 14 function newold_matching_file
JG 15 {
7c4087 16     # Use ${1-} instead of $1 in case 'nounset' is set
JG 17     local -r glob_pattern=${2-}
8ea4ef 18
7c4087 19     # To avoid printing garbage if no files match the pattern, set
JG 20     # 'nullglob' if necessary
21     local -i need_to_unset_nullglob=0
22     if [[ ":$BASHOPTS:" != *:nullglob:* ]] ; then
23         shopt -s nullglob
24         need_to_unset_nullglob=1
25     fi
8ea4ef 26
7c4087 27     file=
JG 28     for f in $glob_pattern ; do
8ea4ef 29         if [ $1 == "n" ]; then
JG 30             [[ -z $f || $f -nt $_file ]] && file=$f
31         elif [ $1 == "o" ]; then
32             [[ -z $f || $f -ot $_file ]] && file=$f
33         fi
7c4087 34     done
8ea4ef 35
7c4087 36     # To avoid unexpected behaviour elsewhere, unset nullglob if it was
JG 37     # set by this function
38     (( need_to_unset_nullglob )) && shopt -u nullglob
8ea4ef 39
7c4087 40     # Use printf instead of echo in case the file name begins with '-'
JG 41     [[ -n $file ]] && printf '%s\n' "$file"
8ea4ef 42
7c4087 43     return 0
8ea4ef 44 }
JG 45
46 #Build latest version of a package
9837c8 47 # Usage: build_pkg [package name] [-f force]
8ea4ef 48 function build_pkg {
JG 49     #check if PKGBUILD has updated, don't rebuild if hasn't changed
b59712 50     if [[ -n $(git pull | grep 'Already up to date.') && -z $(grep 'pkgver() {' PKGBUILD) && -z "$2" ]]; then
8ea4ef 51         return 2
JG 52     fi
f6854c 53
JG 54     #make and force rebuild if is git package
9837c8 55     # Mictosoft fonts have problems with checksums and need a seperate argument
C 56     if [[ "$1" == "ttf-ms-win10" ||
57         "$1" == "ttf-office-2007-fonts" ||
58         "$1" == "ttf-ms-win8" ||
59         "$1" == "ttf-win7-fonts" ]]; then
cb04c8 60         makepkg -s --noconfirm $([[ $CLEAN == "Y" ]] && echo "-c") $([[ $SIGN == "Y" ]] && echo "--sign --key $KEY") $([[ "$2" == "-f" ]] && echo -f) --skipchecksums 2>&1
9837c8 61     else
e74aea 62         makepkg -s --noconfirm $([[ $CLEAN == "Y" ]] && echo "-c") $([[ $SIGN == "Y" ]] && echo "--sign --key $KEY") $([[ "$2" == "-f" ]] && echo -f) 2>&1
9837c8 63     fi
266025 64     if [[ $? != 0  && $? != 13 ]]; then
8ea4ef 65         #Register error
54808c 66         echo $1 >> $ERRORFILE
8ea4ef 67         return 1
JG 68     fi
69
2bcbc6 70     #Remove old packages from build directory
88a91e 71     source PKGBUILD
96e462 72     srcdir="$(pwd)/src"
37434c 73     if grep -q 'pkgver() {' PKGBUILD; then
JG 74         ver=$(pkgver)
75     else
76         ver=$pkgver
77     fi
266025 78     find . -mindepth 1 -maxdepth 1 -type f \( -name "*.pkg.tar.*" -o -name "*.src.tar.*" \) -not -name "*$ver-$pkgrel*" -delete
2bcbc6 79     
JG 80     #Get build artifact names from PKGBUILD and build artifacts
81     #Remove duplicates from the list
37434c 82     pkgs=()
5f18f4 83     ipkgs=()
88a91e 84     for i in ${pkgname[@]}; do
b2c2b5 85         #pkgs+=("$i-$pkgver-$pkgrel")
5f18f4 86         ipkgs+=($(find . -mindepth 1 -maxdepth 1 -type f \( -name "$i*.pkg.tar.*" -o -name "$i*.src.tar.*" \) -not -name "*.sig" | sed 's/^\.\///'))
88a91e 87     done
5f18f4 88     while read -r -d '' x; do pkgs+=("$x"); done < <(printf "%s\0" "${ipkgs[@]}" | sort -uz)
88a91e 89
94852c 90     # Weird exceptions
C 91     if [[ "$1" == "zoom" ]]; then
92         rm zoom*_orig*
93         for i in ${pkgs[@]}; do
94             if [ -z "${i##*_orig*}" ]; then
95                 pkgs=(${pkgs[@]/$i})
96             fi
97         done
98     fi
99
e72b38 100     #Move package to repodir and add to repo db
d4b7a7 101     #Dont change the database if rebuilt the same package at same release and version
5f18f4 102     flag=0
88a91e 103     for i in ${pkgs[@]}; do
e4347d 104         if [[ ! -f $REPODIR/$i ]]; then
5f18f4 105             flag=1
d4b7a7 106         fi
88a91e 107     done
5f18f4 108     if [[ $flag == 1 ]]; then
C 109         rm -f $REPODIR/*$1*.tar.*
110         for i in ${pkgs[@]}; do
111             cp $i $REPODIR/
112             [[ "$SIGN" == "Y" ]] && cp $i.sig $REPODIR/
113         done
114     else 
115         return;
9837c8 116     fi
C 117
e72b38 118     # Add package to waiting list to be added to repo db
9837c8 119     while true; do
54808c 120         if [[ $(cat $WAITLIST_LCK) == 1 ]]; then
e6f0af 121             sleep 1
4ebf3d 122         else
54808c 123             echo 1 > $WAITLIST_LCK
C 124             echo $1 >> $WAITLIST
125             echo 0 > $WAITLIST_LCK
e6f0af 126             break
C 127             fi
128     done
9837c8 129     while true; do
e72b38 130         # Wait until package is at the top of the queue and add to db
54808c 131         if [[ "$(head -n1 $WAITLIST)" == "$1" ]]; then
5f18f4 132         #    for i in ${pkgs[@]}; do
C 133                 repo-add $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) ${pkgs[@]}
134         #    done
9837c8 135             while true; do
54808c 136                 if [[ $(cat $WAITLIST_LCK) == 1 ]]; then
e6f0af 137                     sleep 1
4ebf3d 138                 else
e72b38 139                     # Remove self from top of queue
5e843f 140                     echo 1 > $WAITLIST_LCK
54808c 141                     TEMP=$(mktemp)
C 142                     tail -n +2 $WAITLIST > $TEMP
143                     cp $TEMP $WAITLIST
144                     rm $TEMP
145                     unset TEMP
146                     echo 0 > $WAITLIST_LCK
e6f0af 147                     break
C 148                 fi
149             done
150             break
151         else
54808c 152             if [[ -z "$(grep $1 $WAITLIST)" ]]; then
b2c2b5 153                 # Not on waitlist for some reason, need to readd
54808c 154                 if [[ $(cat $WAITLIST_LCK) == 1 ]]; then
e74aea 155                     sleep 1
C 156                 else
54808c 157                     echo 1 > $WAITLIST_LCK
C 158                     echo $1 >> $WAITLIST
159                     echo 0 > $WAITLIST_LCK
e74aea 160                 fi
C 161             fi
e6f0af 162             sleep 10
4ebf3d 163         fi
e6f0af 164     done
8ea4ef 165
JG 166     #Remove old versions of packages
f6854c 167     #TODO: Want to be able to keep multiple versions of old packages, future work
JG 168     #Currently old package versions stay in the repodir indefinately
169     # while [ $NUM_OLD \< $(find . -name '*.pkg.tar.xz' | wc -l) ]
170     # do
7c4087 171     #    old=$(newold_matching_file o '*.pkg.tar.xz')
JG 172     #    rm $REPODIR/$old $old
f6854c 173     # done
8ea4ef 174     return 0
JG 175 }
176
177 #Update packages in BUILDDIR
f6854c 178 # Usage: build_all [-f force]
8ea4ef 179 function build_all {
JG 180     #system update
9837c8 181     if [[ $UPDATE == "Y" ]]; then
8ea4ef 182         sudo pacman -Syu --noconfirm
JG 183     fi
ac333f 184
8ea4ef 185     #update every package currently stored
c3d80e 186     for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -type d)
8ea4ef 187     do
JG 188         cd $d
9837c8 189         if [[ "$PARALLEL" == "Y" ]]; then
C 190             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")  &
191         else
192             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")
193         fi
8ea4ef 194     done
e6f0af 195     wait
8ea4ef 196
JG 197     return 0
198 }
199
200 #Add a new package to be built
7c4087 201 #Adding build dependencies is
f6854c 202 # Usage: add [package name]
8ea4ef 203 function add {
37434c 204     echo @: $@
JG 205 #    read
206     local i j k
67ef73 207     for i in $@; do
JG 208         cd $BUILDDIR
ca0d8a 209         if [[ -z $(git ls-remote https://aur.archlinux.org/$i.git) ]]; then
C 210             echo "Not a package"
211             exit 2
212         fi
67ef73 213         git clone https://aur.archlinux.org/$i.git
JG 214         cd $i
37434c 215         unset depends
JG 216         unset makedepends
217         local makedeps
218         source PKGBUILD
ca0d8a 219
C 220         #check for all build dependencies
37434c 221         for j in ${makedepends[@]}; do
JG 222             k=$(echo $j | sed 's/[>]=.*//g')
223             if ! pacman -Si $k; then
224                 makedeps+=($k)
ca0d8a 225             fi &>/dev/null
C 226         done
37434c 227         for j in ${depends[@]}; do
JG 228             k=$(echo $j | sed 's/[>]=.*//g')
229             if ! pacman -Si $k; then
230                 makedeps+=($k)
231             fi &>/dev/null
ca0d8a 232         done
37434c 233         echo $i: ${makedeps[@]}
JG 234 #        read
235         for j in ${makedeps[@]}; do
236             add $j
237 #            read
238         done
239         echo $i: ${makedeps[@]}
240 #        read
241         if [[ -n "${makedeps[@]}" ]]; then
3b9f12 242             sudo pacman -Sy
C 243         fi
37434c 244 #read
ca0d8a 245         #Actually build wanted package
37434c 246         cd $BUILDDIR/$i
9837c8 247         build_pkg $i -f
67ef73 248     done
8ea4ef 249     return 0
c3d80e 250 }
C 251
252 #Remove a package from the build list and repository
e6670f 253 #Usage of -a removes all packages moved to official repos
JG 254 # Usage remove [-a|package name]
c3d80e 255 function remove {
e6670f 256     if [[ "$1" == "-a" ]]; then
JG 257         rmlist=""
258         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq core | sort) | tr '\n' ' ')"
259         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq extra | sort) | tr '\n' ' ')"
260         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq community | sort) | tr '\n' ' ')"
261         for i in $rmlist; do
262             rm -rf $BUILDDIR/$i
263             repo-remove $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) $i
264             rm -f $REPODIR/*$i*
265         done
266     else
267         for i in $@; do
268             rm -rf $BUILDDIR/$i
269             repo-remove $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) $i
270             rm -f $REPODIR/*$i*
271         done
272     fi
8ea4ef 273 }
JG 274
7aa510 275 #Check for packages moved to official repos or removed from the AUR
C 276 function check {
277     rmlist=""
278     rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq core | sort) | tr '\n' ' ')"
279     rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq extra | sort) | tr '\n' ' ')"
280     rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq community | sort) | tr '\n' ' ')"
281     TMPFILE=$(mktemp)
282     for i in $(find $BUILDDIR -mindepth 1 -maxdepth 1 -type d); do
283         check_pkg $TMPFILE "$(echo $i | rev | cut -d'/' -f1 | rev)" &
284     done
285     wait
286     echo "Merged into official repos: $rmlist"
287     echo "Not in AUR: $(cat $TMPFILE | tr '\n' ' ')"
288     rm -f $TMPFILE
289 }
290
291 function check_pkg {
292     if [[ -z "$(curl -sI "https://aur.archlinux.org/packages/$2" | head -n1 | grep 200)" ]]; then
293         echo "$2" >> $1
294     fi
295 }
296
297
8ea4ef 298 #Check config and create build folders
f6854c 299 #Set variables before usage
JG 300 # Usage: init
8ea4ef 301 function init {
9837c8 302     if [[ $uid != 1 ]]; then
4ebf3d 303         echo "This must be run as root"
JG 304     fi
305
8ea4ef 306     #check for configuration here
9837c8 307     [[ -z $REPODIR ]] && echo "Enter REPODIR" && return 1
C 308     [[ -z $BUILDDIR ]] && echo "Enter BUILDDIR" && return 2
309     [[ -z $REPONAME ]] && echo "Enter REPONAME" && return 3
8ea4ef 310
JG 311     #make build directories
9837c8 312     [[ ! -d $REPODIR ]] && mkdir -p $REPODIR
C 313     [[ ! -d $BUILDDIR ]] && mkdir -p $BUILDDIR
8ea4ef 314
JG 315     #packages required to build others
4ebf3d 316     pacman -S --noconfirm base-devel git
8ea4ef 317
JG 318     #add repo to pacman.conf so can install own packages
9837c8 319     if [[ -z $(grep "$REPONAME" /etc/pacman.conf) ]]; then
8ea4ef 320         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
JG 321     fi
322
323     #create GPG key for package signing
4ebf3d 324     if [[ "$SIGN" == "Y" && "$KEY" == "" ]]; then
8ea4ef 325         (
JG 326             echo "Key-Type: RSA"
327             echo "Key-Length: 2048"
328             echo "Subkey-Type: RSA"
329             echo "Subkey-Length: 2048"
330             echo "Passphrase: \"\""
331             echo "Expire-Date: 0"
332             echo "Name-Real: John Doe"
333             echo "Name-Comment: Arch buildbot"
334             echo "Name-Email: $(whoami)@localhost"
335             echo "%commit"
336         ) | gpg --batch --generate-key
337         gpg --export --output $REPONAME.key --armor "John Doe"
338         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
339         echo "Please change the key information in this file"
340     fi
341
342     return 0
343 }
344
86d762 345 function send_email {
C 346     (
347     echo "From: build@localhost"
348     echo "To: $EMAIL"
349     echo "Subject: Build errors"
e6f0af 350     echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
be70fd 351     echo "The errors were: $@"
86d762 352     ) | sendmail -t
C 353 }
354
8ea4ef 355 case $1 in
JG 356     "init")
357         init;;
358     "add")
9837c8 359         add ${@:2};;
8ea4ef 360     "build-all")
9837c8 361         build_all $([[ "$2" == "-f" ]] && echo "-f");;
c3d80e 362     "remove")
9837c8 363         remove ${@:2};;
7aa510 364     "check")
C 365         check;;
8ea4ef 366     *)
dc0641 367         echo -e "\033[0;31mInvalid usage\033[0m"
C 368         echo -e "Usage: $0 init|check|add|remove|build-all"
369         echo -e "\033[0;32minit\033[0m                        - initialise repository for use"
370         echo -e "\033[0;32mcheck\033[0m                       - check if packages have been moved into the official repositories or removed from the AUR"
371         echo -e "\033[0;32madd package ...\033[0m             - add a package to \$BUILDDIR and repository, also used to rebuild failed packages"
372         echo -e "\033[0;32mremove -a | package ...\033[0m     - remove package from \$BUILDDIR and repository, \"-a\" removes packages added to official repos"
373         echo -e "\033[0;32mbuild-all [-f]\033[0m              - build all packages in \$BUILDDIR, \"-f\" force builds whole repository"
8ea4ef 374 esac
9837c8 375
C 376 # Error reporting, send email only for build-all as assuming an batch job for that
7aa510 377 if [[ $1 == "build-all" || $1 == "add" ]]; then
54808c 378     if [[ -n $(cat $ERRORFILE) ]]; then
C 379         ERRORS=$(cat $ERRORFILE | tr '\n' ' ')
7aa510 380         echo "Errors in packages: $ERRORS"
C 381         if [[ "$EMAIL" != "" && "$1" == "build-all" ]]; then
382             send_email $ERRORS
383         fi
384     else
385         echo "All packages built successfully"
9837c8 386     fi
C 387 fi
54808c 388
C 389 rm $ERRORFILE $WAITLIST $WAITLIST_LCK