Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
copy helper functions from sage-env to sage; backup env to DOT_SAGE
Browse files Browse the repository at this point in the history
  • Loading branch information
rwst committed May 20, 2014
1 parent 80e319d commit 6b65eea
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions src/bin/sage
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,105 @@ usage_advanced() {
exit 0
}

# Resolve all symbolic links in a filename. This more or less behaves
# like "readlink -f" except that it does not convert the filename to an
# absolute path (a relative path remains relative), nor does it treat
# "." or ".." specially.
#
# AUTHOR: Jeroen Demeyer (2011-08-23): Trac tickets #5852 and #11704
#
resolvelinks() {
# $in is what still needs to be converted (normally has no starting slash)
in="$1"
# $out is the part which is converted (normally ends with trailing slash)
out="./"

# Move stuff from $in to $out
while [ -n "$in" ]; do
# Normalize $in by replacing consecutive slashes by one slash
while { in_single_slash=${in//\/\//\/}; [ "$in" != "$in_single_slash" ]; }; do
in=$in_single_slash
done

# If $in starts with a slash, remove it and set $out to the root
in_without_slash=${in/#\//}
if [ "$in" != "$in_without_slash" ]; then
in=$in_without_slash
out="/"
continue
fi

# Check that the directory $out exists by trying to cd to it.
# If this fails, then cd will show an error message (unlike
# test -d "$out"), so no need to be more verbose.
( cd "$out" ) || return $?


# Get the first component of $in
f=${in%%/*}

# If it is not a symbolic link, simply move it to $out
if [ ! -L "$out$f" ]; then
in=${in#"$f"}
out="$out$f"

# If the new $in starts with a slash, move it to $out
in_without_slash=${in/#\//}
if [ "$in" != "$in_without_slash" ]; then
in=$in_without_slash
out="$out/"
fi
continue
fi

# Now resolve the symbolic link "$f"
f_resolved=`readlink -n "$out$f" 2>/dev/null`
status=$?
# status 127 means readlink could not be found.
if [ $status -eq 127 ]; then
# We don't have "readlink", try a stupid "ls" hack instead.
# This will fail if we have filenames like "a -> b".
fls=`ls -l "$out$f" 2>/dev/null`
status=$?
f_resolved=${fls##*-> }

# If $fls equals $f_resolved, then certainly
# something is wrong
if [ $status -eq 0 -a "$fls" = "$f_resolved" ]; then
echo >&2 "Cannot parse output from ls -l '$out$f'"
return 1
fi
fi
if [ $status -ne 0 ]; then
echo >&2 "Cannot read symbolic link '$out$f'"
return $status
fi

# In $in, replace $f by $f_resolved (leave $out alone)
in=${in/#"$f"/"$f_resolved"}
done

# Return $out
echo "$out"
}

if [ "$DOT_SAGE" = "" ]; then
# It is *not* an error if this directory does not exist, it will
# be created in src/bin/sage or src/sage/misc/misc.py.
# This also works if $HOME/.sage is a symbolic link to a
# non-existing directory.
DOT_SAGE=`resolvelinks "$HOME/.sage"`

# In theory, DOT_SAGE is not required to have a trailing slash.
# But since there are some issues (#11924, maybe #12221),
# we add a slash for safety.
DOT_SAGE="${DOT_SAGE}/"
mkdir -p "$DOT_SAGE"
fi

# Back up environment
/usr/bin/env >"$DOT_SAGE"/orig-env

# Check for '--nodotsage' before sourcing sage-env; otherwise sage-env
# will already have set some environment variables with the old
# setting for DOT_SAGE.
Expand Down

0 comments on commit 6b65eea

Please sign in to comment.