archrepo/update-repo.sh

274 lines
7.0 KiB
Bash
Raw Normal View History

2022-02-13 12:18:26 +01:00
#!/bin/bash
# The purpose of this script is to automate the maintenance of a custom pacman (Arch Linux) repository, whose goal is to
# provide package-ready AUR softs.
# This script should NOT be run as root.
# Dependencies
# Make sure that AWK rules file is present, readable and correctly set in this script's parameters
# Make sure the following packages are installed before running this script:
# package-query aurutils git
2022-02-14 22:26:48 +01:00
2022-02-13 12:18:26 +01:00
# Functions
##################################################################
ascii_frag() {
expr match "$1" "\([^[:digit:]]*\)"
}
ascii_remainder() {
expr match "$1" "[^[:digit:]]*\(.*\)"
}
numeric_frag() {
expr match "$1" "\([[:digit:]]*\)"
}
numeric_remainder() {
expr match "$1" "[[:digit:]]*\(.*\)"
}
# return 1 for $1 > $2
# return 2 for $1 < $2
# return 0 for equal
vercomp() {
local WORK1="$1"
local WORK2="$2"
local NUM1="", NUM2="", ASCII1="", ASCII2=""
while true; do
ASCII1=`ascii_frag "${WORK1}"`
ASCII2=`ascii_frag "${WORK2}"`
WORK1=`ascii_remainder "${WORK1}"`
WORK2=`ascii_remainder "${WORK2}"`
if [ "${ASCII1}" \> "${ASCII2}" ]; then
return 1
elif [ "${ASCII1}" \< "${ASCII2}" ]; then
return 2
fi
NUM1=`numeric_frag "${WORK1}"`
NUM2=`numeric_frag "${WORK2}"`
WORK1=`numeric_remainder "${WORK1}"`
WORK2=`numeric_remainder "${WORK2}"`
if [ -z "${NUM1}" -a -z "${NUM2}" ]; then
return 0
elif [ -z "${NUM1}" -a -n "${NUM2}" ]; then
return 2
elif [ -n "${NUM1}" -a -z "${NUM2}" ]; then
return 1
fi
if [ "${NUM1}" -gt "${NUM2}" ]; then
return 1
elif [ "${NUM1}" -lt "${NUM2}" ]; then
return 2
fi
done
}
##################################################################
init() {
# Only run if the user is not root
if [[ $USER = 'root' ]] ; then
echo "You cannot run this script as root!"
exit 1
fi
if [ -n "$(ls $run_directory_path)" ] ; then
echo "The running directory is not empty!"
exit 1
fi
2022-02-14 22:26:48 +01:00
source ../variables.conf
eval "$(ssh-agent)"
ssh-add $ssh_key
2022-02-13 12:18:26 +01:00
# TODO Check if required packages are installed
echo "ArchRepo update script"
}
checkUpdates() {
echo "Checking for updates:"
upgraded_packages=""
2022-02-14 22:26:48 +01:00
scp $ssh_options $remote_repository/$repo_db_file $remote_repository/$repo_db_filesfile $run_directory_path/
# scp $ssh_options $remote_repository/$repo_db_file $run_directory_path/
# cp $run_directory_path/../$repo_db_filesfile $run_directory_path/
2022-02-13 15:17:55 +01:00
stream=$(tar xOf $run_directory_path/$repo_db_file --wildcards */desc | awk -f $awk_rules_file)
2022-02-13 12:18:26 +01:00
OIFS=$IFS
IFS='|'
stream="${stream:1}"
for package in $stream
do
IFS='
'
count=0
for line in $package; do
if [ $count -eq 0 ]; then
filename=$line
elif [ $count -eq 1 ]; then
name=$line
elif [ $count -eq 2 ]; then
version=$line
fi
((count++))
done
reg="(linux-lts).+-((docs)|(headers))"
if [[ $name =~ $reg ]]; then
echo -e "\nSkipping $name ($version, default exception)."
continue 1
fi
if echo $exlist | grep -w $name > /dev/null; then
echo -e "\nSkipping $name ($version, temporary exception)."
continue 1
fi
echo -e "\nChecking $name ($version)..."
aur_version=$(package-query -A -f %v $name)
vercomp $aur_version $version
result=$?
if [[ $result -eq 1 ]]; then
echo "New version for $name: $version -> $aur_version"
2022-02-14 22:26:48 +01:00
IFS=$OIFS
2022-02-13 12:18:26 +01:00
createPackage $name
addPackageToRepo $name $aur_version
upgraded_packages="$upgraded_packages $name"
fi
IFS='|'
done
IFS=$OIFS
echo "All packages processed."
if [[ $upgraded_packages != "" ]]; then
echo "The following packages were upgraded:"
echo $upgraded_packages
fi
}
addPackage() {
createPackage $1
addPackageToRepo $1
}
createPackage() { # Create package $1
echo "Fetching $1 from AUR..."
aur fetch $1 # TODO This could be replaced by a git clone or even a wget+tar thanks to package-query
echo "Making $1 package..."
cd $1
2022-02-14 22:26:48 +01:00
## if linux-lts{414,49} then download kernel source from local repo (saves ~100MB each time)
## WARNING this onlys works because we already have said sources AND because PKBUILD follow this particular line
## (not the same in 419 nor 54)
#reg="(linux-lts).+"
#if [[ $1 =~ $reg ]]; then
#sed -i -e "s/\"https:\/\/www.kernel.org\/pub\/linux\/kernel\/v4.x\/\${_srcname}.tar.xz\"/\"https:\/\/archlinux.kto.black\/resources\/\${_srcname}.tar.xz\"/g" ./PKGBUILD
#fi
2022-02-13 12:18:26 +01:00
makepkg -s --noconfirm --noprogressbar
cd $run_directory_path
}
2022-02-13 15:17:55 +01:00
# TODO: try manually: can we update db file before sending to remote repo?
2022-02-13 12:18:26 +01:00
addPackageToRepo() { # Add package $1
echo "Adding $1 to repository..."
2022-02-14 22:26:48 +01:00
scp $ssh_options $run_directory_path/$1/$1-$2*.pkg.tar.xz $remote_repository/
repo-add $repo_db_file $run_directory_path/$1/$1-$2*.pkg.tar.xz
scp $ssh_options $run_directory_path/$repo_db_file $run_directory_path/$repo_db_file.old $run_directory_path/$repo_db_filesfile $run_directory_path/$repo_db_filesfile.old $remote_repository/
2022-02-13 12:18:26 +01:00
reg="(linux-lts).+"
if [[ $name =~ $reg ]]; then
echo "Adding $1-headers and $1-docs to repository..."
2022-02-14 22:26:48 +01:00
repo-add $repo_db_file $run_directory_path/$1/$1-headers-$2*.pkg.tar.xz $run_directory_path/$1/$1-docs-$2*.pkg.tar.xz
scp $ssh_options $run_directory_path/$1/$1-headers-$2*.pkg.tar.xz $run_directory_path/$1/$1-docs-$2*.pkg.tar.xz $remote_repository/
2022-02-13 12:18:26 +01:00
fi
}
clean() {
echo "Cleaning directory..."
rm -rf $run_directory_path/*
echo "Done."
}
clearRepo() {
stream=$(tar xOf $repo_directory/$repo_db_file --wildcards */desc | awk -f $awk_rules_file)
OIFS=$IFS
IFS='|'
stream="${stream:1}"
current=""
for package in $stream
do
IFS='
'
count=0
for line in $package; do
if [ $count -eq 0 ]; then
current="$current $line"
else
continue
fi
((count++))
done
IFS='|'
done
IFS=$OIFS
cd $repo_directory
reg="^($repo_name.).+"
for filename in *; do
if [[ $filename =~ $reg || $filename = 'sync' || $filename = 'local' || $filename = 'resources' ]]; then
continue
fi
for package in $current; do
if [ $filename = $package ]; then
continue 2
fi
done
echo "Removing $filename..."
rm $filename
done
}
run_directory_path=$(pwd)
# Set parameters
repo_directory='/mnt/archrepo'
repo_name='kto'
repo_db_file=$repo_name.db.tar
2022-02-14 22:26:48 +01:00
repo_db_filesfile=$repo_name.files.tar
2022-02-13 12:18:26 +01:00
awk_rules_file='../parser.awk'
# Main process
if [ "$1" = "" ]; then
init
checkUpdates
elif [ "$1" = "add" ]; then
init
addPackage $2
elif [ "$1" = "clean" ]; then
clean
elif [ "$1" = "clear" ]; then
read -r -p "Are you sure? [y/N] " response
response=${response,,} # tolower
if [[ "$response" =~ ^(yes|y)$ ]]; then
clearRepo
else
echo "Cancelling... Perhaps did you mean 'clean', which empties the local build directory?"
exit 1
fi
else
exit 1
fi
exit 0