python: add a %%pml magic

Fixes #162.

* python/spot/ltsmin.i: Implement the magic.
* NEWS: Mention it.
* tests/python/ltsmin-pml.ipynb: New file.
* tests/Makefile.am, doc/org/tut.org: Add it.
* tests/python/ipnbdoctest.py: Adjust.
This commit is contained in:
Alexandre Duret-Lutz 2016-06-12 12:53:55 +02:00
parent a7e4395f9d
commit 272daf62fc
6 changed files with 564 additions and 14 deletions

View file

@ -106,25 +106,31 @@ class model:
res += '\n';
return res
def require(tool):
def require(*tools):
"""
Exit with status code 77 if the required tool is not installed.
This function is mostly useful in Spot test suite, where 77 is a
code used to indicate that some test should be skipped.
"""
if tool != "divine":
raise ValueError("unsupported argument for require(): " + tool)
import shutil
if shutil.which("divine") == None:
print ("divine not available", file=sys.stderr)
sys.exit(77)
out = subprocess.check_output(['divine', 'compile',
'--help'], stderr=subprocess.STDOUT)
if b'LTSmin' not in out:
print ("divine available but no support for LTSmin",
file=sys.stderr)
sys.exit(77)
for tool in tools:
if tool == "divine":
if shutil.which("divine") == None:
print("divine not available", file=sys.stderr)
sys.exit(77)
out = subprocess.check_output(['divine', 'compile', '--help'],
stderr=subprocess.STDOUT)
if b'LTSmin' not in out:
print("divine available but no support for LTSmin",
file=sys.stderr)
sys.exit(77)
elif tool == "spins":
if shutil.which("spins") == None:
print("spins not available", file=sys.stderr)
sys.exit(77)
else:
raise ValueError("unsupported argument for require(): " + tool)
# Load IPython specific support if we can.
@ -166,8 +172,36 @@ try:
spot.aux.rm_f(t.name + '.cpp')
spot.aux.rm_f(t.name + '2C')
@magics_class
class EditPML(Magics):
@cell_magic
def pml(self, line, cell):
if not line:
raise ValueError("missing variable name for %%pml")
with tempfile.NamedTemporaryFile(dir='.', suffix='.pml') as t:
t.write(cell.encode('utf-8'))
t.flush()
try:
p = subprocess.Popen(['spins', t.name],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
out = p.communicate()
if out[0]:
print(out[0], file=sys.stderr)
ret = p.wait()
if ret:
raise subprocess.CalledProcessError(ret, 'spins')
self.shell.user_ns[line] = load(t.name + '.spins')
finally:
spot.aux.rm_f(t.name + '.spins.c')
spot.aux.rm_f(t.name + '.spins')
ip = get_ipython()
ip.register_magics(EditDVE)
ip.register_magics(EditPML)
except (ImportError, NameError):
pass