Ticket #13147: 13147_lazy_spkg.patch

File 13147_lazy_spkg.patch, 4.3 KB (added by ohanar, 9 years ago)

Lazy string solution for use with spkg

  • sage/interfaces/cleaner.py

    # HG changeset patch
    # User Mike Hansen <mhansen@gmail.com>
    # Date 1338284389 25200
    # Node ID bb37f43b5feaf166b5835cfccf17235fe6fbd94c
    # Parent  87014ea4c9ead601f74e4d4cb5770111d27093c9
    #12415: Make sage.misc.misc.SAGE_TMP a lazy string
    
    The purpose of this is so that the SAGE_TMP can still reflect the PID
    even after Sage forks.
    
    diff --git a/sage/interfaces/cleaner.py b/sage/interfaces/cleaner.py
    a b  
    1515import os
    1616
    1717import sage.misc.misc as misc
    18 F = '%s/spawned_processes'%misc.SAGE_TMP
    1918
    2019def cleaner(pid, cmd=''):
    2120    if cmd != '':
    2221        cmd = cmd.strip().split()[0]
    2322    # This is safe, since only this process writes to this file.
     23    F = '%s/spawned_processes'%misc.SAGE_TMP
    2424    if os.path.exists(F):
    2525        o = open(F,'a')
    2626    else:
    27         if not os.path.exists(misc.SAGE_TMP):
     27        if not os.path.exists(str(misc.SAGE_TMP)):
    2828            return
    2929        o = open(F,'w')
    3030    o.write('%s %s\n'%(pid, cmd))
  • sage/interfaces/quit.py

    diff --git a/sage/interfaces/quit.py b/sage/interfaces/quit.py
    a b  
    1313################################################################################
    1414
    1515import os
     16from sage.misc.misc import SAGE_TMP
    1617
    1718expect_objects = []
    1819
     
    6364
    6465        sage: sage.interfaces.quit.expect_quitall()
    6566    """
    66     from sage.misc.misc import SAGE_TMP
    67     file = os.path.join(SAGE_TMP, 'spawned_processes')
     67    file = os.path.join(str(SAGE_TMP), 'spawned_processes')
    6868    if not os.path.exists(file):
    6969        return
    7070    for L in open(file).readlines():
  • sage/misc/dist.py

    diff --git a/sage/misc/dist.py b/sage/misc/dist.py
    a b  
    7070
    7171    EXAMPLES::
    7272
    73         sage: install_scripts(SAGE_TMP, ignore_existing=True)
     73        sage: install_scripts(str(SAGE_TMP), ignore_existing=True) # SAGE_TMP is lazy
    7474        Checking that Sage has the command 'gap' installed
    7575        ...
    7676    """
  • sage/misc/misc.py

    diff --git a/sage/misc/misc.py b/sage/misc/misc.py
    a b  
    133133# It is called temp instead of tmp mainly for
    134134# "historical reasons"...
    135135
    136 SAGE_TMP='%s/temp/%s/%s/'%(DOT_SAGE, HOSTNAME, os.getpid())
     136def lazy_string(func):
     137    """
     138    Creates a lazy string that gets its values by evaluating a
     139    function that takes no parameters.
    137140
    138 sage_makedirs(SAGE_TMP)
     141    EXAMPLES::
     142
     143        sage: from sage.misc.misc import lazy_string
     144        sage: def foo(): return 'sage'
     145        sage: s = lazy_string(foo); s
     146        l'sage'
     147        sage: s + 'math'
     148        'sagemath'
     149    """
     150    from speaklater import make_lazy_string
     151    return make_lazy_string(func)
     152
     153@lazy_string
     154def SAGE_TMP():
     155    d = '%s/temp/%s/%s/'%(DOT_SAGE, HOSTNAME, os.getpid())
     156    sage_makedirs(d)
     157    return d
     158
     159@lazy_string
     160def SPYX_TMP():
     161    return '%s/spyx/'%SAGE_TMP
    139162
    140163SAGE_DATA = '%s/data/'%SAGE_ROOT
    141164SAGE_EXTCODE = '%s/data/extcode/'%SAGE_ROOT
    142 SPYX_TMP = '%s/spyx/'%SAGE_TMP
    143 
    144165
    145166def delete_tmpfiles():
    146167    # !!!If you change this, see also SAGE_ROOT/local/bin/sage-doctest!!!
    147168    import shutil
    148169    try:
    149         if os.path.exists(SAGE_TMP):
    150             shutil.rmtree(SAGE_TMP)
     170        if os.path.exists(str(SAGE_TMP)):
     171            shutil.rmtree(str(SAGE_TMP))
    151172    except OSError, msg:
    152173        print msg
    153174        pass
    154175
    155 SAGE_TMP_INTERFACE='%s/interface/'%SAGE_TMP
    156 sage_makedirs(SAGE_TMP_INTERFACE)
     176@lazy_string
     177def SAGE_TMP_INTERFACE():
     178    d = '%s/interface/'%SAGE_TMP
     179    sage_makedirs(d)
     180    return d
    157181
    158182SAGE_DB = '%s/db'%DOT_SAGE
    159183sage_makedirs(SAGE_DB)
  • sage/tests/cmdline.py

    diff --git a/sage/tests/cmdline.py b/sage/tests/cmdline.py
    a b  
    257257        sage: F = open(script, 'w')
    258258        sage: F.write(s)
    259259        sage: F.close()
    260         sage: os.chdir(SAGE_TMP)
     260        sage: os.chdir(str(SAGE_TMP)) # SAGE_TMP is lazy
    261261        sage: (out, err, ret) = test_executable(["sage", "--preparse", script])
    262262        sage: ret
    263263        0
     
    286286        sage: F.write(s)
    287287        sage: F.close()
    288288        sage: OLD_TESTDIR = os.environ['SAGE_TESTDIR']
    289         sage: os.environ['SAGE_TESTDIR'] = SAGE_TMP
     289        sage: os.environ['SAGE_TESTDIR'] = str(SAGE_TMP) # SAGE_TMP is lazy
    290290        sage: (out, err, ret) = test_executable(["sage", "-t", script])
    291291        sage: ret
    292292        128