Ticket #12602: 12602_spkg_download.patch

File 12602_spkg_download.patch, 13.5 KB (added by jdemeyer, 9 years ago)
  • spkg/bin/sage

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1332274895 -3600
    # Node ID 35345d06fdf226a7f99d17db2488dbbb3f9fd6d1
    # Parent  9a30468baae6d126b02e4017604d89909f85a59f
    Rework download/extract code in sage-spkg
    
    diff --git a/spkg/bin/sage b/spkg/bin/sage
    a b  
    766766fi
    767767
    768768install() {
    769     cd "$SAGE_ROOT/spkg"
    770     SAGE_LOGS="$SAGE_ROOT/spkg/logs"
     769    cd "$CUR"
    771770    mkdir -p "$SAGE_LOGS"
    772771    for PKG in "$@"
    773772    do
     
    785784        echo "Calling sage-spkg on '$PKG'"
    786785        PKG_NAME=`echo "$PKG" | sed -e "s/\.spkg$//"`
    787786        PKG_NAME=`basename "$PKG_NAME"`
    788         case "$PKG" in
    789             /*) PKG_PATH="$PKG";;
    790             *) PKG_PATH="$CUR/$PKG";;
    791         esac
    792         # Could use ./pipestatus here, but using an absolute path is safer:
    793         # (We'll have to change it anyway in case 'pipestatus' one day moves.)
     787
    794788        "$SAGE_ROOT"/spkg/pipestatus \
    795             "sage-spkg $OPTF $OPTS '$PKG_PATH' 2>&1" \
    796             "(trap '' SIGINT; tee -a '$SAGE_ROOT'/install.log '$SAGE_LOGS/$PKG_NAME'.log)"
    797         # Do not try to install further packages (if any) if one failed:
     789            "sage-spkg $OPTF $OPTS '$PKG' 2>&1" \
     790            "(trap '' SIGINT; tee -a '$SAGE_ROOT/install.log' '$SAGE_LOGS/$PKG_NAME.log')"
     791        # Do not try to install further packages if one failed
    798792        if [ $? -ne 0 ]; then
    799             echo >&2 "Error: Failed to install package '$PKG_NAME'."
    800793            exit 1
    801794        fi
    802         shift
    803795    done
    804     exit $?
     796    exit 0
    805797}
    806798
    807799if [ "$1" = '-optional' -o "$1" = "--optional" ]; then
  • spkg/bin/sage-env

    diff --git a/spkg/bin/sage-env b/spkg/bin/sage-env
    a b  
    214214fi
    215215
    216216# Setting Sage-related location environment variables.
    217 SAGE_PACKAGES="$SAGE_ROOT/spkg" && export SAGE_PACKAGES
    218 SAGE_LOCAL="$SAGE_ROOT/local"   && export SAGE_LOCAL
    219 SAGE_DATA="$SAGE_ROOT/data"     && export SAGE_DATA
    220 SAGE_DOC="$SAGE_ROOT/devel/sage/doc" && export SAGE_DOC
    221 PATH="$SAGE_PACKAGES/bin:$SAGE_LOCAL/bin:$PATH" && export PATH
     217export SAGE_LOCAL="$SAGE_ROOT/local"
     218export SAGE_DATA="$SAGE_ROOT/data"
     219export SAGE_PACKAGES="$SAGE_ROOT/spkg"
     220export SAGE_LOGS="$SAGE_ROOT/spkg/logs"
     221export SAGE_DOC="$SAGE_ROOT/devel/sage/doc"
     222export PATH="$SAGE_PACKAGES/bin:$SAGE_LOCAL/bin:$PATH"
    222223
    223224# We offer a toolchain option, so if $SAGE_LOCAL/toolchain/toolchain-env exists source it.
    224225# Since the user might do something crazy we do not do any checks, but hope for the best.
  • spkg/bin/sage-spkg

    diff --git a/spkg/bin/sage-spkg b/spkg/bin/sage-spkg
    a b  
    3131#
    3232# AUTHORS:
    3333#
     34# - Jeroen Demeyer (2012-02-27): #12602: refactor code to find packages,
     35#   download them and extract them.
     36#
    3437# - Jeroen Demeyer (2012-02-27): #12479: big reorganization.
    3538#
    3639# - Volker Braun, Jeroen Demeyer (2012-01-18): #11073: remove the
     
    165168    DELETE_TMP=0
    166169fi
    167170
    168 ##################################################################
    169 # Figure out the package filename, download it if needed.
    170 ##################################################################
    171 PKG_NAME=`basename "$1" | sed -e "s/\.spkg$//"`
    172 PKG_SRC="$1"
    173 PKG_BASE=`echo "$PKG_NAME" | sed -e "s/-.*//"`
    174 
    175 if [ ! -f "$PKG_SRC" ]; then
    176     if [ -f "$SAGE_PACKAGES/standard/$PKG_NAME.spkg" ]; then
    177         PKG_SRC="$SAGE_PACKAGES/standard/$PKG_NAME.spkg"
    178     else
    179         if [ -f "$SAGE_PACKAGES/optional/$PKG_NAME.spkg" ]; then
    180             PKG_SRC="$SAGE_PACKAGES/optional/$PKG_NAME.spkg"
    181         else
    182             CUR=`pwd`
    183             cd "$SAGE_PACKAGES"
    184             PKG_NEWEST_VER=`./standard/newest_version "$PKG_NAME" 2> /dev/null`
    185             if [ -n "$PKG_NEWEST_VER" ]; then
    186                 PKG_SRC="$SAGE_PACKAGES/standard/$PKG_NEWEST_VER.spkg"
    187             else
    188                 PKG_NEWEST_VER=`./standard/newest_version -base "$PKG_NAME" 2> /dev/null`
    189                 if [ -n "$PKG_NEWEST_VER" ]; then
    190                     PKG_SRC="$SAGE_PACKAGES/standard/$PKG_NEWEST_VER.spkg"
    191                 fi
    192             fi
    193         fi
    194     fi
    195 fi
    196 
    197171# Don't verbosely extract files from spkgs by default (#10040):
    198172if [ "$SAGE_SPKG_LIST_FILES" = "yes" ]; then
    199173    UNTAR_VERBOSE=v
     
    201175    unset UNTAR_VERBOSE
    202176fi
    203177
     178##################################################################
     179# Figure out the package filename, download it if needed.
     180##################################################################
     181# One should be able to install a package using
     182# sage -i <package-name> where <package-name> can be any of the
     183# following values:
     184#
     185# 1a. /path/to/<package>-x.y.z.spkg, i.e. the package is found somewhere
     186#     in your file system and you're giving an absolute path.
     187# 1b. relative/path/to/<package>-x.y.z.spkg, the same with a relative
     188#     path.
     189# 2a. <package>-x.y.z, i.e. the name of the package plus the package's
     190#     version numbers.
     191# 2b. <package>-x.y.z.spkg, i.e. the name of the package in addition to
     192#     the version numbers and the ".spkg" extension.
     193# 3.  <package>, i.e. the name of the package without a version number.
     194# 4.  <URL>/<package>-x.y.z.spkg, i.e. the full URL where the package
     195#     is hosted.  The package is always downloaded, even if a local
     196#     package with the same name and version already exists.
     197#
     198# In cases 2a, 2b and 3 we first look locally inside spkg/* for a
     199# matching package.  Otherwise, we try to download it.  In all cases,
     200# we reduce to case 1a.
     201#
     202# See #7544 and #12602.
     203#
     204
     205PKG_SRC="$1"
     206# Does PKG_SRC contain a slash?
     207if echo "$PKG_SRC" | grep / >/dev/null; then
     208    PKG_HAS_PATH=yes
     209fi
     210# PKG_NAME is the last path component without .spkg
     211# This already reduces case 2b to case 2a.
     212PKG_NAME=`basename "$PKG_SRC" | sed 's/\.spkg$//'`
     213PKG_BASE=`echo "$PKG_NAME" | sed 's/-.*//'`
     214
     215
     216if [ -f "$PKG_SRC" ]; then
     217    # PKG_SRC is a file.  If it is given by a relative path, prepend `pwd`
     218    # (reduce case 1b to 1a)
     219    if ! echo "$PKG_SRC" | grep '^/' >/dev/null; then
     220        PKG_SRC="`pwd`/$PKG_SRC"
     221    fi
     222elif [ -z "$PKG_HAS_PATH" ]; then
     223    # If PKG_SRC is not an existing file and doesn't contain a slash,
     224    # we are in case 2a or 3.  Try to find a package in spkg/standard
     225    # or spkg/optional (or other possible directories under spkg).
     226    cd "$SAGE_PACKAGES"
     227    for spkg in `ls -1t */${PKG_NAME}.spkg */${PKG_NAME}-*.spkg 2>/dev/null`; do
     228        if [ -f "$spkg" ]; then
     229            # Found a good package
     230            PKG_SRC="`pwd`/$spkg"
     231            PKG_NAME=`basename "$spkg" | sed 's/\.spkg$//'`
     232            break
     233        fi
     234    done
     235fi
     236
     237# If we haven't found the package yet, we must download it
     238if [ ! -f "$PKG_SRC" ]; then
     239    echo "Attempting to download package $PKG_NAME"
     240    mkdir -p "$SAGE_PACKAGES/optional"
     241    cd "$SAGE_PACKAGES/optional"
     242   
     243    # Handle case 3 above, where the package name contains no version
     244    # number and no full path.  Reduce to case 2a.
     245    if [ "$PKG_BASE" = "$PKG_NAME" -a -z "$PKG_HAS_PATH" ]; then
     246        # No version number found
     247        echo "Searching for latest online version of $PKG_BASE"
     248        PKG_NAME=`sage-latest-online-package "$PKG_BASE"`
     249        if [ $? -ne 0 ]; then
     250            exit 1
     251        fi
     252        echo "Found $PKG_NAME"
     253        PKG_SRC="$PKG_NAME"
     254    fi
     255
     256    # This script can handle either full URLs (case 4) or a package
     257    # name with version (case 2a).
     258    sage-download_package "$PKG_SRC"
     259
     260    if [ ! -f "$PKG_NAME.spkg" ]; then
     261        echo >&2 "Error: Failed to download package $PKG_NAME from $SAGE_SERVER"
     262        exit 1
     263    fi
     264    PKG_SRC="`pwd`/$PKG_NAME.spkg"
     265fi
     266
     267# Do a final check that PKG_SRC is a file with an absolute path
     268cd /
     269if [ ! -f "$PKG_SRC" ]; then
     270    echo >&2 "Error: spkg file '$PKG_SRC' not found."
     271    echo >&2 "This shouldn't happen, it is a bug in the sage-spkg script."
     272    exit 1
     273fi
     274
     275# Go back to SAGE_ROOT where we have less chance of completely messing
     276# up the system if we do something wrong.
     277cd "$SAGE_ROOT" || exit
     278
     279##################################################################
     280# Extract the package
     281##################################################################
     282# Usage: uncompress_spkg $PKG_FILE_NAME
     283# Uncompresses a spkg to stdout.
     284uncompress_spkg()
     285{
     286    bzip2 -d -c "$1" 2>/dev/null || gzip -d -c "$1" 2>/dev/null || cat "$1"
     287}
     288
    204289if [ $INFO -ne 0 ]; then
    205     if [ ! -f "$PKG_SRC" ]; then
    206         echo >&2 "Package $PKG_NAME not found"
    207     fi
    208     bunzip2 -c "$PKG_SRC" 2>/dev/null | tar Ofx${UNTAR_VERBOSE} - $PKG_NAME/SPKG.txt 2>/dev/null
    209     if [ $? -ne 0 ]; then
    210         tar Ofx${UNTAR_VERBOSE} "$PKG_SRC" "$PKG_NAME/SPKG.txt" 2>/dev/null
    211     fi
    212     echo ""
     290    uncompress_spkg "$PKG_SRC" | tar Oxf - "$PKG_NAME/SPKG.txt"
    213291    if [ $? -ne 0 ]; then
    214292        echo >&2 "No file SPKG.txt in $PKG_NAME"
    215293        exit 1
     
    242320fi
    243321
    244322if [ $DELETE_TMP -eq 1 ]; then
    245     rm -rf "$PKG_BASE-"*
     323    rm -rf "$PKG_NAME"
    246324else
    247     for dir in "$PKG_BASE-"*
    248     do
    249         if [ -e "$dir" ]; then
    250             echo "Moving old directory $dir to $SAGE_BUILD_DIR/old..."
    251             mkdir -p old
    252             if [ $? -ne 0 ]; then
    253                 echo >&2 "Error creating directory $SAGE_BUILD_DIR/old."
    254                 exit 1
    255             fi
    256             rm -rf old/"$dir"
    257             mv "$dir" old/
     325    if [ -e "$PKG_NAME" ]; then
     326        echo "Moving old directory $PKG_NAME to $SAGE_BUILD_DIR/old..."
     327        mkdir -p old
     328        if [ $? -ne 0 ]; then
     329            echo >&2 "Error creating directory $SAGE_BUILD_DIR/old."
     330            exit 1
    258331        fi
    259     done
     332        rm -rf old/"$PKG_NAME"
     333        mv "$PKG_NAME" old/
     334    fi
    260335fi
    261336
    262 if [ ! -f "$PKG_SRC" ]; then
    263     echo "$0: file $PKG_NAME does not exist"
    264     echo "Attempting to download it."
    265     CUR=`pwd`
    266     mkdir -p "$SAGE_PACKAGES/optional"
    267     cd "$SAGE_PACKAGES/optional"
    268    
    269     FOUND_VERSION=''
    270     if ! echo "$PKG_NAME" | grep - >/dev/null; then
    271         # No version number found
    272         echo "Searching for latest version of $PKG_NAME"
    273         PKG_NAME=`sage-latest-online-package "$PKG_NAME"`
    274         if [ $? -eq 0 ]; then
    275             echo "Found package $PKG_NAME"
    276             FOUND_VERSION='1'
    277         else
    278             echo "$PKG_NAME"
    279             exit 1
    280         fi
    281     fi
    282 
    283     # See trac ticket #7544. One should be able to install a package using
    284     # sage -i <package-name> where <package-name> can be any of the
    285     # following values:
    286     #
    287     # 1. <package>-x.y.z, i.e. the name of the package plus the package's
    288     #    version numbers.
    289     # 2. the full name <package>-x.y.z.spkg, i.e. the name of the package in
    290     #    addition to the version numbers and the ".spkg" extension.
    291     # 3. <URL>/<package>-x.y.z.spkg, i.e. the full URL where the package is
    292     #    hosted. This can be a URL on the Sage website or somewhere else.
    293     # 4. /path/to/<package>-x.y.z.spkg, i.e. the package is found somewhere
    294     #    in your file system and you're giving an absolute or relative path
    295     #    to the package.
    296     #
    297     # See trac ticket #8043. If no version was specified but a version
    298     # was found above, then $PKG_NAME now contains the version number.
    299     # Download this, since it's a known Sage package.  Otherwise,
    300     # download the package specified by $1, since it either already
    301     # contains the version number or is a URL.
    302     if [ "x$FOUND_VERSION" = "x" ]; then
    303         sage-download_package "$1"
    304     else
    305         sage-download_package "$PKG_NAME"
    306     fi
    307     if [ ! -f "$PKG_NAME.spkg" ]; then
    308         echo "sage: Failed to download package $PKG_NAME from $SAGE_SERVER"
    309         exit 1
    310     fi
    311     PKG_SRC="`pwd`/$PKG_NAME.spkg"
    312     cd "$CUR"
     337if [ -e "$PKG_NAME" ]; then
     338    echo >&2 "Error (re)moving $PKG_NAME"
     339    exit 1
    313340fi
    314341
    315 echo "Extracting package $PKG_SRC ..."
     342echo "Extracting package $PKG_SRC"
    316343ls -l "$PKG_SRC"
    317344
    318 if [ -d "$PKG_NAME" ]; then
    319     echo "Removing previous version."
    320     rm -rf "$PKG_NAME"
    321 fi
    322 
    323 bunzip2 -c "$PKG_SRC" 2>/dev/null | tar fx${UNTAR_VERBOSE} -  2>/dev/null
    324 if [ ! -d "$PKG_NAME" ]; then
    325     tar fx${UNTAR_VERBOSE} "$PKG_SRC"
     345uncompress_spkg "$PKG_SRC" | tar x${UNTAR_VERBOSE}f -
     346if [ $? -ne 0 ]; then
     347    echo >&2 "Error: failed to extract $PKG_SRC"
     348    exit 1
    326349fi
    327350echo "Finished extraction"
    328351
    329 if [ ! -d "$PKG_NAME" ]; then
    330     echo >&2 "sage: After decompressing the directory $PKG_NAME does not exist"
    331     echo >&2 "This means that the corresponding .spkg needs to be downloaded"
    332     echo >&2 "again."
    333     if ! echo "$PKG_NAME" | grep - >/dev/null; then
    334         # No version number found
    335         echo "Searching for latest version of $PKG_NAME"
    336         PKG_NAME=`sage-latest-online-package "$PKG_NAME"`
    337         if [ $? -eq 0 ]; then
    338             echo "Found package $PKG_NAME"
    339         else
    340             echo "$PKG_NAME"
    341             exit 1
    342         fi
    343     fi
    344     sage-download_package "$PKG_NAME"
    345     echo `pwd`
    346     bunzip2 -c "$PKG_NAME.spkg"  | tar fx${UNTAR_VERBOSE} -
    347     if [ ! -d "$PKG_NAME.spkg" ]; then
    348         tar fx${UNTAR_VERBOSE} "$PKG_NAME.spkg"
    349     fi
    350     if [ ! -d "$PKG_NAME" ]; then
    351         echo "Second download resulted in a corrupted package."
    352         exit 1
    353     fi
    354 fi
    355 
    356352##################################################################
    357353# The package has been extracted, prepare for installation
    358354##################################################################
    359355
    360356cd "$PKG_NAME"
     357if [ $? -ne 0 ]; then
     358    echo >&2 "Error: after extracting, the directory $PKG_NAME does not exist"
     359    exit 1
     360fi
    361361
    362362# When there is no spkg-install, assume the "spkg" is a tarball not
    363363# specifically made for Sage.  Since we want it to be as easy as