Ticket #13579: 13579_review.patch

File 13579_review.patch, 17.1 KB (added by jdemeyer, 9 years ago)
  • doc/common/builder.py

    # HG changeset patch
    # User Jeroen Demeyer <jdemeyer@cage.ugent.be>
    # Date 1350061176 -7200
    # Node ID 5ed869e7f54efbbbae5d4bd797f2796ca323658d
    # Parent  7ad363f983947f8f9b301b22107a5b99cda7934a
    Fix temporary files in Sage: reviewer patch
    
    diff --git a/doc/common/builder.py b/doc/common/builder.py
    a b  
    99    pass
    1010
    1111from sage.misc.cachefunc import cached_method
     12from sage.misc.misc import sage_makedirs as mkdir
    1213
    1314# Read options
    1415execfile(os.path.join(os.getenv('SAGE_ROOT'), 'devel', 'sage', 'doc', 'common' , 'build_options.py'))
     
    1617##########################################
    1718#          Utility Functions             #
    1819##########################################
    19 def mkdir(path):
    20     """
    21     Makes the directory at path if it doesn't exist and returns the
    22     string path.
    23 
    24     EXAMPLES::
    25 
    26         sage: sys.path.append(os.environ['SAGE_DOC']+'/common/'); import builder
    27         sage: from sage.misc.misc import SAGE_TMP
    28         sage: d = os.path.join(SAGE_TMP, 'builder_test_dir');  d
    29         '...builder_test_dir'
    30         sage: os.path.exists(d)
    31         False
    32         sage: dd = builder.mkdir(d)
    33         sage: d == dd
    34         True
    35         sage: os.path.exists(d)
    36         True
    37     """
    38     if not os.path.exists(path):
    39         os.makedirs(path)
    40     return path
    41 
    4220def copytree(src, dst, symlinks=False, ignore_errors=False):
    4321    """
    4422    Recursively copy a directory tree using copy2().
     
    142120            sage: b._output_dir('html')
    143121            '.../devel/sage/doc/output/html/en/tutorial'
    144122        """
    145         return mkdir(os.path.join(SAGE_DOC, "output", type, self.lang, self.name))
     123        d = os.path.join(SAGE_DOC, "output", type, self.lang, self.name)
     124        mkdir(d)
     125        return d
    146126
    147127    def _doctrees_dir(self):
    148128        """
     
    157137            sage: b._doctrees_dir()
    158138            '.../devel/sage/doc/output/doctrees/en/tutorial'
    159139        """
    160         return mkdir(os.path.join(SAGE_DOC, "output", 'doctrees', self.lang, self.name))
     140        d = os.path.join(SAGE_DOC, "output", 'doctrees', self.lang, self.name)
     141        mkdir(d)
     142        return d
    161143
    162144    def _output_formats(self):
    163145        """
  • sage/all.py

    diff --git a/sage/all.py b/sage/all.py
    a b  
    235235
    236236    pari._unsafe_deallocate_pari_stack()
    237237   
    238     ### The following is removed -- since it would cleanup
    239     ### the tmp directory that the sage cleaner depends upon.
    240     # The following code close all open file descriptors,
    241     # so that on shared file systems the delete_tmpfiles
    242     # command below works.
    243     # AUTHOR:
    244     #    * Kate Minola (2007-05-03)
    245     #import resource             # Resource usage information.
    246     #maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
    247     #if maxfd != resource.RLIM_INFINITY:
    248         # Iterate through and close all file descriptors.
    249     #    for fd in range(0, maxfd):
    250     #        try:
    251     #            os.close(fd)
    252     #        except OSError:  # ERROR, fd wasn't open to begin with (ignored)
    253     #            pass
    254     # Now delete the temp files
    255     #from sage.misc.misc import delete_tmpfiles
    256     #delete_tmpfiles()
    257 
    258238    # stop the twisted reactor
    259239    try:
    260240       from twisted.internet import reactor
  • sage/interfaces/magma.py

    diff --git a/sage/interfaces/magma.py b/sage/interfaces/magma.py
    a b  
    243243    in a writable directory owned by the user, since when attached,
    244244    Magma has to write sig and lck files.
    245245
    246     EXAMPLES:
     246    EXAMPLES::
     247
    247248        sage: sage.interfaces.magma.extcode_dir()
    248         '...tmp_dir-.../data/'
     249        '.../dir_.../data/'
    249250    """
    250251    global EXTCODE_DIR
    251252    if not EXTCODE_DIR:
  • sage/interfaces/sage0.py

    diff --git a/sage/interfaces/sage0.py b/sage/interfaces/sage0.py
    a b  
    284284        EXAMPLES::
    285285       
    286286            sage: sage0._quit_string()
    287             'from sage.misc.misc import delete_tmpfiles; delete_tmpfiles()'
     287            ''
    288288        """
    289         return 'from sage.misc.misc import delete_tmpfiles; delete_tmpfiles()'
     289        return ""
    290290
    291291    def preparse(self, x):
    292292        """
  • sage/misc/misc.py

    diff --git a/sage/misc/misc.py b/sage/misc/misc.py
    a b  
    3838import operator, os, stat, socket, sys, signal, time, weakref, resource, math
    3939import sage.misc.prandom as random
    4040
    41 from sage.misc.temporary_file import tmp_dir, tmp_filename
     41from sage.misc.temporary_file import tmp_dir, tmp_filename, delete_tmpfiles
    4242
    4343from banner import version, banner
    4444
     
    138138# It is called temp instead of tmp mainly for
    139139# "historical reasons"...
    140140
    141 SAGE_TMP='%s/temp/%s/%s/'%(DOT_SAGE, HOSTNAME, os.getpid())
    142 
     141SAGE_TMP = os.path.join(DOT_SAGE, 'tmp', HOSTNAME, str(os.getpid()))
    143142sage_makedirs(SAGE_TMP)
    144143
    145 SPYX_TMP = '%s/spyx/'%SAGE_TMP
     144SPYX_TMP = os.path.join(SAGE_TMP, 'spyx/')
    146145
    147 
    148 def delete_tmpfiles():
    149     # !!!If you change this, see also SAGE_ROOT/local/bin/sage-doctest!!!
    150     import shutil
    151     try:
    152         if os.path.exists(SAGE_TMP):
    153             shutil.rmtree(SAGE_TMP)
    154     except OSError, msg:
    155         print msg
    156         pass
    157 
    158 SAGE_TMP_INTERFACE='%s/interface/'%SAGE_TMP
     146SAGE_TMP_INTERFACE = os.path.join(SAGE_TMP, 'interface/')
    159147sage_makedirs(SAGE_TMP_INTERFACE)
    160148
    161 SAGE_DB = '%s/db'%DOT_SAGE
     149SAGE_DB = os.path.join(DOT_SAGE, 'db')
    162150sage_makedirs(SAGE_DB)
    163151
    164152try:
  • sage/misc/preparser.py

    diff --git a/sage/misc/preparser.py b/sage/misc/preparser.py
    a b  
    15551555
    15561556    We attach a file::
    15571557
    1558         sage: t=tmp_filename(ext='.py'); open(t,'w').write("print 'hello world'")
     1558        sage: t = tmp_filename(ext='.py')
     1559        sage: open(t,'w').write("print 'hello world'")
    15591560        sage: sage.misc.preparser.load(t, globals(), attach=True)
    15601561        hello world
    1561         sage: os.path.normpath(t) in sage.misc.preparser.attached
     1562        sage: t in sage.misc.preparser.attached
    15621563        True
    15631564
    15641565    You can't attach remote URLs (yet)::
     
    18791880    EXAMPLES::
    18801881
    18811882        sage: sage.misc.reset.reset_attached()
    1882         sage: t=tmp_filename(ext='.py'); open(t,'w').write("print 'hello world'")
    1883         sage: t = os.path.normpath(t)
     1883        sage: t = tmp_filename(ext='.py')
     1884        sage: open(t,'w').write("print 'hello world'")
    18841885        sage: attach(t)
    18851886        hello world
    18861887        sage: attached_files() == [t]
     
    18991900    EXAMPLES::
    19001901
    19011902        sage: sage.misc.reset.reset_attached()
    1902         sage: t=tmp_filename(ext='.py'); open(t,'w').write("print 'hello world'")
    1903         sage: t = os.path.normpath(t)
     1903        sage: t = tmp_filename(ext='.py')
     1904        sage: open(t,'w').write("print 'hello world'")
    19041905        sage: attach(t)
    19051906        hello world
    19061907        sage: attached_files() == [t]
  • sage/misc/reset.pyx

    diff --git a/sage/misc/reset.pyx b/sage/misc/reset.pyx
    a b  
    167167    EXAMPLES::
    168168   
    169169        sage: sage.misc.reset.reset_attached()
    170         sage: t = tmp_filename(ext='.py'); open(t,'w').write("print 'hello world'")
    171         sage: t = os.path.normpath(t)
     170        sage: t = tmp_filename(ext='.py')
     171        sage: open(t,'w').write("print 'hello world'")
    172172        sage: attach(t)
    173173        hello world
    174174        sage: attached_files() == [t]
  • sage/misc/session.pyx

    diff --git a/sage/misc/session.pyx b/sage/misc/session.pyx
    a b  
    378378    Here we test attaching multiple files at once::
    379379   
    380380        sage: sage.misc.reset.reset_attached()
    381         sage: t1=tmp_filename(ext='.py'); open(t1,'w').write("print 'hello world'")
    382         sage: t2=tmp_filename(ext='.py'); open(t2,'w').write("print 'hi there xxx'")
    383         sage: t1, t2 = map(os.path.normpath, (t1, t2))
     381        sage: t1 = tmp_filename(ext='.py')
     382        sage: open(t1,'w').write("print 'hello world'")
     383        sage: t2 = tmp_filename(ext='.py')
     384        sage: open(t2,'w').write("print 'hi there xxx'")
    384385        sage: attach(t1, t2)
    385386        hello world
    386387        hi there xxx
  • sage/misc/temporary_file.py

    diff --git a/sage/misc/temporary_file.py b/sage/misc/temporary_file.py
    a b  
    55import os
    66import tempfile
    77import atexit
    8 from shutil import rmtree
    98
    10 temporary_directories = []
    11 temporary_files = []
     9def delete_tmpfiles():
     10    """
     11    Remove the directory ``SAGE_TMP``.
    1212
    13 def _delete_temporary_files_at_exit():
    14     """
    15     Remove all temporary files
     13    TESTS:
    1614
    17     EXAMPLES::
     15    This is automatically run when Sage exits, test this by running a
     16    separate session of Sage::
    1817
    19         sage: from sage.misc.temporary_file import tmp_filename, temporary_files
    20         sage: old_temporary_files = list(temporary_files)
    21         sage: tmp = tmp_filename('test')
    22         sage: new_temporary_files = list(temporary_files)
    23         sage: set(new_temporary_files).difference(old_temporary_files) == set([tmp])
     18        sage: from sage.tests.cmdline import test_executable
     19        sage: child_SAGE_TMP, err, ret = test_executable(["sage", "-c", "print SAGE_TMP"])
     20        sage: err, ret
     21        ('', 0)
     22        sage: os.path.exists(child_SAGE_TMP)
     23        False
     24
     25    The parent directory should exist::
     26
     27        sage: parent_SAGE_TMP = os.path.normpath(child_SAGE_TMP + '/..')
     28        sage: os.path.isdir(parent_SAGE_TMP)
    2429        True
    2530    """
    26     global temporary_files, temporary_directories
    27     for f in temporary_files:
    28         try:
    29             os.unlink(f)
    30         except OSError:
    31             pass
    32     for d in temporary_directories:
    33         rmtree(d, ignore_errors=True)
     31    import shutil
     32    from sage.misc.misc import SAGE_TMP
     33    shutil.rmtree(str(SAGE_TMP), ignore_errors=True)
    3434
    3535
    3636# Run when Python shuts down
    37 atexit.register(_delete_temporary_files_at_exit)
     37atexit.register(delete_tmpfiles)
    3838
    3939
    4040#################################################################
    4141# temporary directory
    4242#################################################################
    4343
    44 def tmp_dir(name="tmp_dir-", ext=""):
     44def tmp_dir(name="dir_", ext=""):
    4545    r"""
    4646    Create and return a temporary directory in
    4747    ``$HOME/.sage/temp/hostname/pid/``
     
    5050
    5151    INPUT:
    5252
    53     - ``name`` -- (default: "tmp_dir") A prefix for the directory name.
     53    - ``name`` -- (default: ``"dir_"``) A prefix for the directory name.
    5454
    55     - ``ext`` -- (default: "") A suffix for the directory name.
     55    - ``ext`` -- (default: ``""``) A suffix for the directory name.
    5656
    5757    OUTPUT:
    5858
     
    6868        sage: _ = open('file_inside_d', 'w')
    6969    """
    7070    from sage.misc.misc import SAGE_TMP
    71     tmp = tempfile.mkdtemp(prefix=name, suffix=ext, dir=SAGE_TMP)
     71    tmp = tempfile.mkdtemp(prefix=name, suffix=ext, dir=str(SAGE_TMP))
    7272    name = os.path.abspath(tmp)
    73     global temporary_directories
    74     temporary_directories.append(name)
    7573    return name + os.sep
    7674   
    7775
     
    7977# temporary filename
    8078#################################################################
    8179
    82 def tmp_filename(name="tmp_filename-", ext=""):
     80def tmp_filename(name="tmp_", ext=""):
    8381    r"""
    8482    Create and return a temporary file in
    8583    ``$HOME/.sage/temp/hostname/pid/``
     
    9694
    9795    INPUT:
    9896
    99     - ``name`` -- (default: "tmp_filename-") A prefix for the file name.
     97    - ``name`` -- (default: ``"tmp_"``) A prefix for the file name.
    10098
    101     - ``ext`` -- (default: "") A suffix for the file name.
     99    - ``ext`` -- (default: ``""``) A suffix for the file name.
    102100
    103101    OUTPUT:
    104102
     
    112110        sage: _ = open(fn, 'w')
    113111    """
    114112    from sage.misc.misc import SAGE_TMP
    115     handle, tmp = tempfile.mkstemp(prefix=name, suffix=ext, dir=SAGE_TMP)
     113    handle, tmp = tempfile.mkstemp(prefix=name, suffix=ext, dir=str(SAGE_TMP))
    116114    os.close(handle)
    117115    name = os.path.abspath(tmp)
    118     global temporary_files
    119     temporary_files.append(name)
    120116    return name
    121117
    122118def graphics_filename(ext='png'):
  • sage/plot/animate.py

    diff --git a/sage/plot/animate.py b/sage/plot/animate.py
    a b  
    22Animated plots
    33
    44EXAMPLES:
     5
    56We plot a circle shooting up to the right::
    67
    78    sage: a = animate([circle((i,i), 1-1/(i+1), hue=i/10) for i in srange(0,2,0.2)],
     
    1819import os
    1920
    2021from sage.structure.sage_object import SageObject
    21 from sage.misc.temporary_file import tmp_filename
     22from sage.misc.temporary_file import tmp_filename, tmp_dir
    2223import plot
    2324import sage.misc.misc
    2425import sage.misc.viewer
     
    252253            return self._png_dir
    253254        except AttributeError:
    254255            pass
    255         d = sage.misc.temporary_file.tmp_dir()
     256        d = tmp_dir()
    256257        G = self._frames
    257258        for i, frame in enumerate(self._frames):
    258259            filename = '%s/%s'%(d,sage.misc.misc.pad_zeros(i,8))
  • sage/tests/cmdline.py

    diff --git a/sage/tests/cmdline.py b/sage/tests/cmdline.py
    a b  
    5151
    5252from subprocess import *
    5353import os, select
    54 from sage.misc.cachefunc import cached_function
    55 
    56 
    57 @cached_function
    58 def get_tmpdir():
    59     """
    60     Return a safe temporary directory to run the executable in.
    61    
    62     EXAMPLES::
    63 
    64         sage: from sage.tests.cmdline import get_tmpdir
    65         sage: get_tmpdir()   # random output
    66         '/home/username/.sage/temp/hostname/pid/sage-test-cmdline-extension'
    67         sage: get_tmpdir() == get_tmpdir()  # test caching
    68         True
    69     """
    70     from sage.misc.temporary_file import tmp_dir
    71     return tmp_dir('sage-test-cmdline-')
    7254
    7355
    7456def test_executable(args, input="", timeout=50.0):
     
    9375
    9476    EXAMPLES::
    9577
    96         sage: from sage.tests.cmdline import test_executable, get_tmpdir
     78        sage: from sage.tests.cmdline import test_executable
    9779        sage: (out, err, ret) = test_executable(["cat"], "Hello World!")
    9880        sage: out
    9981        'Hello World!'
     
    239221
    240222    Test ``sage-run`` on a Python file, both with an absolute and with a relative path::
    241223
    242         sage: F = open(os.path.join(get_tmpdir(), 'python_test_file.py'), 'w')
     224        sage: dir = tmp_dir(); name = 'python_test_file.py'
     225        sage: fullname = os.path.join(dir, name)
     226        sage: F = open(fullname, 'w')
    243227        sage: F.write("print 3^33\n")
    244228        sage: F.close()
    245         sage: (out, err, ret) = test_executable(["sage", F.name])
     229        sage: (out, err, ret) = test_executable(["sage", fullname])
    246230        sage: print out
    247231        34
    248232        sage: err
    249233        ''
    250234        sage: ret
    251235        0
    252         sage: (dir,filename) = os.path.split(F.name)
    253236        sage: os.chdir(dir)
    254         sage: (out, err, ret) = test_executable(["sage", filename])
     237        sage: (out, err, ret) = test_executable(["sage", name])
    255238        sage: print out
    256239        34
    257240        sage: err
     
    262245    The same as above, but now with a ``.sage`` file.  This indirectly
    263246    also tests the preparser::
    264247   
    265         sage: sage_test_file = 'sage_test_file.sage'
    266         sage: F = open(os.path.join(get_tmpdir(), sage_test_file), 'w')
     248        sage: dir = tmp_dir(); name = 'sage_test_file.sage'
     249        sage: fullname = os.path.join(dir, name)
     250        sage: F = open(fullname, 'w')
    267251        sage: F.write("k.<a> = GF(5^3); print a^124\n")
    268252        sage: F.close()
    269         sage: (out, err, ret) = test_executable(["sage", F.name])
     253        sage: (out, err, ret) = test_executable(["sage", fullname])
    270254        sage: print out
    271255        1
    272256        sage: err
    273257        ''
    274258        sage: ret
    275259        0
    276         sage: (out, err, ret) = test_executable(["sage", sage_test_file])
     260        sage: os.chdir(dir)
     261        sage: (out, err, ret) = test_executable(["sage", name])
    277262        sage: print out
    278263        1
    279264        sage: err
     
    281266        sage: ret
    282267        0
    283268
    284     Testing "sage --preparse FILE" and "sage -t FILE".  First create a file and preparse it::
     269    Testing ``sage --preparse FILE`` and ``sage -t FILE``.  First create
     270    a file and preparse it::
    285271
    286272        sage: s = '\"\"\"\nThis is a test file.\n\"\"\"\ndef my_add(a,b):\n    \"\"\"\n    Add a to b.\n\n        EXAMPLES::\n\n            sage: my_add(2,2)\n            4\n        \"\"\"\n    return a+b\n'
    287         sage: script = os.path.join(get_tmpdir(), 'my_script.sage')
     273        sage: script = os.path.join(tmp_dir(), 'my_script.sage')
    288274        sage: script_py = script[:-5] + '.py'
    289275        sage: F = open(script, 'w')
    290276        sage: F.write(s)
     
    292278        sage: (out, err, ret) = test_executable(["sage", "--preparse", script])
    293279        sage: ret
    294280        0
    295         sage: os.path.exists(script_py)
     281        sage: os.path.isfile(script_py)
    296282        True
    297283
    298284    Now test my_script.sage and the preparsed version my_script.py::
     
    317303        sage: F.write(s)
    318304        sage: F.close()
    319305        sage: OLD_TESTDIR = os.environ['SAGE_TESTDIR']
    320         sage: os.environ['SAGE_TESTDIR'] = get_tmpdir()
     306        sage: os.environ['SAGE_TESTDIR'] = tmp_dir()
    321307        sage: (out, err, ret) = test_executable(["sage", "-t", script])
    322308        sage: ret
    323309        128
     
    517503        True
    518504   
    519505    """
    520     p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=get_tmpdir())
     506    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    521507    if input: p.stdin.write(input)
    522508    p.stdin.close()
    523509    fdout = p.stdout.fileno()