Skip to content

Commit

Permalink
feat: build checks for packages already built before building
Browse files Browse the repository at this point in the history
This will prevent a bunch of `ERROR: A package is already built`, where one can think there's an
actual error but in this context it's simply a informational message.
  • Loading branch information
kriansa committed Dec 20, 2021
1 parent efddc6b commit 88ae8d0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 66 deletions.
27 changes: 20 additions & 7 deletions lib/cmds/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ function build_install {
local pkgs=("$@")
local built_packages=0

# Create the gnupg folder before start the build process. The performance hit of running this
# before each build is not impactful enough so we can keep doing it here. If it ever becomes too
# problematic we can consider a different alternative here.
gpg::start

for pkg in "${pkgs[@]}"; do
build_pkg "$pkg" "$opt_force" && ((built_packages+=1))
done

# Make sure we cleanup after using GPG
gpg::cleanup

if [ "$built_packages" -le 0 ]; then
msg "No packages built."
return
Expand Down Expand Up @@ -77,15 +85,20 @@ function build_pkg {
return 2
fi

if test "$force" = "false"; then
msg "Checking build version of package $pkg"

if pkg::build_is_latest "$pkg"; then
msg2 "Package $pkg is already built at its latest version."
msg2 "Use 'pacom build --force' to override."
return 0
fi
fi

local pkgbuild_dir; pkgbuild_dir="$(db::get_package_pkgbuild_dir "$pkg")"
local build_path="$GIT_REPO_PATH/$pkg/$pkgbuild_dir"
local tmp_error; tmp_error="$(mktemp)"

# Create the gnupg folder before start the build process. The performance hit of running this
# before each build is not impactful enough so we can keep doing it here. If it ever becomes too
# problematic we can consider a different alternative here.
gpg::start

# Build the package
test "$force" = "true" && local force_param="--force"
msg "Building package $pkg"
Expand All @@ -100,8 +113,8 @@ function build_pkg {
)
status=$?

# Make sure we cleanup after using GPG
gpg::cleanup
# Synchronize changes on GPG, if any
gpg::sync_db

if grep --quiet 'One or more PGP signatures could not be verified' "$tmp_error"; then
msg2 "It seems that you need to import some PGP key(s) so this package can be built."
Expand Down
52 changes: 2 additions & 50 deletions lib/cmds/update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,56 +85,8 @@ function update_upstream_vcs {

# Updates on VCS packages are handled differently. Even if there are no changes on the PKGBUILD,
# there might be changes on the VCS upstream repo, so we need to check if it has updates as well.
is_vcs_package "$pkg" || return 2
vcs_package_has_updates "$pkg" || return 2
pkg::is_vcs "$pkg" || return 2
pkg::build_is_latest "$pkg" && return 2

ask "New update on upstream for VCS package $pkg. Update?" && return 0 || return 2
}

function is_vcs_package {
local pkg=$1
local vcs_regex=".*-(bzr|git|hg|svn)$"

[[ "$pkg" =~ $vcs_regex ]]
}

function vcs_package_has_updates {
local pkg=$1
local pkgbuild_dir; pkgbuild_dir="$(db::get_package_pkgbuild_dir "$pkg")"
local build_path="$GIT_REPO_PATH/$pkg/$pkgbuild_dir"

msg "Checking VCS updates for $pkg..."

# Get the current built version on the repo
current=$(repo::get_built_package_version "$pkg")

# Run makepkg to update the PKGBUILD pkgver
( cd "$build_path" && makepkg --nobuild --clean --cleanbuild --nocheck \
--needed --rmdeps --noconfirm --noprogressbar > /dev/null 2>&1 )

# Evaluates the PKGBUILD to get the version: It should have been changed by the makepkg above
# Copied from https://github.com/AladW/aurutils/blob/master/lib/aur-srcver
#
# How safe is this, by the way? Well, at the moment we end up sourcing the PKGBUILD, the user has
# already reviewed and accepted its inclusion on the repository, so there is consent already,
# since for building, makepkg will source it anyway. If it ever changes, it will be detected on
# the update process, before VCS update is triggered, so again, consent is given, hence no
# issues.
#
# shellcheck disable=SC2016
last=$(env -C "$build_path" -i bash -c '
PATH= source PKGBUILD
if [[ -v epoch ]]; then
fullver=$epoch:$pkgver-$pkgrel
else
fullver=$pkgver-$pkgrel
fi
echo "$fullver"')

# Clean all version changes done to the PKGBUILD by makepkg
git::clean_repo "$pkg"

test "$current" != "$last"
}
18 changes: 9 additions & 9 deletions lib/gpg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ function gpg::start {

# Create the GPG path
msg "Mounting a temporary GPG home structure"
export GNUPGHOME; GNUPGHOME="$(gpg::home-path)"
export GNUPGHOME="$GIT_REPO_PATH/.gnupg"
test -d "$GNUPGHOME" || mkdir "$GNUPGHOME"
chmod 700 "$GNUPGHOME"

# Then import saved keys onto it
local keys_path; keys_path="$(gpg::keys-path)"
local keys_path; keys_path="$(gpg::keys_path)"
if [ -f "$keys_path" ]; then
gpg --import "$keys_path" 2> /dev/null
fi
Expand All @@ -21,25 +21,25 @@ function gpg::start {
gpg -k > /dev/null 2>&1
}

function gpg::keys-path {
function gpg::keys_path {
echo "$GIT_REPO_PATH/gpg-keys.asc"
}

function gpg::home-path {
echo "$GIT_REPO_PATH/.gnupg"
}

function gpg::cleanup {
function gpg::sync_db {
# Do nothing if we're using system gpg
test -n "$USE_SYSTEM_GPG" && return

# Save all existing keys onto our git repo
local keys_path; keys_path="$(gpg::keys-path)"
local keys_path; keys_path="$(gpg::keys_path)"
if ! gpg --export --armor --output "$keys_path" --batch --yes 2> /dev/null; then
error "Failure while saving the GPG keys to your pacom git repository."
msg "Check the output of the command: 'GNUPGHOME=\"$GNUPGHOME\" gpg --export --armor --output \"$keys_path\"'"
fi

git::commit_gpg ":arrow_up: update gpg database"
}

function gpg::cleanup {
msg "Cleaning up temporary GPG home structure"
rm -rf "$GNUPGHOME"
}
55 changes: 55 additions & 0 deletions lib/pkg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- mode: sh; sh-shell: bash -*-
# Common package related functions

function pkg::build_is_latest {
local pkg=$1

# Get the current built version on the repo
local current; current=$(repo::get_built_package_version "$pkg")
local next; next=$(pkg::get_build_version "$pkg")

test "$current" = "$next"
}

function pkg::is_vcs {
local pkg=$1
local vcs_regex=".*-(bzr|git|hg|svn)$"

[[ "$pkg" =~ $vcs_regex ]]
}

function pkg::get_build_version {
local pkg=$1
local pkgbuild_dir; pkgbuild_dir="$(db::get_package_pkgbuild_dir "$pkg")"
local build_path="$GIT_REPO_PATH/$pkg/$pkgbuild_dir"

# Run makepkg to update the PKGBUILD pkgver for vcs packages
pkg::is_vcs "$pkg" && ( cd "$build_path" && \
makepkg --nobuild --clean --cleanbuild --nodeps --noconfirm > /dev/null 2>&1 )

# Evaluates the PKGBUILD to get the version: It should have been changed by the makepkg above
# Copied from https://github.com/AladW/aurutils/blob/master/lib/aur-srcver
#
# How safe is this, by the way? Well, at the moment we end up sourcing the PKGBUILD, the user has
# already reviewed and accepted its inclusion on the repository, so there is consent already,
# since for building, makepkg will source it anyway. If it ever changes, it will be detected on
# the update process, before VCS update is triggered, so again, consent is given, hence no
# issues.
#
# shellcheck disable=SC2016
local last; last=$(env -C "$build_path" -i bash -c '
PATH= source PKGBUILD
if [ -n "$epoch" ]; then
fullver=$epoch:$pkgver-$pkgrel
else
fullver=$pkgver-$pkgrel
fi
echo "$fullver"')

# Clean all version changes done to the PKGBUILD by makepkg made by vcs packages
pkg::is_vcs "$pkg" && git::clean_repo "$pkg" > /dev/null 2>&1

echo "$last"
}

0 comments on commit 88ae8d0

Please sign in to comment.