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

Commit

Permalink
sage.misc.temporary_file: Move SAGE_TMP implementation here
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Koeppe committed May 1, 2022
1 parent eb1a786 commit 2e81b67
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 61 deletions.
60 changes: 5 additions & 55 deletions src/sage/misc/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from .lazy_string import lazy_string
from sage.env import DOT_SAGE, HOSTNAME
from sage.misc.lazy_import import lazy_import
import sage.misc.temporary_file

lazy_import("sage.misc.call", ["AttrCallObject", "attrcall", "call_method"],
deprecation=29869)
Expand Down Expand Up @@ -214,61 +215,10 @@ def try_read(obj, splitlines=False):
#################################################


@lazy_string
def SAGE_TMP():
"""
EXAMPLES::
sage: from sage.misc.misc import SAGE_TMP
sage: SAGE_TMP
l'.../temp/...'
"""
d = os.path.join(DOT_SAGE, 'temp', HOSTNAME, str(os.getpid()))
os.makedirs(d, exist_ok=True)
return d


@lazy_string
def ECL_TMP():
"""
Temporary directory that should be used by ECL interfaces launched from
Sage.
EXAMPLES::
sage: from sage.misc.misc import ECL_TMP
sage: ECL_TMP
l'.../temp/.../ecl'
"""
d = os.path.join(str(SAGE_TMP), 'ecl')
os.makedirs(d, exist_ok=True)
return d


@lazy_string
def SPYX_TMP():
"""
EXAMPLES::
sage: from sage.misc.misc import SPYX_TMP
sage: SPYX_TMP
l'.../temp/.../spyx'
"""
return os.path.join(str(SAGE_TMP), 'spyx')


@lazy_string
def SAGE_TMP_INTERFACE():
"""
EXAMPLES::
sage: from sage.misc.misc import SAGE_TMP_INTERFACE
sage: SAGE_TMP_INTERFACE
l'.../temp/.../interface'
"""
d = os.path.join(str(SAGE_TMP), 'interface')
os.makedirs(d, exist_ok=True)
return d
SAGE_TMP = lazy_string(sage.misc.temporary_file.sage_tmp)
ECL_TMP = lazy_string(sage.misc.temporary_file.ecl_tmp)
SPYX_TMP = lazy_string(sage.misc.temporary_file.spyx_tmp)
SAGE_TMP_INTERFACE = lazy_string(sage.misc.temporary_file.sage_tmp_interface)


SAGE_DB = os.path.join(DOT_SAGE, 'db')
Expand Down
74 changes: 68 additions & 6 deletions src/sage/misc/temporary_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,71 @@
import os
import tempfile
import atexit
import functools

# We do not use sage.misc.cachefunc here so that this module does not depend on
# Cython modules.

@functools.cache
def sage_tmp():
"""
EXAMPLES::
sage: from sage.misc.temporary_file import sage_tmp
sage: sage_tmp()
'.../temp/...'
"""
# This duplicates code that is run when sage.misc.misc is loaded;
# but modularized distributions may not have sage.misc.misc.
from sage.env import DOT_SAGE, HOSTNAME
os.makedirs(DOT_SAGE, mode=0o700, exist_ok=True)

d = os.path.join(DOT_SAGE, 'temp', HOSTNAME, str(os.getpid()))
os.makedirs(d, exist_ok=True)
return d


@functools.cache
def ecl_tmp():
"""
Temporary directory that should be used by ECL interfaces launched from
Sage.
EXAMPLES::
sage: from sage.misc.temporary_file import ecl_tmp
sage: ecl_tmp()
'.../temp/.../ecl'
"""
d = os.path.join(sage_tmp(), 'ecl')
os.makedirs(d, exist_ok=True)
return d


@functools.cache
def spyx_tmp():
"""
EXAMPLES::
sage: from sage.misc.temporary_file import spyx_tmp
sage: spyx_tmp()
'.../temp/.../spyx'
"""
return os.path.join(sage_tmp(), 'spyx')


@functools.cache
def sage_tmp_interface():
"""
EXAMPLES::
sage: from sage.misc.temporary_file import sage_tmp_interface
sage: sage_tmp_interface()
'.../temp/.../interface'
"""
d = os.path.join(sage_tmp(), 'interface')
os.makedirs(d, exist_ok=True)
return d


def delete_tmpfiles():
Expand All @@ -52,8 +117,7 @@ def delete_tmpfiles():
True
"""
import shutil
from sage.misc.misc import SAGE_TMP
shutil.rmtree(str(SAGE_TMP), ignore_errors=True)
shutil.rmtree(sage_tmp(), ignore_errors=True)


# Run when Python shuts down
Expand Down Expand Up @@ -96,8 +160,7 @@ def tmp_dir(name="dir_", ext=""):
0
sage: f.close()
"""
from sage.misc.misc import SAGE_TMP
tmp = tempfile.mkdtemp(prefix=name, suffix=ext, dir=str(SAGE_TMP))
tmp = tempfile.mkdtemp(prefix=name, suffix=ext, dir=sage_tmp())
name = os.path.abspath(tmp)
return name + os.sep

Expand Down Expand Up @@ -146,8 +209,7 @@ def tmp_filename(name="tmp_", ext=""):
0
sage: f.close()
"""
from sage.misc.misc import SAGE_TMP
handle, tmp = tempfile.mkstemp(prefix=name, suffix=ext, dir=str(SAGE_TMP))
handle, tmp = tempfile.mkstemp(prefix=name, suffix=ext, dir=sage_tmp())
os.close(handle)
name = os.path.abspath(tmp)
return name
Expand Down

0 comments on commit 2e81b67

Please sign in to comment.