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

Joel Grunbaum
2020-11-29 2bcbc6ecf97c003e53bfae3d4abab0e8c15d12b8
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"
C 73     ver=$(pkgver)
266025 74     find . -mindepth 1 -maxdepth 1 -type f \( -name "*.pkg.tar.*" -o -name "*.src.tar.*" \) -not -name "*$ver-$pkgrel*" -delete
2bcbc6 75     
JG 76     #Get build artifact names from PKGBUILD and build artifacts
77     #Remove duplicates from the list
5f18f4 78     ipkgs=()
88a91e 79     for i in ${pkgname[@]}; do
b2c2b5 80         #pkgs+=("$i-$pkgver-$pkgrel")
5f18f4 81         ipkgs+=($(find . -mindepth 1 -maxdepth 1 -type f \( -name "$i*.pkg.tar.*" -o -name "$i*.src.tar.*" \) -not -name "*.sig" | sed 's/^\.\///'))
88a91e 82     done
5f18f4 83     while read -r -d '' x; do pkgs+=("$x"); done < <(printf "%s\0" "${ipkgs[@]}" | sort -uz)
88a91e 84
94852c 85     # Weird exceptions
C 86     if [[ "$1" == "zoom" ]]; then
87         rm zoom*_orig*
88         for i in ${pkgs[@]}; do
89             if [ -z "${i##*_orig*}" ]; then
90                 pkgs=(${pkgs[@]/$i})
91             fi
92         done
93     fi
94
e72b38 95     #Move package to repodir and add to repo db
d4b7a7 96     #Dont change the database if rebuilt the same package at same release and version
5f18f4 97     flag=0
88a91e 98     for i in ${pkgs[@]}; do
e4347d 99         if [[ ! -f $REPODIR/$i ]]; then
5f18f4 100             flag=1
d4b7a7 101         fi
88a91e 102     done
5f18f4 103     if [[ $flag == 1 ]]; then
C 104         rm -f $REPODIR/*$1*.tar.*
105         for i in ${pkgs[@]}; do
106             cp $i $REPODIR/
107             [[ "$SIGN" == "Y" ]] && cp $i.sig $REPODIR/
108         done
109     else 
110         return;
9837c8 111     fi
C 112
e72b38 113     # Add package to waiting list to be added to repo db
9837c8 114     while true; do
54808c 115         if [[ $(cat $WAITLIST_LCK) == 1 ]]; then
e6f0af 116             sleep 1
4ebf3d 117         else
54808c 118             echo 1 > $WAITLIST_LCK
C 119             echo $1 >> $WAITLIST
120             echo 0 > $WAITLIST_LCK
e6f0af 121             break
C 122             fi
123     done
9837c8 124     while true; do
e72b38 125         # Wait until package is at the top of the queue and add to db
54808c 126         if [[ "$(head -n1 $WAITLIST)" == "$1" ]]; then
5f18f4 127         #    for i in ${pkgs[@]}; do
C 128                 repo-add $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) ${pkgs[@]}
129         #    done
9837c8 130             while true; do
54808c 131                 if [[ $(cat $WAITLIST_LCK) == 1 ]]; then
e6f0af 132                     sleep 1
4ebf3d 133                 else
e72b38 134                     # Remove self from top of queue
5e843f 135                     echo 1 > $WAITLIST_LCK
54808c 136                     TEMP=$(mktemp)
C 137                     tail -n +2 $WAITLIST > $TEMP
138                     cp $TEMP $WAITLIST
139                     rm $TEMP
140                     unset TEMP
141                     echo 0 > $WAITLIST_LCK
e6f0af 142                     break
C 143                 fi
144             done
145             break
146         else
54808c 147             if [[ -z "$(grep $1 $WAITLIST)" ]]; then
b2c2b5 148                 # Not on waitlist for some reason, need to readd
54808c 149                 if [[ $(cat $WAITLIST_LCK) == 1 ]]; then
e74aea 150                     sleep 1
C 151                 else
54808c 152                     echo 1 > $WAITLIST_LCK
C 153                     echo $1 >> $WAITLIST
154                     echo 0 > $WAITLIST_LCK
e74aea 155                 fi
C 156             fi
e6f0af 157             sleep 10
4ebf3d 158         fi
e6f0af 159     done
8ea4ef 160
JG 161     #Remove old versions of packages
f6854c 162     #TODO: Want to be able to keep multiple versions of old packages, future work
JG 163     #Currently old package versions stay in the repodir indefinately
164     # while [ $NUM_OLD \< $(find . -name '*.pkg.tar.xz' | wc -l) ]
165     # do
7c4087 166     #    old=$(newold_matching_file o '*.pkg.tar.xz')
JG 167     #    rm $REPODIR/$old $old
f6854c 168     # done
8ea4ef 169     return 0
JG 170 }
171
172 #Update packages in BUILDDIR
f6854c 173 # Usage: build_all [-f force]
8ea4ef 174 function build_all {
JG 175     #system update
9837c8 176     if [[ $UPDATE == "Y" ]]; then
8ea4ef 177         sudo pacman -Syu --noconfirm
JG 178     fi
ac333f 179
8ea4ef 180     #update every package currently stored
c3d80e 181     for d in $(find $BUILDDIR -maxdepth 1 -mindepth 1 -type d)
8ea4ef 182     do
JG 183         cd $d
9837c8 184         if [[ "$PARALLEL" == "Y" ]]; then
C 185             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")  &
186         else
187             build_pkg $(echo $d | rev | cut -d'/' -f1 | rev) $1 &> $([[ "$QUIET" == "Y" ]] && echo "/dev/null" || echo "/dev/tty")
188         fi
8ea4ef 189     done
e6f0af 190     wait
8ea4ef 191
JG 192     return 0
193 }
194
195 #Add a new package to be built
7c4087 196 #Adding build dependencies is
f6854c 197 # Usage: add [package name]
8ea4ef 198 function add {
67ef73 199     for i in $@; do
JG 200         cd $BUILDDIR
ca0d8a 201         if [[ -z $(git ls-remote https://aur.archlinux.org/$i.git) ]]; then
C 202             echo "Not a package"
203             exit 2
204         fi
67ef73 205         git clone https://aur.archlinux.org/$i.git
JG 206         cd $i
ca0d8a 207
C 208         #check for all build dependencies
209         for i in ${makedepends[@]}; do
7c4087 210             if pacman -Si $i; then
ca0d8a 211                 makedepends=${makedepends[@]/$delete}
C 212             fi &>/dev/null
213         done
214         for i in ${makedepends[@]}; do
215             add $i
216         done
3b9f12 217         if [[ -n "${makedepends[@]}" ]]; then
C 218             sudo pacman -Sy
219         fi
220
ca0d8a 221         #Actually build wanted package
9837c8 222         build_pkg $i -f
67ef73 223     done
8ea4ef 224     return 0
c3d80e 225 }
C 226
227 #Remove a package from the build list and repository
e6670f 228 #Usage of -a removes all packages moved to official repos
JG 229 # Usage remove [-a|package name]
c3d80e 230 function remove {
e6670f 231     if [[ "$1" == "-a" ]]; then
JG 232         rmlist=""
233         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq core | sort) | tr '\n' ' ')"
234         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq extra | sort) | tr '\n' ' ')"
235         rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq community | sort) | tr '\n' ' ')"
236         for i in $rmlist; do
237             rm -rf $BUILDDIR/$i
238             repo-remove $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) $i
239             rm -f $REPODIR/*$i*
240         done
241     else
242         for i in $@; do
243             rm -rf $BUILDDIR/$i
244             repo-remove $([[ "$SIGN" == "Y" ]] && echo "--sign --key $KEY") $REPODIR/$REPONAME.db.tar.$([ -n "$COMPRESSION" ] || echo $COMPRESSION && echo zst) $i
245             rm -f $REPODIR/*$i*
246         done
247     fi
8ea4ef 248 }
JG 249
7aa510 250 #Check for packages moved to official repos or removed from the AUR
C 251 function check {
252     rmlist=""
253     rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq core | sort) | tr '\n' ' ')"
254     rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq extra | sort) | tr '\n' ' ')"
255     rmlist="$rmlist $(comm -12 <(pacman -Slq $REPONAME | sort) <(pacman -Slq community | sort) | tr '\n' ' ')"
256     TMPFILE=$(mktemp)
257     for i in $(find $BUILDDIR -mindepth 1 -maxdepth 1 -type d); do
258         check_pkg $TMPFILE "$(echo $i | rev | cut -d'/' -f1 | rev)" &
259     done
260     wait
261     echo "Merged into official repos: $rmlist"
262     echo "Not in AUR: $(cat $TMPFILE | tr '\n' ' ')"
263     rm -f $TMPFILE
264 }
265
266 function check_pkg {
267     if [[ -z "$(curl -sI "https://aur.archlinux.org/packages/$2" | head -n1 | grep 200)" ]]; then
268         echo "$2" >> $1
269     fi
270 }
271
272
8ea4ef 273 #Check config and create build folders
f6854c 274 #Set variables before usage
JG 275 # Usage: init
8ea4ef 276 function init {
9837c8 277     if [[ $uid != 1 ]]; then
4ebf3d 278         echo "This must be run as root"
JG 279     fi
280
8ea4ef 281     #check for configuration here
9837c8 282     [[ -z $REPODIR ]] && echo "Enter REPODIR" && return 1
C 283     [[ -z $BUILDDIR ]] && echo "Enter BUILDDIR" && return 2
284     [[ -z $REPONAME ]] && echo "Enter REPONAME" && return 3
8ea4ef 285
JG 286     #make build directories
9837c8 287     [[ ! -d $REPODIR ]] && mkdir -p $REPODIR
C 288     [[ ! -d $BUILDDIR ]] && mkdir -p $BUILDDIR
8ea4ef 289
JG 290     #packages required to build others
4ebf3d 291     pacman -S --noconfirm base-devel git
8ea4ef 292
JG 293     #add repo to pacman.conf so can install own packages
9837c8 294     if [[ -z $(grep "$REPONAME" /etc/pacman.conf) ]]; then
8ea4ef 295         printf "[$REPONAME]\nSigLevel = Optional TrustAll\nServer = file://$REPODIR\n" >> /etc/pacman.conf
JG 296     fi
297
298     #create GPG key for package signing
4ebf3d 299     if [[ "$SIGN" == "Y" && "$KEY" == "" ]]; then
8ea4ef 300         (
JG 301             echo "Key-Type: RSA"
302             echo "Key-Length: 2048"
303             echo "Subkey-Type: RSA"
304             echo "Subkey-Length: 2048"
305             echo "Passphrase: \"\""
306             echo "Expire-Date: 0"
307             echo "Name-Real: John Doe"
308             echo "Name-Comment: Arch buildbot"
309             echo "Name-Email: $(whoami)@localhost"
310             echo "%commit"
311         ) | gpg --batch --generate-key
312         gpg --export --output $REPONAME.key --armor "John Doe"
313         gpg --export-secret-keys --output $REPONAME.secret.key --armor "John Doe"
314         echo "Please change the key information in this file"
315     fi
316
317     return 0
318 }
319
86d762 320 function send_email {
C 321     (
322     echo "From: build@localhost"
323     echo "To: $EMAIL"
324     echo "Subject: Build errors"
e6f0af 325     echo "There were build errors for the build of $REPONAME at $(date), please address them soon."
be70fd 326     echo "The errors were: $@"
86d762 327     ) | sendmail -t
C 328 }
329
8ea4ef 330 case $1 in
JG 331     "init")
332         init;;
333     "add")
9837c8 334         add ${@:2};;
8ea4ef 335     "build-all")
9837c8 336         build_all $([[ "$2" == "-f" ]] && echo "-f");;
c3d80e 337     "remove")
9837c8 338         remove ${@:2};;
7aa510 339     "check")
C 340         check;;
8ea4ef 341     *)
dc0641 342         echo -e "\033[0;31mInvalid usage\033[0m"
C 343         echo -e "Usage: $0 init|check|add|remove|build-all"
344         echo -e "\033[0;32minit\033[0m                        - initialise repository for use"
345         echo -e "\033[0;32mcheck\033[0m                       - check if packages have been moved into the official repositories or removed from the AUR"
346         echo -e "\033[0;32madd package ...\033[0m             - add a package to \$BUILDDIR and repository, also used to rebuild failed packages"
347         echo -e "\033[0;32mremove -a | package ...\033[0m     - remove package from \$BUILDDIR and repository, \"-a\" removes packages added to official repos"
348         echo -e "\033[0;32mbuild-all [-f]\033[0m              - build all packages in \$BUILDDIR, \"-f\" force builds whole repository"
8ea4ef 349 esac
9837c8 350
C 351 # Error reporting, send email only for build-all as assuming an batch job for that
7aa510 352 if [[ $1 == "build-all" || $1 == "add" ]]; then
54808c 353     if [[ -n $(cat $ERRORFILE) ]]; then
C 354         ERRORS=$(cat $ERRORFILE | tr '\n' ' ')
7aa510 355         echo "Errors in packages: $ERRORS"
C 356         if [[ "$EMAIL" != "" && "$1" == "build-all" ]]; then
357             send_email $ERRORS
358         fi
359     else
360         echo "All packages built successfully"
9837c8 361     fi
C 362 fi
54808c 363
C 364 rm $ERRORFILE $WAITLIST $WAITLIST_LCK