Opened 11 years ago
Closed 10 years ago
#11932 closed enhancement (fixed)
Allow running Sage with a temporary DOT_SAGE directory
Reported by:  Jeroen Demeyer  Owned by:  Leif Leonhardy 

Priority:  major  Milestone:  sage5.1 
Component:  scripts  Keywords:  
Cc:  John Palmieri  Merged in:  sage5.1.beta0 
Authors:  John Palmieri  Reviewers:  Jeroen Demeyer 
Report Upstream:  N/A  Work issues:  
Branch:  Commit:  
Dependencies:  #11926  Stopgaps: 
Description (last modified by )
Version which deals with matplotlib issue: apply
 trac_11932root.v2.patch to the Sage root repository
 trac_11932scripts.patch to the local/bin repository
 trac_11932doc.patch to the Sage repository
 trac_11932sage.v2.patch to the Sage repository
Attachments (5)
Change History (37)
comment:1 Changed 11 years ago by
Cc:  John Palmieri added 

comment:2 Changed 11 years ago by
Dependencies:  → #11926 

comment:3 followup: 4 Changed 11 years ago by
I'd rename the function (as it creates a temporary directory), and not use a global variable for the "return value".
The function could either take the variable name as its parameter (such that sage_make_tmp_dir DIR
assigns the directory name to DIR
), or just "echo" the result (as mktemp
already does), such that one writes DIR=sage_make_tmp_dir
or whatever.
comment:4 Changed 11 years ago by
Replying to leif:
or just "echo" the result (as
mktemp
already does), such that one writesDIR=sage_make_tmp_dir
or whatever.
DIR=`sage_make_tmp_dir`
of course.
comment:5 Changed 11 years ago by
Description:  modified (diff) 

Status:  new → needs_review 
Here's a version in which the function echoes the path of the temp dir.
This works best with the patch from #11924: without applying the patch there, the parent directory of the temporary .sage directory gets polluted with pickle cache files.
comment:7 Changed 11 years ago by
Description:  modified (diff) 

Milestone:  → sage5.0 
Rebased to #11073. Documentation added.
comment:8 Changed 11 years ago by
Authors:  → John Palmieri 

Reviewers:  → Jeroen Demeyer 
Status:  needs_review → needs_work 
The following seems to be portable:
mktemp d ${TMPDIR:/tmp}/dotsageXXXXXX
so I would use this instead of sage_mktempdir
.
sagesage
needs to be replaced by sage
.
Finally, why not simply run
./sage norc </dev/null >/dev/null 2>/dev/null
in spkginstall
?
comment:10 Changed 11 years ago by
Description:  modified (diff) 

Status:  needs_work → needs_review 
Fixed. The patch to spkg/install is no longer valid; instead, we need to patch the sagestarts script.
comment:11 Changed 11 years ago by
Status:  needs_review → needs_work 

This is not working, and I'm probably making some bash error in the sage
script. When I run
sage c 'print "hello"'
it works as it should: no banner, just "hello", then a new shell prompt. When I run
sage norc c 'print "hello"'
it starts up Sage, prints a banner, prints "hello", and then gives the Sage prompt. As a consequence, the sagestarts
script is not working right. What am I doing wrong?
comment:12 Changed 11 years ago by
Status:  needs_work → needs_review 

Never mind, I've found various workarounds. The best seems to be to replace sage "@$"
with source sage "@$"
.
comment:13 followup: 14 Changed 11 years ago by
No, that doesn't work: it doesn't clean up the temporary directory afterward. Using "$SAGE_ROOT/sage" "$@"
seems to work, but is that a good solution?
comment:14 Changed 11 years ago by
Replying to jhpalmieri:
Using
"$SAGE_ROOT/sage" "$@"
seems to work, but is that a good solution?
I don't see how calling sage
could be different from calling $SAGE_ROOT/sage
, are you sure the former doesn't work (I haven't tried).
comment:15 Changed 11 years ago by
Calling sage
at this point in the script will call the script spkg/bin/sage
, because that directory is now in the path, so it is conceivably different from calling $SAGE_ROOT/sage
. In practice, it does behave differently, as I mentioned above: using $SAGE_ROOT/sage
in the script, the command sage norc c 'print "hello"'
works as expected, while using just sage
, the command prints the Sage banner, prints "hello", and then you end up at a Sage prompt, running Sage interactively.
comment:16 Changed 11 years ago by
Status:  needs_review → needs_work 

I confirm your observation but I still don't understand what's going on. I'm investigating...
comment:17 Changed 11 years ago by
Okay, found it. "sage" refers to the shell function
sage() { sage_setup sageipython "$@" i }
We should probably rename this function to run_sage
or so...
In the mean time, replace $SAGE_ROOT/sage "$@"
by
command sage "$@"
Help of command:
command: command [pVv] command [arg ...] Execute a simple command or display information about commands. Runs COMMAND with ARGS suppressing shell function lookup, or display information about the specified COMMANDs. Can be used to invoke commands on disk when a function with the same name exists. Options: p use a default value for PATH that is guaranteed to find all of the standard utilities v print a description of COMMAND similar to the `type' builtin V print a more verbose description of each COMMAND Exit Status: Returns exit status of COMMAND, or failure if COMMAND is not found.
comment:18 Changed 11 years ago by
Status:  needs_work → needs_review 

Ah, good, thank you. I agree, having a function named "sage" is probably a bad idea, but changing it should be dealt with elsewhere. (It would require a number of changes in the sage
script, and a correspondingly large number of tests to make sure everything works.)
comment:19 Changed 11 years ago by
On an entirely different topic: I don't like the norc
option name. In the light of #12647, I would expect that option to suppress the reading of the sagerc
script (and/or the init.sage
script). I would only support nodotsage
.
comment:20 Changed 11 years ago by
Here are new patches which just use nodotsage
. (I also considered tmpdotsage
, but it doesn't matter too much, as long as it's documented.)
comment:21 Changed 11 years ago by
It would be nice to also have nosagerc
, and let nodotsage
by default not bypass [$DOT_SAGE/]sagerc
(if it exists; of course no directory should be created if it doesn't).
comment:23 Changed 10 years ago by
Unfortunately, this doesn't quite work properly since DOT_SAGE
is changed after reading sageenv
. The following variables still refer to $HOME/.sage
:
jdemeyer@arcanis:/usr/local/src/sage5.0.beta13$ ./sage nodotsage sh c env  fgrep "$HOME/.sage" MPLCONFIGDIR=/home/jdemeyer/.sage//matplotlib1.1.0 SAGE_STARTUP_FILE=/home/jdemeyer/.sage//init.sage SAGE_TESTDIR=/home/jdemeyer/.sage//tmp
I suggest moving the nodotsage
check before the sourcing of sageenv
.
comment:24 Changed 10 years ago by
Status:  needs_review → needs_work 

comment:26 Changed 10 years ago by
Just one more issue: now that you don't source sageenv
, the command sage
looks for sage
in the system PATH
. I suggest to replace
command sage "$@"
by
command "$0" "$@"
If you make this change, you can give this ticket positive review.
Furthermore, I would like to point out that $HOME/.sage/matplotlib1.1.0
is still created by sageenv
when building Sage. But that shouldn't stop this ticket.
Changed 10 years ago by
Attachment:  trac_11932root.v2.patch added 

comment:27 Changed 10 years ago by
Description:  modified (diff) 

Here are two versions. If we want the easy root, the first version just makes the change

spkg/bin/sage
diff git a/spkg/bin/sage b/spkg/bin/sage
a b cd "$SAGE_ROOT" 238 238 if [ "$1" = 'nodotsage' ]; then 239 239 export DOT_SAGE=`mktemp d ${TMPDIR:/tmp}/dotsageXXXXXX` 240 240 shift 241 command sage"$@"241 command "$0" "$@" 242 242 status=$? 243 243 rm rf "$DOT_SAGE" 244 244 exit $status
The second version deals (I think) with the matplotlib issue, creating the config directory in sage.misc.misc, where other subdirectories of DOT_SAGE
are created. It's not a very significant difference: the file trac_11932sage.v2.patch and these changes to the root patch:

spkg/bin/sageenv
diff git a/spkg/bin/sageenv b/spkg/bin/sageenv
a b fi 281 281 282 282 if [ "$DOT_SAGE" = "" ]; then 283 283 # It is *not* an error if this directory does not exist, it will 284 # be created in spkg/bin/sage. This also works if $HOME/.sage is a 285 # symbolic link to a nonexisting directory. 284 # be created in spkg/bin/sage or devel/sage/sage/misc/misc.py. 285 # This also works if $HOME/.sage is a symbolic link to a 286 # nonexisting directory. 286 287 DOT_SAGE=`resolvelinks "$HOME/.sage"` 287 288 288 289 # In theory, DOT_SAGE is not required to have a trailing slash. … … if [ f "$MPLINITFILE" ]; then 340 341 MPLCONFIGDIR="$DOT_SAGE/matplotlib" 341 342 fi 342 343 export MPLCONFIGDIR 343 # Create the directory, because matplotlib doesn't seem to be happy otherwi 344 mkdir p "$MPLCONFIGDIR" 344 # The directory is created when Sage starts (see sage.misc.misc). 345 345 fi 346 346 347 347 # Add some directories to $LD_LIBRARY_PATH:
If you have time to look at the second version, please consider it. If you would rather deal with just the first version here, we can move the other version to a new ticket.
comment:28 Changed 10 years ago by
Milestone:  sage5.0 → sage5.1 

For the Sage patch, what about:
try: # Create the matplotlib config directory sage_makedirs(os.environ["MPLCONFIGDIR"]) except KeyError: pass
I don't see a reason to create a directory when the environment variable doesn't exist.
comment:29 Changed 10 years ago by
Since we set MPLCONFIGDIR
in sageenv, there is no reason for it not to be set (unless someone does something risky and unsupported like try to import the Sage library using the system's Python). But I made the change anyway.
Changed 10 years ago by
Attachment:  trac_11932sage.v2.patch added 

comment:30 Changed 10 years ago by
Description:  modified (diff) 

Looks good and works well.
I'm currently running
sage nodotsage tp 2 devel/sage/sage
comment:32 Changed 10 years ago by
Merged in:  → sage5.1.beta0 

Resolution:  → fixed 
Status:  positive_review → closed 
I copied some patches from #11926 (and make some small changes to John's patch).