Ticket #329: trac_329_sage_scripts.2.patch

File trac_329_sage_scripts.2.patch, 3.7 KB (added by ddrake, 10 years ago)

apply to sage_scripts repo

  • new file sage-add-integrity-check-to-spkg

    # HG changeset patch
    # User Dan Drake <drake@kaist.edu>
    # Date 1268121405 -32400
    # Node ID ea75176adb6fadbe18e981235e37b7221d066ba1
    # Parent  69ccc5d679b1e1c42e57b28e492770cfd185040a
    integrity verification for spkgs; see trac #329
    
    diff --git a/sage-add-integrity-check-to-spkg b/sage-add-integrity-check-to-spkg
    new file mode 100755
    - +  
     1#!/usr/bin/env bash
     2
     3# given a spkg file as $1, this adds the necessary checksum information.
     4# See http://trac.sagemath.org/sage_trac/ticket/329.
     5
     6SPKGNAME=$(basename $1 .spkg)
     7
     8if [ "x$1" = "x$SPKGNAME" ]
     9then
     10    echo "I don't think you gave me a spkg file."
     11    exit 1
     12fi
     13
     14echo Adding integrity verification checksum to $1...
     15
     16# is the spkg bzipped?
     17TYPE=$(file $1 | grep 'bzip2 compressed data')
     18if [ -z "$TYPE" ]
     19then
     20    CAT="cat"
     21    UNCAT="cat"
     22else
     23    CAT="bunzip2 -c"
     24    UNCAT="bzip2 -c"
     25fi
     26
     27$CAT $1 > $SPKGNAME.tar
     28
     29(tar xf $SPKGNAME.tar --exclude $SPKGNAME.cksum -O; \
     30tar tvf $SPKGNAME.tar --exclude $SPKGNAME.cksum) | \
     31cksum | awk '{print $1, $2}' > $SPKGNAME.cksum
     32
     33echo checksum of $1 is `cat $SPKGNAME.cksum`.
     34
     35tar rf $SPKGNAME.tar $SPKGNAME.cksum
     36
     37$UNCAT $SPKGNAME.tar > $1
     38rm $SPKGNAME.tar $SPKGNAME.cksum
  • sage-spkg

    diff --git a/sage-spkg b/sage-spkg
    a b  
    216216    cd "$CUR"
    217217fi
    218218
     219sage-spkg-integrity-check $PKG_SRC
     220
    219221# * The -i option below to ignore checksum errors, since
    220222#   I've had problems with this on Solaris.
    221223# * The m option avoids clock skew problems.   
  • new file sage-spkg-integrity-check

    diff --git a/sage-spkg-integrity-check b/sage-spkg-integrity-check
    new file mode 100755
    - +  
     1#!/usr/bin/env bash
     2
     3# Given a spkg file as $1, this script will check the integrity of $1
     4# using the methods descrbed in
     5# http://trac.sagemath.org/sage_trac/ticket/329.
     6#
     7# This script exits with status...
     8#     0 if the verification passed;
     9#     1 if the checksum exits but verification failed;
     10#     2 if no cksum information was found;
     11#     3 if something else goes wrong.
     12
     13if [ -z "$1" ]
     14then
     15    echo Error in $0: you need to specify a .spkg file.
     16    exit 3
     17fi
     18
     19if [ -z "$SAGE_ROOT" ]
     20then
     21    echo Error in $0: \$SAGE_ROOT must be set.
     22    exit 3
     23fi
     24
     25echo -n "Verifying integrity of $1..."
     26
     27SPKGNAME=$(basename $1 .spkg)
     28
     29# is the spkg bzipped?
     30TYPE=$(file $1 | grep 'bzip2 compressed data')
     31if [ -z "$TYPE" ]
     32then
     33    J=""
     34else
     35    # we're assuming that "j" is the tar option for bzip2 compression
     36    J="j"
     37fi
     38
     39# extract existing checksum
     40tar ${J}xf $1 -O $SPKGNAME.cksum | awk 'NR==1 {print $1, $2}' > $SAGE_ROOT/spkg/cksum/$SPKGNAME.downloaded.cksum
     41
     42# the "NR==1" tells awk to only print line 1; if a checksum gets put
     43# into the spkg twice, the -O in tar will print it out twice. We just
     44# take the first line.
     45
     46# GNU tar exits with exit code 2 if the file isn't found, so we use the
     47# bash PIPESTATUS array to check that exit code (the usual $? returns
     48# the exit code of the final process in the pipe)
     49if [ ${PIPESTATUS[0]} = "2" ]
     50then
     51    echo no integrity checksum stored in $1, skipping integrity check.
     52    exit 2
     53fi
     54
     55# now compute the checksum
     56(tar ${J}xf $1 -O --exclude $SPKGNAME.cksum ; \
     57tar ${J}tvf $1 --exclude $SPKGNAME.cksum 2>/dev/null) | \
     58cksum | awk '{print $1, $2}' > $SAGE_ROOT/spkg/cksum/$SPKGNAME.computed.cksum
     59
     60cmp $SAGE_ROOT/spkg/cksum/$SPKGNAME.downloaded.cksum $SAGE_ROOT/spkg/cksum/$SPKGNAME.computed.cksum
     61if [ $? = "0" ]
     62then
     63    echo passed.
     64else
     65    echo FAILED!
     66    echo $1 appears corrupted; expected checksum is \"$(cat $SAGE_ROOT/spkg/cksum/$SPKGNAME.downloaded.cksum)\", but computed checksum is \"$(cat $SAGE_ROOT/spkg/cksum/$SPKGNAME.computed.cksum)\".
     67    exit 1
     68fi