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

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