|
|
@ -1,5 +1,5 @@
|
|
|
|
from ipykernel.kernelbase import Kernel
|
|
|
|
from ipykernel.kernelbase import Kernel
|
|
|
|
from pexpect import replwrap, EOF
|
|
|
|
from pexpect import replwrap, EOF, spawn
|
|
|
|
|
|
|
|
|
|
|
|
from subprocess import check_output
|
|
|
|
from subprocess import check_output
|
|
|
|
from os import unlink
|
|
|
|
from os import unlink
|
|
|
@ -10,7 +10,7 @@ import re
|
|
|
|
import signal
|
|
|
|
import signal
|
|
|
|
import urllib
|
|
|
|
import urllib
|
|
|
|
|
|
|
|
|
|
|
|
__version__ = '0.2'
|
|
|
|
__version__ = '0.0.1dev1'
|
|
|
|
|
|
|
|
|
|
|
|
version_pat = re.compile(r'version (\d+(\.\d+)+)')
|
|
|
|
version_pat = re.compile(r'version (\d+(\.\d+)+)')
|
|
|
|
|
|
|
|
|
|
|
@ -19,10 +19,11 @@ from .images import (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BashKernel(Kernel):
|
|
|
|
class MagmaKernel(Kernel):
|
|
|
|
implementation = 'bash_kernel'
|
|
|
|
implementation = 'magma_kernel'
|
|
|
|
implementation_version = __version__
|
|
|
|
implementation_version = __version__
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
@property
|
|
|
|
@property
|
|
|
|
def language_version(self):
|
|
|
|
def language_version(self):
|
|
|
|
m = version_pat.search(self.banner)
|
|
|
|
m = version_pat.search(self.banner)
|
|
|
@ -35,29 +36,39 @@ class BashKernel(Kernel):
|
|
|
|
if self._banner is None:
|
|
|
|
if self._banner is None:
|
|
|
|
self._banner = check_output(['bash', '--version']).decode('utf-8')
|
|
|
|
self._banner = check_output(['bash', '--version']).decode('utf-8')
|
|
|
|
return self._banner
|
|
|
|
return self._banner
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
language_info = {'name': 'bash',
|
|
|
|
@property
|
|
|
|
'codemirror_mode': 'shell',
|
|
|
|
def banner(self):
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
language_info = {'name': 'magma',
|
|
|
|
|
|
|
|
'codemirror_mode': 'magma',
|
|
|
|
'mimetype': 'text/x-sh',
|
|
|
|
'mimetype': 'text/x-sh',
|
|
|
|
'file_extension': '.sh'}
|
|
|
|
'file_extension': '.mgm'}
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
Kernel.__init__(self, **kwargs)
|
|
|
|
Kernel.__init__(self, **kwargs)
|
|
|
|
self._start_bash()
|
|
|
|
self._start_magma()
|
|
|
|
|
|
|
|
|
|
|
|
def _start_bash(self):
|
|
|
|
def _start_magma(self):
|
|
|
|
# Signal handlers are inherited by forked processes, and we can't easily
|
|
|
|
# Signal handlers are inherited by forked processes, and we can't easily
|
|
|
|
# reset it from the subprocess. Since kernelapp ignores SIGINT except in
|
|
|
|
# reset it from the subprocess. Since kernelapp ignores SIGINT except in
|
|
|
|
# message handlers, we need to temporarily reset the SIGINT handler here
|
|
|
|
# message handlers, we need to temporarily reset the SIGINT handler here
|
|
|
|
# so that bash and its children are interruptible.
|
|
|
|
# so that bash and its children are interruptible.
|
|
|
|
sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
|
|
sig = signal.signal(signal.SIGINT, signal.SIG_DFL)
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
self.bashwrapper = replwrap.bash()
|
|
|
|
magma = spawn('magma', echo=False, encoding='utf-8')
|
|
|
|
|
|
|
|
magma.expect('> ')
|
|
|
|
|
|
|
|
magma.sendline('SetLineEditor(false);')
|
|
|
|
|
|
|
|
magma.expect('> ')
|
|
|
|
|
|
|
|
magma.sendline('')
|
|
|
|
|
|
|
|
self.magmawrapper = replwrap.REPLWrapper(magma, '> ', 'SetPrompt("{}");')
|
|
|
|
finally:
|
|
|
|
finally:
|
|
|
|
signal.signal(signal.SIGINT, sig)
|
|
|
|
signal.signal(signal.SIGINT, sig)
|
|
|
|
|
|
|
|
|
|
|
|
# Register Bash function to write image data to temporary file
|
|
|
|
# Register Bash function to write image data to temporary file
|
|
|
|
self.bashwrapper.run_command(image_setup_cmd)
|
|
|
|
# self.magmawrapper.run_command(image_setup_cmd)
|
|
|
|
|
|
|
|
|
|
|
|
def do_execute(self, code, silent, store_history=True,
|
|
|
|
def do_execute(self, code, silent, store_history=True,
|
|
|
|
user_expressions=None, allow_stdin=False):
|
|
|
|
user_expressions=None, allow_stdin=False):
|
|
|
@ -67,24 +78,25 @@ class BashKernel(Kernel):
|
|
|
|
|
|
|
|
|
|
|
|
interrupted = False
|
|
|
|
interrupted = False
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
output = self.bashwrapper.run_command(code.rstrip(), timeout=None)
|
|
|
|
output = self.magmawrapper.run_command(code.rstrip(), timeout=None)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.bashwrapper.child.sendintr()
|
|
|
|
self.magmawrapper.child.sendintr()
|
|
|
|
interrupted = True
|
|
|
|
interrupted = True
|
|
|
|
self.bashwrapper._expect_prompt()
|
|
|
|
self.magmawrapper._expect_prompt()
|
|
|
|
output = self.bashwrapper.child.before
|
|
|
|
output = self.magmawrapper.child.before
|
|
|
|
except EOF:
|
|
|
|
except EOF:
|
|
|
|
output = self.bashwrapper.child.before + 'Restarting Bash'
|
|
|
|
output = self.magmawrapper.child.before + 'Restarting Bash'
|
|
|
|
self._start_bash()
|
|
|
|
self._start_magma()
|
|
|
|
|
|
|
|
|
|
|
|
if not silent:
|
|
|
|
if not silent:
|
|
|
|
image_filenames, output = extract_image_filenames(output)
|
|
|
|
#image_filenames, output = extract_image_filenames(output)
|
|
|
|
|
|
|
|
|
|
|
|
# Send standard output
|
|
|
|
# Send standard output
|
|
|
|
stream_content = {'name': 'stdout', 'text': output}
|
|
|
|
stream_content = {'name': 'stdout', 'text': output}
|
|
|
|
self.send_response(self.iopub_socket, 'stream', stream_content)
|
|
|
|
self.send_response(self.iopub_socket, 'stream', stream_content)
|
|
|
|
|
|
|
|
|
|
|
|
# Send images, if any
|
|
|
|
# Send images, if any
|
|
|
|
|
|
|
|
'''
|
|
|
|
for filename in image_filenames:
|
|
|
|
for filename in image_filenames:
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
data = display_data_for_image(filename)
|
|
|
|
data = display_data_for_image(filename)
|
|
|
@ -93,10 +105,12 @@ class BashKernel(Kernel):
|
|
|
|
self.send_response(self.iopub_socket, 'stream', message)
|
|
|
|
self.send_response(self.iopub_socket, 'stream', message)
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
self.send_response(self.iopub_socket, 'display_data', data)
|
|
|
|
self.send_response(self.iopub_socket, 'display_data', data)
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
if interrupted:
|
|
|
|
if interrupted:
|
|
|
|
return {'status': 'abort', 'execution_count': self.execution_count}
|
|
|
|
return {'status': 'abort', 'execution_count': self.execution_count}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
exitcode = int(self.bashwrapper.run_command('echo $?').rstrip())
|
|
|
|
exitcode = int(self.bashwrapper.run_command('echo $?').rstrip())
|
|
|
|
except Exception:
|
|
|
|
except Exception:
|
|
|
@ -110,45 +124,6 @@ class BashKernel(Kernel):
|
|
|
|
error_content['status'] = 'error'
|
|
|
|
error_content['status'] = 'error'
|
|
|
|
return error_content
|
|
|
|
return error_content
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
|
|
|
|
'''
|
|
|
|
return {'status': 'ok', 'execution_count': self.execution_count,
|
|
|
|
return {'status': 'ok', 'execution_count': self.execution_count,
|
|
|
|
'payload': [], 'user_expressions': {}}
|
|
|
|
'payload': [], 'user_expressions': {}}
|
|
|
|
|
|
|
|
|
|
|
|
def do_complete(self, code, cursor_pos):
|
|
|
|
|
|
|
|
code = code[:cursor_pos]
|
|
|
|
|
|
|
|
default = {'matches': [], 'cursor_start': 0,
|
|
|
|
|
|
|
|
'cursor_end': cursor_pos, 'metadata': dict(),
|
|
|
|
|
|
|
|
'status': 'ok'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not code or code[-1] == ' ':
|
|
|
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tokens = code.replace(';', ' ').split()
|
|
|
|
|
|
|
|
if not tokens:
|
|
|
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
matches = []
|
|
|
|
|
|
|
|
token = tokens[-1]
|
|
|
|
|
|
|
|
start = cursor_pos - len(token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if token[0] == '$':
|
|
|
|
|
|
|
|
# complete variables
|
|
|
|
|
|
|
|
cmd = 'compgen -A arrayvar -A export -A variable %s' % token[1:] # strip leading $
|
|
|
|
|
|
|
|
output = self.bashwrapper.run_command(cmd).rstrip()
|
|
|
|
|
|
|
|
completions = set(output.split())
|
|
|
|
|
|
|
|
# append matches including leading $
|
|
|
|
|
|
|
|
matches.extend(['$'+c for c in completions])
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
# complete functions and builtins
|
|
|
|
|
|
|
|
cmd = 'compgen -cdfa %s' % token
|
|
|
|
|
|
|
|
output = self.bashwrapper.run_command(cmd).rstrip()
|
|
|
|
|
|
|
|
matches.extend(output.split())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if not matches:
|
|
|
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
matches = [m for m in matches if m.startswith(token)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {'matches': sorted(matches), 'cursor_start': start,
|
|
|
|
|
|
|
|
'cursor_end': cursor_pos, 'metadata': dict(),
|
|
|
|
|
|
|
|
'status': 'ok'}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|