Add generic scripts to import patches and sync variants

This commit is contained in:
Martchus 2018-12-29 19:30:00 +01:00
parent 0d4177fc67
commit 1df7498ca9
5 changed files with 215 additions and 62 deletions

122
devel/import-patches.sh Executable file
View File

@ -0,0 +1,122 @@
#!/usr/bin/bash
# Imports patches from a Git repository to replace all currently present patches of a PKGBUILD
# setup shell environment
#set -euxo pipefail
set -e # abort on first error
shopt -s nullglob
source /usr/share/makepkg/util/message.sh
colorize
# read arguments
if [[ ! $1 ]] || [[ ! $2 ]] || [[ ! $3 ]]; then
echo 'Imports patches from a Git repository to replace all currently present patches of a PKGBUILD'
echo "usage: $0 path/to/PKGBUILD path/to/git-repo commit-range"
echo "example: $0 ~/pkgbuilds/some-project/default/PKGBUILD ~/repos/some-project master..fixes-for-arch-package"
echo "caveats: Only supports sha256sums (so far). Variables in other source lines than the first are not preserved."
exit -1
fi
pkgbuild_file=$1
pkgbuild_dir=${pkgbuild_file%/*}
git_repo_path=$2
git_commit_range=$3
if [[ ! -f $pkgbuild_file ]]; then
error "The specified PKGBUILD file \"$pkgbuild_file\" does not exist."
exit -1
fi
if [[ ! -d $git_repo_path ]]; then
error "The specified Git repository \"$git_repo_path\" does not exist."
exit -2
fi
msg "Sourcing $pkgbuild_file"
source "$pkgbuild_file"
msg "Determine sources/checksums to be preserved"
new_sources=()
new_sha256sums=()
file_index=0
for source in "${source[@]}"; do
# TODO: support other checksums than SHA-256
if [ "${source: -6}" != .patch ]; then
new_sources+=("$source")
new_sha256sums+=("${sha256sums[$file_index]}")
msg2 "$source; ${sha256sums[$file_index]}"
fi
file_index=$((file_index + 1))
done
msg "Removing old patches"
patches=("$pkgbuild_dir"/*.patch)
for patch in "${patches[@]}"; do
if [[ -f $patch ]]; then
msg2 "$patch"
rm "$patch"
fi
done
msg "Exporting patches"
[ "${pkgbuild_dir:0:1}" != / ] \
&& absolute_pkgbuild_dir=$PWD/$pkgbuild_dir \
|| absolute_pkgbuild_dir=$pkgbuild_dir
git -C "$git_repo_path" format-patch "$git_commit_range" --output-directory "$absolute_pkgbuild_dir"
msg "Determine new sources/checksums"
new_patches=("$pkgbuild_dir"/*.patch)
for patch in "${new_patches[@]}"; do
new_sources+=("$patch")
sum=$(sha256sum "$patch")
sum=${sum%% *}
new_sha256sums+=("$sum")
msg2 "$patch; $sum"
done
msg "Adjust PKGBUILD file"
# preserve first src line to keep variables unevaluated
# FIXME: allow this for all sources which should be preserved
newsrc=$(grep 'source=(' "$pkgbuild_file")
[[ $newsrc ]] || newsrc="source=(${new_sources[0]}"
[ "${newsrc: -1:1}" == ')' ] && newsrc="${newsrc: 0:-1}" # truncate trailing )
# add sources
for source in "${new_sources[@]:1}"; do
newsrc+="\n '${source##*/}'"
done
newsrc+=')'
# add sha256sums
newsums="sha256sums=('${new_sha256sums[0]}'"
for sum in "${new_sha256sums[@]:1}"; do
newsums+="\n '${sum}'"
done
newsums+=')'
# apply changes
mv "$pkgbuild_file" "$pkgbuild_file.bak"
awk -v newsrc="$newsrc" -v newsums="$newsums" '
/^[[:blank:]]*source(_[^=]+)?=/,/\)[[:blank:]]*(#.*)?$/ {
if (!s) {
print newsrc
s++
}
next
}
/^[[:blank:]]*(md|sha)[[:digit:]]+sums(_[^=]+)?=/,/\)[[:blank:]]*(#.*)?$/ {
if (!w) {
print newsums
w++
}
next
}
1
END {
if (!s) {
print newsrc
}
if (!w) {
print newsums
}
}
' "$pkgbuild_file.bak" > "$pkgbuild_file"
msg "Diff"
diff -Naur "$pkgbuild_file.bak" "$pkgbuild_file"

View File

@ -0,0 +1,2 @@
#!/usr/bin/bash -e
../sync-variants.sh qt5-base mingw-w64 mingw-w64-{static,dynamic,angle}

91
devel/sync-variants.sh Executable file
View File

@ -0,0 +1,91 @@
#!/usr/bin/bash
# Syncs different variants of a PKGBUILD
# setup shell environment
#set -euxo pipefail
set -e # abort on first error
shopt -s nullglob
shopt -s extglob
export BASHOPTS
source /usr/share/makepkg/util/message.sh
colorize
# declare (variant-specific) variables to be preserved
variables_to_preserve=(
pkgname
_pkg_arch
_android_arch
_android_platform
_android_platform_arch
_android_api_level
_boost_arch
_boost_address_model
)
# read arguments
if [[ ! $1 ]] || [[ ! $2 ]] || [[ ! $3 ]]; then
echo 'Syncs different variants of a PKGBUILD'
echo "Usage: $0 path/to/variants master-variant variants-to-adjust"
echo "example: $0 ~/pkgbuilds/qt5-base mingw-w64 mingw-w64-{static,angle,dynamic}"
echo "example: $0 ~/pkgbuilds/qt5-base android-aarch64 android-*"
exit -1
fi
path=$1
master=$2
shift 2
if [[ ! -d $path ]]; then
error "Variant-path \"$path\" does not exist."
exit -1
fi
if [[ ! -d $path/$master ]]; then
error "Master \"$master\" does not exist in \"$path\"."
exit -1
fi
# determine variant dirs (using $(echo ...) to apply glob)
pushd "$path" > /dev/null
variant_dirs=("$@")
variant_dirs=($(echo ${variant_dirs[@]}))
# sync variant dirs with master
for variant_dir in "${variant_dirs[@]}"; do
# skip test packages
[[ $variant_dir == *'-test' ]] && continue
# prevent syncing master with itself
[[ $variant_dir == $master ]] && continue
msg "Sync $path/$master -> $path/$variant_dir"
# ensure variant dir exists
if [[ ! -d $variant_dir ]]; then
warning "Creating $path/$variant_dir because it doesn't exist yet"
mkdir "$variant_dir"
fi
# read values to preserve (use sed rather than just sourcing to preserve variables)
msg2 "Saving values to preserve"
declare -A values_to_preserve=()
for variable_to_preserve in "${variables_to_preserve[@]}"; do
value=$(sed -n -e "/^${variable_to_preserve}=.*$/p" "$variant_dir/PKGBUILD" | sed -E 's/([\:#$%&\-\])/\\\1/g')
values_to_preserve[$variable_to_preserve]=${value#${variable_to_preserve}=}
[[ $value ]] && plain " - $value"
# note: Last sed to escape special characters is required because the value is later passed again to sed as replacement value.
done
msg2 "Replace files"
rm "$variant_dir/"* # clean existing files first (files might have been removed in master and we don't want any leftovers)
cp "$master/"* "$variant_dir"
msg2 "Restore values to preserve"
sed_args=()
for variable_to_preserve in "${variables_to_preserve[@]}"; do
sed_args+=('-e' "s:^${variable_to_preserve}=.*$:${variable_to_preserve}=${values_to_preserve[$variable_to_preserve]}:")
# if we would have just sourced the variant PKGBUILD:
# sed_args+=('-e' "s/^${variable_to_preserve}=.*$/${variable_to_preserve}=${!variable_to_preserve}/")
done
sed "${sed_args[@]}" "$master/PKGBUILD" > "$variant_dir/PKGBUILD"
done
popd > /dev/null

View File

@ -1,34 +0,0 @@
#!/bin/bash
# Syncs the different variants of android-openssl
set -e # abort on first error
master="${1:-android-aarch64}"
[[ -d 'openssl' ]] && pushd 'openssl' || pushd .
if [ $# -gt 1 ]; then
echo "Error: too many arguments specified"
echo "Usage: $0 master_dir"
exit -2
elif [[ ! -d $master ]]; then
echo "Error: specified master $master does not exist"
exit -3
fi
for dir in android-*; do
[[ $dir == *'-test' ]] && continue
[[ $dir == $master ]] && continue
[[ -d $dir ]] || continue
source "$dir/PKGBUILD"
rm "$dir/"* # clean first (files might have been removed in master)
cp "$master/"* "$dir"
sed -e "s/pkgname=android-.*-openssl/pkgname=$dir-openssl/" \
-e "s/_android_arch=.*/_android_arch=$_android_arch/" \
-e "s/_pkg_arch=.*/_pkg_arch=$_pkg_arch/" \
-e "s/_android_platform=.*/_android_platform=$_android_platform/" \
"$master/PKGBUILD" > "$dir/PKGBUILD"
done
popd

View File

@ -1,28 +0,0 @@
#!/usr/bin/bash
# Syncs the different variants of mingw-w64-qt5-base
set -e # abort on first error
master="${1:-mingw-w64}"
[[ -d 'qt5-base' ]] && pushd 'qt5-base' || pushd .
if [ $# -gt 1 ]; then
echo "Error: too many arguments specified"
echo "Usage: $0 master_dir"
exit -2
elif [[ ! -d $master ]]; then
echo "Error: specified master $master does not exist"
exit -3
fi
for dir in mingw-w64 mingw-w64-*; do
[[ $dir == *'-test' ]] && continue
if [[ $dir != $master ]] && [[ -d $dir ]]; then
rm "$dir/"* # clean first (files might have been removed in master)
cp "$master/"* "$dir"
sed -e '/pkgname=mingw-w64-qt5-base/{c\pkgname=mingw-w64-qt5-base'${dir#mingw-w64} -e ';d}' "$master/PKGBUILD" > "$dir/PKGBUILD"
fi
done
popd