# 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
|
|
15 | 15 | import os |
16 | 16 | |
17 | 17 | import sage.misc.misc as misc |
18 | | F = '%s/spawned_processes'%misc.SAGE_TMP |
19 | 18 | |
20 | 19 | def cleaner(pid, cmd=''): |
21 | 20 | if cmd != '': |
22 | 21 | cmd = cmd.strip().split()[0] |
23 | 22 | # This is safe, since only this process writes to this file. |
| 23 | F = '%s/spawned_processes'%misc.SAGE_TMP |
24 | 24 | if os.path.exists(F): |
25 | 25 | o = open(F,'a') |
26 | 26 | else: |
27 | | if not os.path.exists(misc.SAGE_TMP): |
| 27 | if not os.path.exists(str(misc.SAGE_TMP)): |
28 | 28 | return |
29 | 29 | o = open(F,'w') |
30 | 30 | o.write('%s %s\n'%(pid, cmd)) |
diff --git a/sage/interfaces/quit.py b/sage/interfaces/quit.py
a
|
b
|
|
13 | 13 | ################################################################################ |
14 | 14 | |
15 | 15 | import os |
| 16 | from sage.misc.misc import SAGE_TMP |
16 | 17 | |
17 | 18 | expect_objects = [] |
18 | 19 | |
… |
… |
|
63 | 64 | |
64 | 65 | sage: sage.interfaces.quit.expect_quitall() |
65 | 66 | """ |
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') |
68 | 68 | if not os.path.exists(file): |
69 | 69 | return |
70 | 70 | for L in open(file).readlines(): |
diff --git a/sage/misc/dist.py b/sage/misc/dist.py
a
|
b
|
|
70 | 70 | |
71 | 71 | EXAMPLES:: |
72 | 72 | |
73 | | sage: install_scripts(SAGE_TMP, ignore_existing=True) |
| 73 | sage: install_scripts(str(SAGE_TMP), ignore_existing=True) # SAGE_TMP is lazy |
74 | 74 | Checking that Sage has the command 'gap' installed |
75 | 75 | ... |
76 | 76 | """ |
diff --git a/sage/misc/misc.py b/sage/misc/misc.py
a
|
b
|
|
133 | 133 | # It is called temp instead of tmp mainly for |
134 | 134 | # "historical reasons"... |
135 | 135 | |
136 | | SAGE_TMP='%s/temp/%s/%s/'%(DOT_SAGE, HOSTNAME, os.getpid()) |
| 136 | def lazy_string(func): |
| 137 | """ |
| 138 | Creates a lazy string that gets its values by evaluating a |
| 139 | function that takes no parameters. |
137 | 140 | |
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 |
| 154 | def SAGE_TMP(): |
| 155 | d = '%s/temp/%s/%s/'%(DOT_SAGE, HOSTNAME, os.getpid()) |
| 156 | sage_makedirs(d) |
| 157 | return d |
| 158 | |
| 159 | @lazy_string |
| 160 | def SPYX_TMP(): |
| 161 | return '%s/spyx/'%SAGE_TMP |
139 | 162 | |
140 | 163 | SAGE_DATA = '%s/data/'%SAGE_ROOT |
141 | 164 | SAGE_EXTCODE = '%s/data/extcode/'%SAGE_ROOT |
142 | | SPYX_TMP = '%s/spyx/'%SAGE_TMP |
143 | | |
144 | 165 | |
145 | 166 | def delete_tmpfiles(): |
146 | 167 | # !!!If you change this, see also SAGE_ROOT/local/bin/sage-doctest!!! |
147 | 168 | import shutil |
148 | 169 | 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)) |
151 | 172 | except OSError, msg: |
152 | 173 | print msg |
153 | 174 | pass |
154 | 175 | |
155 | | SAGE_TMP_INTERFACE='%s/interface/'%SAGE_TMP |
156 | | sage_makedirs(SAGE_TMP_INTERFACE) |
| 176 | @lazy_string |
| 177 | def SAGE_TMP_INTERFACE(): |
| 178 | d = '%s/interface/'%SAGE_TMP |
| 179 | sage_makedirs(d) |
| 180 | return d |
157 | 181 | |
158 | 182 | SAGE_DB = '%s/db'%DOT_SAGE |
159 | 183 | sage_makedirs(SAGE_DB) |
diff --git a/sage/tests/cmdline.py b/sage/tests/cmdline.py
a
|
b
|
|
257 | 257 | sage: F = open(script, 'w') |
258 | 258 | sage: F.write(s) |
259 | 259 | sage: F.close() |
260 | | sage: os.chdir(SAGE_TMP) |
| 260 | sage: os.chdir(str(SAGE_TMP)) # SAGE_TMP is lazy |
261 | 261 | sage: (out, err, ret) = test_executable(["sage", "--preparse", script]) |
262 | 262 | sage: ret |
263 | 263 | 0 |
… |
… |
|
286 | 286 | sage: F.write(s) |
287 | 287 | sage: F.close() |
288 | 288 | 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 |
290 | 290 | sage: (out, err, ret) = test_executable(["sage", "-t", script]) |
291 | 291 | sage: ret |
292 | 292 | 128 |