Commit e37f62dc authored by Alexandre Duret-Lutz's avatar Alexandre Duret-Lutz

python: have %%dve and %%pml honor SPOT_TMPDIR and TMPDIR

* python/spot/aux.py (tmpdir): New context manager.
* python/spot/ltsmin.i: Use it for the two magics.
* NEWS: Mention this.
parent b136b81c
...@@ -127,6 +127,10 @@ New in spot 2.0.3a (not yet released) ...@@ -127,6 +127,10 @@ New in spot 2.0.3a (not yet released)
with spins, and dynamically load them. This is with spins, and dynamically load them. This is
akin to the %%dve magic that was already supported. akin to the %%dve magic that was already supported.
* The %%dve and %%pml magics honor the SPOT_TMPDIR and
TMPDIR environment variables. This especially helps
when the current directory is read-only.
Documentation: Documentation:
* A new example page shows how to test the equivalence of * A new example page shows how to test the equivalence of
......
...@@ -26,6 +26,7 @@ import subprocess ...@@ -26,6 +26,7 @@ import subprocess
import sys import sys
import os import os
import errno import errno
import contextlib
def extend(*classes): def extend(*classes):
""" """
...@@ -80,3 +81,13 @@ def rm_f(filename): ...@@ -80,3 +81,13 @@ def rm_f(filename):
except OSError as e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise raise
@contextlib.contextmanager
def tmpdir():
cwd = os.getcwd()
tmpdir = os.environ.get('SPOT_TMPDIR') or os.environ.get('TMPDIR') or '.'
try:
os.chdir(tmpdir)
yield
finally:
os.chdir(cwd)
...@@ -149,28 +149,27 @@ try: ...@@ -149,28 +149,27 @@ try:
def dve(self, line, cell): def dve(self, line, cell):
if not line: if not line:
raise ValueError("missing variable name for %%dve") raise ValueError("missing variable name for %%dve")
# DiViNe prefers when files are in the current directory with spot.aux.tmpdir():
# so write cell into local file with tempfile.NamedTemporaryFile(dir='.', suffix='.dve') as t:
with tempfile.NamedTemporaryFile(dir='.', suffix='.dve') as t: t.write(cell.encode('utf-8'))
t.write(cell.encode('utf-8')) t.flush()
t.flush()
try:
try: p = subprocess.Popen(['divine', 'compile',
p = subprocess.Popen(['divine', 'compile', '--ltsmin', t.name],
'--ltsmin', t.name], stdout=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
stderr=subprocess.STDOUT, universal_newlines=True)
universal_newlines=True) out = p.communicate()
out = p.communicate() if out[0]:
if out[0]: print(out[0], file=sys.stderr)
print(out[0], file=sys.stderr) ret = p.wait()
ret = p.wait() if ret:
if ret: raise subprocess.CalledProcessError(ret, 'divine')
raise subprocess.CalledProcessError(ret, 'divine') self.shell.user_ns[line] = load(t.name + '2C')
self.shell.user_ns[line] = load(t.name + '2C') finally:
finally: spot.aux.rm_f(t.name + '.cpp')
spot.aux.rm_f(t.name + '.cpp') spot.aux.rm_f(t.name + '2C')
spot.aux.rm_f(t.name + '2C')
@magics_class @magics_class
class EditPML(Magics): class EditPML(Magics):
...@@ -179,25 +178,26 @@ try: ...@@ -179,25 +178,26 @@ try:
def pml(self, line, cell): def pml(self, line, cell):
if not line: if not line:
raise ValueError("missing variable name for %%pml") raise ValueError("missing variable name for %%pml")
with tempfile.NamedTemporaryFile(dir='.', suffix='.pml') as t: with spot.aux.tmpdir():
t.write(cell.encode('utf-8')) with tempfile.NamedTemporaryFile(dir='.', suffix='.pml') as t:
t.flush() t.write(cell.encode('utf-8'))
t.flush()
try:
p = subprocess.Popen(['spins', t.name], try:
stdout=subprocess.PIPE, p = subprocess.Popen(['spins', t.name],
stderr=subprocess.STDOUT, stdout=subprocess.PIPE,
universal_newlines=True) stderr=subprocess.STDOUT,
out = p.communicate() universal_newlines=True)
if out[0]: out = p.communicate()
print(out[0], file=sys.stderr) if out[0]:
ret = p.wait() print(out[0], file=sys.stderr)
if ret: ret = p.wait()
raise subprocess.CalledProcessError(ret, 'spins') if ret:
self.shell.user_ns[line] = load(t.name + '.spins') raise subprocess.CalledProcessError(ret, 'spins')
finally: self.shell.user_ns[line] = load(t.name + '.spins')
spot.aux.rm_f(t.name + '.spins.c') finally:
spot.aux.rm_f(t.name + '.spins') spot.aux.rm_f(t.name + '.spins.c')
spot.aux.rm_f(t.name + '.spins')
ip = get_ipython() ip = get_ipython()
ip.register_magics(EditDVE) ip.register_magics(EditDVE)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment