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

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