Ticket #5852: 5852_sage_root.patch

File 5852_sage_root.patch, 6.7 KB (added by jdemeyer, 10 years ago)

Patch for $SAGE_ROOT/sage, SAGE_ROOT repository

  • sage

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1314106427 -7200
    # Node ID 7ba0f9e85bd7526c7094bdc31e39242f03dce467
    # Parent  63ec78ff119b55fcee239ba2e1e5a3c3a06a09cc
    Expand symlinks recursively to detect SAGE_ROOT
    
    diff --git a/sage b/sage
    a b  
    11#!/usr/bin/env bash
     2#
     3# Sage: a free open-source mathematics software system
     4#
     5# This program is free software; you can redistribute it and/or
     6# modify it under the terms of the GNU General Public License
     7# as published by the Free Software Foundation; either version 2
     8# of the License, or (at your option) any later version.
     9#
     10# This program is distributed in the hope that it will be useful,
     11# but WITHOUT ANY WARRANTY; without even the implied warranty of
     12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13# GNU General Public License for more details.
     14#
     15# You should have received a copy of the GNU General Public License
     16# along with this program; if not, write to the Free Software
     17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    218
    3 # Set SAGE_ROOT to the location of the sage install.
    4 SAGE_ROOT="....."
    519
    6 CUR="`pwd`"   # save the current directory, so can change back after startup
     20# Set SAGE_ROOT to the location of the sage install, i.e. the directory
     21# containing this shell script.  If unset, we will try to figure it out
     22# automatically.
     23#SAGE_ROOT=/path/to/sage-version
    724
    8 if [ "x$CUR" = "x" ]; then
    9     echo "**************************************************************"
    10     echo " Error: the current working directory does not exist. Please  "
    11     echo " run sage again from an existing directory.                   "
    12     echo "**************************************************************"
     25
     26
     27# Resolve all symbolic links in a filename.  This more or less behaves
     28# like "readlink -f" except that it does not convert the filename to an
     29# absolute path (a relative path remains relative), nor does it treat
     30# "." or ".." specially.  See Trac ticket #5852.
     31resolvelinks() {
     32    # $in is what still needs to be converted (normally has no starting slash)
     33    in="$1"
     34    # $out is the part which is converted (normally ends with trailing slash)
     35    out="./"
     36
     37    # Move stuff from $in to $out
     38    while [ -n "$in" ]; do
     39        # Normalize $in by replacing consecutive slashes by one slash
     40        while { in_single_slash=${in//\/\//\/}; [ "$in" != "$in_single_slash" ]; }; do
     41            in=$in_single_slash
     42        done
     43
     44        # If $in starts with a slash, remove it and set $out to the root
     45        in_without_slash=${in/#\//}
     46        if [ "$in" != "$in_without_slash" ]; then
     47            in=$in_without_slash
     48            out="/"
     49            continue
     50        fi
     51
     52        # Check that the directory $out exists by trying to cd to it.
     53        # If this fails, then cd will show an error message (unlike
     54        # test -d "$out"), so no need to be more verbose.
     55        ( cd "$out" ) || return $?
     56
     57
     58        # Get the first component of $in
     59        f=${in%%/*}
     60
     61        # If it is not a symbolic link, simply move it to $out
     62        if [ ! -L "$out$f" ]; then
     63            in=${in#"$f"}
     64            out="$out$f"
     65
     66            # If the new $in starts with a slash, move it to $out
     67            in_without_slash=${in/#\//}
     68            if [ "$in" != "$in_without_slash" ]; then
     69                in=$in_without_slash
     70                out="$out/"
     71            fi
     72            continue
     73        fi
     74
     75        # Now resolve the symbolic link "$f"
     76        f_resolved=`readlink -n "$out$f" 2>/dev/null`
     77        status=$?
     78        # status 127 means readlink could not be found.
     79        if [ $status -eq 127 ]; then
     80            # We don't have "readlink", try a stupid "ls" hack instead.
     81            # This will fail if we have filenames like "a -> b".
     82            fls=`ls -l "$out$f" 2>/dev/null`
     83            status=$?
     84            f_resolved=${fls##*-> }
     85
     86            # If $fls equals $f_resolved, then certainly
     87            # something is wrong
     88            if [ $status -eq 0 -a "$fls" = "$f_resolved" ]; then
     89                echo >&2 "Cannot parse output from ls -l '$out$f'"
     90                return 1
     91            fi
     92        fi
     93        if [ $status -ne 0 ]; then
     94            echo >&2 "Cannot read symbolic link '$out$f'"
     95            return $status
     96        fi
     97
     98        # In $in, replace $f by $f_resolved (leave $out alone)
     99        in=${in/#"$f"/"$f_resolved"}
     100    done
     101
     102    # Return $out
     103    echo "$out"
     104}
     105
     106# If SAGE_ROOT is not given, find it out from $0
     107if [ -z "$SAGE_ROOT" ];  then
     108    # Get the path to $0 (this shell script) with all symbolic links
     109    # resolved
     110    SAGE_ROOT=`resolvelinks "$0"` || \
     111    SAGE_ROOT="$0"
     112
     113    # Get the directory component
     114    SAGE_ROOT="${SAGE_ROOT%/*}"
     115fi
     116
     117# Make SAGE_ROOT absolute
     118SAGE_ROOT=`cd "$SAGE_ROOT" && pwd -P`
     119
     120# Save the current directory, so we can change back after startup
     121# If pwd fails, we fall back to SAGE_ROOT
     122CUR=`pwd` || CUR="$SAGE_ROOT"
     123
     124if [ ! -f "$SAGE_ROOT/local/bin/sage-sage" ]; then
     125cat >&2 <<COMPLAINT
     126************************************************************************
     127You must compile Sage first using 'make' in the Sage root directory.
     128(If you have already compiled Sage, you must set the SAGE_ROOT variable
     129in the file '$0').
     130************************************************************************
     131COMPLAINT
    13132    exit 1
    14133fi
    15134
    16 if [ "$SAGE_ROOT" = "....." ];  then
    17     SAGE_ROOT=`readlink -n "$0" 2> /dev/null` || \
    18     SAGE_ROOT=`realpath    "$0" 2> /dev/null` || \
    19     SAGE_ROOT="$0"
    20 
    21     SAGE_ROOT="${SAGE_ROOT%/*}/"
    22 
    23     if [ ! -f "$SAGE_ROOT/local/bin/sage-sage" ]; then
    24         echo "**************************************************************************"
    25         echo "You must compile Sage first using 'make' in the Sage root directory." >&2
    26         echo "(If you have already compiled Sage, you must set the SAGE_ROOT variable in "
    27         echo "the file '$0')".
    28         echo "**************************************************************************"
    29         exit 1
    30     fi
    31 fi
    32 
    33 # Make root absolute:
    34 cd "$SAGE_ROOT"
    35 SAGE_ROOT=`pwd`
    36135export SAGE_ROOT
    37136export CUR
    38137
    39 "$SAGE_ROOT/local/bin/sage-sage" "$@"
    40138
    41 # This should kill all children of this process too.
     139# Run the actual Sage script
     140cd "$SAGE_ROOT"
     141local/bin/sage-sage "$@"
     142
     143
     144# Kill all processes in the current process group.  In practice, this
     145# means all child processes not running their own pty.
    42146# Uncomment this if you have trouble with orphans.
    43 # Note that you'll get an annoying "Killed" message
    44 # whenever Sage exits.
    45 # kill -9 -$$
     147# We do this in the background after waiting 10 seconds to give the
     148# various processes (including this shell script) some time to exit
     149# cleanly.
     150# { sleep 10; kill -9 -$$ 2>/dev/null; } &