From c90ec19701a1b7f77d07f10e159cc4de2f31fb54 Mon Sep 17 00:00:00 2001 From: Matthias Koeppe Date: Sat, 6 Jan 2024 15:43:55 -0800 Subject: [PATCH] build/bin/sage-spkg: Make spkg-check an installed script --- build/bin/sage-spkg | 69 +++++++++++++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/build/bin/sage-spkg b/build/bin/sage-spkg index f4a9d2ef0fa..6b48b99e33f 100755 --- a/build/bin/sage-spkg +++ b/build/bin/sage-spkg @@ -355,8 +355,9 @@ export SAGE_DESTDIR="${SAGE_BUILD_DIR}/${PKG_NAME}/inst" # end of spkg-install. export SAGE_DESTDIR_LOCAL="${SAGE_DESTDIR}${SAGE_INST_LOCAL}" -INSTALLED_SCRIPTS="prerm piprm postrm" -WRAPPED_SCRIPTS="build install check preinst postinst $INSTALLED_SCRIPTS" +INSTALLED_SCRIPTS_DEST="$SAGE_SPKG_SCRIPTS/$PKG_BASE" +INSTALLED_SCRIPTS="prerm piprm postrm check" +WRAPPED_SCRIPTS="build install preinst pipinst postinst $INSTALLED_SCRIPTS" warning_for_experimental_packages() { ############################ @@ -490,7 +491,7 @@ if [ -z "$PKG_NAME_UPSTREAM" ]; then if [ -d "$PKG_SCRIPTS"/src ]; then ln -s $(cd "$PKG_SCRIPTS"/src && pwd -P) "$PKG_NAME"/src fi - for a in build install check preinst postinst; do # replace by use of $WRAPPED_SCRIPTS later + for a in $WRAPPED_SCRIPTS; do if [ -r "$PKG_SCRIPTS"/spkg-$a.in ]; then cp "$PKG_SCRIPTS"/spkg-$a.in "$PKG_NAME"/ elif [ -x "$PKG_SCRIPTS"/spkg-$a ]; then @@ -544,6 +545,7 @@ prepare_for_installation() { ##################################### write_script_wrapper() { local script="$1" local script_dir="$2" + local fallback_script_dir="$3" trap "echo >&2 Error: Unexpected error writing wrapper script for $script; exit \$_" ERR @@ -584,14 +586,21 @@ if [ \$? -ne 0 ]; then exit 1 fi -cd "\$SAGE_PKG_DIR" +__EOF__ + if [ -n "$fallback_script_dir" ]; then + cat >> "$tmpscript" <<__EOF__ +cd "\$SAGE_PKG_DIR" 2>/dev/null || cd "$fallback_script_dir" + +__EOF__ + else + cat >> "$tmpscript" <<__EOF__ if [ \$? -ne 0 ]; then echo >&2 "Error: could not cd to the package build directory \$SAGE_PKG_DIR" exit 1 fi __EOF__ - + fi cat "$script.in" >> "$tmpscript" mv "$tmpscript" "$script" chmod +x "$script" @@ -607,20 +616,29 @@ touch spkg-piprm.in # 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 +for script in $WRAPPED_SCRIPTS; do # '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 - if echo "$INSTALLED_SCRIPTS" | grep -w "$script" > /dev/null; then - script_dir="$SAGE_ROOT" - else - script_dir="$(pwd)" - fi + # directory, and may be run later. + # For the installed *rm scripts, set their root directory to $SAGE_ROOT. + # For the installed check scripts, some need the temporary build directory, + # others don't. So try to run out of the temporary build directory + # but fall back to $SAGE_ROOT. + case $script in + check) script_dir="$(pwd)" + fallback_script_dir="\$SAGE_ROOT" + ;; + *rm) script_dir="\$SAGE_ROOT" + fallback_script_dir= + ;; + *) script_dir="$(pwd)" + fallback_script_dir= + ;; + esac script="spkg-$script" if [ -f "$script.in" ]; then - write_script_wrapper "$(pwd)/$script" "$script_dir" + write_script_wrapper "$(pwd)/$script" "$script_dir" "$fallback_script_dir" fi done } ####################################### prepare_for_installation @@ -735,15 +753,14 @@ install_scripts() { ############################################## # Some spkg scripts, if they exist, should also be installed to # $SAGE_SPKG_SCRIPTS; they are not included in the package's manifest, but are # removed by sage-spkg-uninstall -INSTALLED_SCRIPTS_DEST="$SAGE_SPKG_SCRIPTS/$PKG_BASE" -if [ ! -f $INSTALLED_SCRIPTS_DEST/spkg-requirements.txt ]; then +if [ ! -f "$INSTALLED_SCRIPTS_DEST"/spkg-requirements.txt ]; then # No packages to uninstall with pip, so remove the prepared uninstall script # and the prepared deferred installation script rm -f spkg-piprm spkg-piprm.in spkg-pipinst spkg-pipinst.in fi -for script in $INSTALLED_SCRIPTS pipinst; do # pipinst can be added to WRAPPED_SCRIPTS later +for script in $INSTALLED_SCRIPTS; do script="spkg-$script" if [ -f "$script" ]; then @@ -788,9 +805,14 @@ run_test_suite() { ############################################### # SAGE_INST_LOCAL. It might make more sense to run the tests before, but the # spkg-check scripts were written before use of DESTDIR installs, and so # fail in many cases. This might be good to change later. - if [ -f spkg-check ]; then + if ! cd "$SAGE_BUILD_DIR/$PKG_NAME" 2>/dev/null; then + cd "$PKG_SCRIPTS" || exit $? + fi + + if [ -f "$INSTALLED_SCRIPTS_DEST"/spkg-check ]; then echo "Running the test suite for $PKG_NAME..." - time ./spkg-check + export PKG_BASE + time "$INSTALLED_SCRIPTS_DEST"/spkg-check if [ $? -ne 0 ]; then TEST_SUITE_RESULT="failed" if [ "$SAGE_CHECK" = "warn" ]; then @@ -805,8 +827,10 @@ run_test_suite() { ############################################### TEST_SUITE_RESULT="passed" echo "Passed the test suite for $PKG_NAME." fi - elif [ -f spkg-check.in ]; then - echo "The test suite for $PKG_NAME requires the temporary build directory." + elif [ -f "$PKG_SCRIPTS"/spkg-check.in -o -f "$PKG_SCRIPTS"/spkg-check ]; then + echo "The test suite for $PKG_NAME cannot be run because the script" + echo "$INSTALLED_SCRIPTS_DEST/spkg-check" + echo "is missing. Install/re-install package $PKG_NAME to run the test suite." exit 1 else echo "Package $PKG_NAME has no test suite." @@ -899,9 +923,6 @@ if [ $POST_INSTALL = 1 ]; then fi if [ "$SAGE_CHECK" = "yes" -o "$SAGE_CHECK" = "warn" ]; then - if ! cd "$SAGE_BUILD_DIR/$PKG_NAME" 2>/dev/null; then - cd "$PKG_SCRIPTS" || exit $? - fi run_test_suite fi