Skip to content

Commit

Permalink
build/bin/{sage-spkg,sage-dist-helpers}: For non-sudo DESTDIR-staged …
Browse files Browse the repository at this point in the history
…installation, defer installation of wheel to post-install phase
  • Loading branch information
Matthias Koeppe committed Nov 21, 2023
1 parent f10820f commit a6400d5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 19 deletions.
50 changes: 35 additions & 15 deletions build/bin/sage-dist-helpers
Original file line number Diff line number Diff line change
Expand Up @@ -355,38 +355,58 @@ sdh_store_and_pip_install_wheel() {
shift
done
sdh_store_wheel "$@"
if [ -n "$SAGE_SUDO" ]; then
# Trac #29585: Do the SAGE_DESTDIR staging of the wheel installation
# ONLY if SAGE_SUDO is set (in that case, we still do the staging so
# that we do not invoke pip as root).

wheel_basename="${wheel##*/}"
distname="${wheel_basename%%-*}"

if [ -d "$SAGE_BUILD_DIR/$PKG_NAME" ]; then
# Normal package install through sage-spkg;
# scripts live in the package's build directory
# until copied to the final destination by sage-spkg.
script_dir="$SAGE_BUILD_DIR/$PKG_NAME"
else
script_dir="$SAGE_SPKG_SCRIPTS/$PKG_BASE"
fi

if [ -n "$SAGE_DESTDIR" -a -z "$SAGE_SUDO" ]; then
# We stage the wheel file and do the actual installation in post.
echo "sdh_actually_pip_install_wheel $distname $pip_options -r \"\$SAGE_SPKG_SCRIPTS/\$PKG_BASE/spkg-requirements.txt\"" >> "$script_dir"/spkg-pipinst
else
if [ -n "$SAGE_DESTDIR" ]; then
# Trac #29585: Do the SAGE_DESTDIR staging of the wheel installation
# ONLY if SAGE_SUDO is set (in that case, we still do the staging so
# that we do not invoke pip as root).
# --no-warn-script-location: Suppress a warning caused by --root
local sudo=""
local root="--root=$SAGE_DESTDIR --no-warn-script-location"
else
# Trac #32361: Of course, this can only be done for normal packages,
# whose installation goes through sage-spkg.
# For script packages, we do have to invoke pip as root.
elif [ -n "$SAGE_SUDO" ]; then
# Trac #32361: For script packages, we do have to invoke pip as root.
local sudo="$SAGE_SUDO"
local root=""
else
#
local sudo=""
local root=""
fi
else
local sudo=""
local root=""
sdh_actually_pip_install_wheel $distname $root $pip_options -r "$SAGE_SPKG_SCRIPTS/$PKG_BASE/spkg-requirements.txt"
fi
echo "sdh_pip_uninstall -r \"\$SAGE_SPKG_SCRIPTS/\$PKG_BASE/spkg-requirements.txt\"" >> "$script_dir"/spkg-piprm
}

sdh_actually_pip_install_wheel() {
distname=$1
shift
# Trac #32659: pip no longer reinstalls local wheels if the version is the same.
# Because neither (1) applying patches nor (2) local changes (in the case
# of sage-conf, sage-setup, etc.) bump the version number, we need to
# override this behavior. The pip install option --force-reinstall does too
# much -- it also reinstalls all dependencies, which we do not want.
wheel_basename="${wheel##*/}"
distname="${wheel_basename%%-*}"
$sudo sage-pip-uninstall "$distname"
if [ $? -ne 0 ]; then
echo "(ignoring error)" >&2
fi
$sudo sage-pip-install $root $pip_options "$wheel" || \
sdh_die "Error installing ${wheel##*/}"
$sudo sage-pip-install "$@" || \
sdh_die "Error installing $@"
}

sdh_pip_uninstall() {
Expand Down
22 changes: 18 additions & 4 deletions build/bin/sage-spkg
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,13 @@ WRAPPED_SCRIPTS="build install check preinst postinst $INSTALLED_SCRIPTS"

# Prepare script for uninstallation of packages that use sdh_pip_install
# or sdh_store_and_pip_install_wheel.
echo 'sdh_pip_uninstall -r $SAGE_SPKG_SCRIPTS/$PKG_BASE/spkg-requirements.txt' > spkg-piprm.in
touch spkg-piprm.in

for script in $WRAPPED_SCRIPTS; do
# Prepare script for deferred installation of packages that use sdh_pip_install
# or sdh_store_and_pip_install_wheel.
touch spkg-pipinst.in

for script in $WRAPPED_SCRIPTS pipinst; do # pipinst can be added to WRAPPED_SCRIPTS later
# 'Installed' scripts are not run immediately out of the package build
# directory, and may be run later, so set their root directory to
# $SAGE_ROOT
Expand Down Expand Up @@ -706,10 +710,11 @@ INSTALLED_SCRIPTS_DEST="$SAGE_SPKG_SCRIPTS/$PKG_BASE"

if [ ! -f $INSTALLED_SCRIPTS_DEST/spkg-requirements.txt ]; then
# No packages to uninstall with pip, so remove the prepared uninstall script
rm -f spkg-piprm spkg-piprm.in
# and the prepared deferred installation script
rm -f spkg-piprm spkg-piprm.in spkg-pipinst spkg-pipinst.in
fi

for script in $INSTALLED_SCRIPTS; do
for script in $INSTALLED_SCRIPTS pipinst; do # pipinst can be added to WRAPPED_SCRIPTS later
script="spkg-$script"

if [ -f "$script" ]; then
Expand All @@ -729,6 +734,15 @@ done


# Run the post-install script, if any
# But first complete the delayed installation of wheels.
if [ -f spkg-pipinst ]; then
echo "Running pip-install script for $PKG_NAME."
$SAGE_SUDO ./spkg-pipinst
if [ $? -ne 0 ]; then
error_msg "Error running the pipinst script for $PKG_NAME."
exit 1
fi
fi
if [ -f spkg-postinst ]; then
echo "Running post-install script for $PKG_NAME."
time $SAGE_SUDO ./spkg-postinst
Expand Down

0 comments on commit a6400d5

Please sign in to comment.