Mercurial > hg > Members > aka > jupyter_CbC_kernel
annotate jupyter_c_kernel/kernel.py @ 52:0ef931211f77
Added cflags and ldflags magics
author | Ben Spoor <ben.spoor@altran.com> |
---|---|
date | Wed, 12 Apr 2017 15:32:07 +0000 |
parents | a98647258349 |
children | 7857f8eea835 |
rev | line source |
---|---|
39
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
1 from queue import Queue |
33 | 2 from threading import Thread |
3 | |
0 | 4 from ipykernel.kernelbase import Kernel |
5 import subprocess | |
6 import tempfile | |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
7 import os |
36
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
8 import os.path as path |
34 | 9 |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
10 |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
11 class RealTimeSubprocess(subprocess.Popen): |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
12 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
13 A subprocess that allows to read its stdout and stderr in real time |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
14 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
15 |
34 | 16 def __init__(self, cmd, write_to_stdout, write_to_stderr): |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
17 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
18 :param cmd: the command to execute |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
19 :param write_to_stdout: a callable that will be called with chunks of data from stdout |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
20 :param write_to_stderr: a callable that will be called with chunks of data from stderr |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
21 """ |
34 | 22 self._write_to_stdout = write_to_stdout |
23 self._write_to_stderr = write_to_stderr | |
24 | |
37
aac47bc07111
Fix subprocess bufsize & master compilation
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
36
diff
changeset
|
25 super().__init__(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=0) |
34 | 26 |
27 self._stdout_queue = Queue() | |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
28 self._stdout_thread = Thread(target=RealTimeSubprocess._enqueue_output, args=(self.stdout, self._stdout_queue)) |
34 | 29 self._stdout_thread.daemon = True |
30 self._stdout_thread.start() | |
31 | |
32 self._stderr_queue = Queue() | |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
33 self._stderr_thread = Thread(target=RealTimeSubprocess._enqueue_output, args=(self.stderr, self._stderr_queue)) |
34 | 34 self._stderr_thread.daemon = True |
35 self._stderr_thread.start() | |
36 | |
37 @staticmethod | |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
38 def _enqueue_output(stream, queue): |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
39 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
40 Add chunks of data from a stream to a queue until the stream is empty. |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
41 """ |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
42 for line in iter(lambda: stream.read(4096), b''): |
34 | 43 queue.put(line) |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
44 stream.close() |
34 | 45 |
35 | 46 def write_contents(self): |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
47 """ |
39
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
48 Write the available content from stdin and stderr where specified when the instance was created |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
49 :return: |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
50 """ |
39
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
51 |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
52 def read_all_from_queue(queue): |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
53 res = b'' |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
54 size = queue.qsize() |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
55 while size != 0: |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
56 res += queue.get_nowait() |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
57 size -= 1 |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
58 return res |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
59 |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
60 stdout_contents = read_all_from_queue(self._stdout_queue) |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
61 if stdout_contents: |
34 | 62 self._write_to_stdout(stdout_contents) |
39
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
63 stderr_contents = read_all_from_queue(self._stderr_queue) |
fecdf8733f3b
Make sure to empty the queue so no message should be left over in it
Brendan Rius <brendan@omixy.com>
parents:
38
diff
changeset
|
64 if stderr_contents: |
34 | 65 self._write_to_stderr(stderr_contents) |
0 | 66 |
67 | |
68 class CKernel(Kernel): | |
20 | 69 implementation = 'jupyter_c_kernel' |
0 | 70 implementation_version = '1.0' |
71 language = 'c' | |
72 language_version = 'C11' | |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
73 language_info = {'name': 'c', |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
74 'mimetype': 'text/plain', |
50
a98647258349
solve error “traitlets.traitlets.TraitError: FileExtension trait 'file_extension' does not begin with a dot: ‘c’” when exporting to script with nbconvert
Davide Bortolami <d@fermiumlabs.com>
parents:
39
diff
changeset
|
75 'file_extension': '.c'} |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
76 banner = "C kernel.\n" \ |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
77 "Uses gcc, compiles in C11, and creates source code files and executables in temporary folder.\n" |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
78 |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
79 def __init__(self, *args, **kwargs): |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
80 super(CKernel, self).__init__(*args, **kwargs) |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
81 self.files = [] |
36
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
82 mastertemp = tempfile.mkstemp(suffix='.out') |
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
83 os.close(mastertemp[0]) |
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
84 self.master_path = mastertemp[1] |
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
85 filepath = path.join(path.dirname(path.realpath(__file__)), '..', 'resources', 'master.c') |
37
aac47bc07111
Fix subprocess bufsize & master compilation
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
36
diff
changeset
|
86 subprocess.call(['gcc', filepath, '-std=c11', '-rdynamic', '-ldl', '-o', self.master_path]) |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
87 |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
88 def cleanup_files(self): |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
89 """Remove all the temporary files created by the kernel""" |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
90 for file in self.files: |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
91 os.remove(file) |
36
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
92 os.remove(self.master_path) |
0 | 93 |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
94 def new_temp_file(self, **kwargs): |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
95 """Create a new temp file to be deleted when the kernel shuts down""" |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
96 # We don't want the file to be deleted when closed, but only when the kernel stops |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
97 kwargs['delete'] = False |
9 | 98 kwargs['mode'] = 'w' |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
99 file = tempfile.NamedTemporaryFile(**kwargs) |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
100 self.files.append(file.name) |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
101 return file |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
102 |
34 | 103 def _write_to_stdout(self, contents): |
104 self.send_response(self.iopub_socket, 'stream', {'name': 'stdout', 'text': contents}) | |
33 | 105 |
34 | 106 def _write_to_stderr(self, contents): |
107 self.send_response(self.iopub_socket, 'stream', {'name': 'stderr', 'text': contents}) | |
33 | 108 |
34 | 109 def create_jupyter_subprocess(self, cmd): |
38
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
110 return RealTimeSubprocess(cmd, |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
111 lambda contents: self._write_to_stdout(contents.decode()), |
cb587f21c8bb
Rename Jupytersubprocess and comment it
Brendan Rius <brendan@omixy.com>
parents:
37
diff
changeset
|
112 lambda contents: self._write_to_stderr(contents.decode())) |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
113 |
52
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
114 def compile_with_gcc(self, source_filename, binary_filename, cflags=None, ldflags=None): |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
115 cflags = ['-std=c11', '-fPIC', '-shared', '-rdynamic'] + cflags |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
116 args = ['gcc', source_filename] + cflags + ['-o', binary_filename] + ldflags |
34 | 117 return self.create_jupyter_subprocess(args) |
0 | 118 |
52
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
119 def _filter_magics(self, code): |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
120 |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
121 magics = {'cflags': [], 'ldflags': []} |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
122 |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
123 for line in code.splitlines(): |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
124 if line.startswith('//%'): |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
125 key, value = line[3:].split(":", 2) |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
126 key = key.strip().lower() |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
127 |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
128 if key in ['ldflags', 'cflags']: |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
129 for flag in value.split(): |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
130 magics[key] += [flag] |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
131 |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
132 return magics |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
133 |
0 | 134 def do_execute(self, code, silent, store_history=True, |
135 user_expressions=None, allow_stdin=False): | |
52
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
136 |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
137 magics = self._filter_magics(code) |
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
138 |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
139 with self.new_temp_file(suffix='.c') as source_file: |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
140 source_file.write(code) |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
141 source_file.flush() |
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
142 with self.new_temp_file(suffix='.out') as binary_file: |
52
0ef931211f77
Added cflags and ldflags magics
Ben Spoor <ben.spoor@altran.com>
parents:
50
diff
changeset
|
143 p = self.compile_with_gcc(source_file.name, binary_file.name, magics['cflags'], magics['ldflags']) |
33 | 144 while p.poll() is None: |
35 | 145 p.write_contents() |
146 p.write_contents() | |
33 | 147 if p.returncode != 0: # Compilation failed |
34 | 148 self._write_to_stderr( |
149 "[C kernel] GCC exited with code {}, the executable will not be executed".format( | |
150 p.returncode)) | |
33 | 151 return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [], |
152 'user_expressions': {}} | |
0 | 153 |
36
8acbab5a9f21
Remove stream buffering on stdout & stderr
Louis 'Kureuil' Person <louis.person@epitech.eu>
parents:
35
diff
changeset
|
154 p = self.create_jupyter_subprocess([self.master_path, binary_file.name]) |
33 | 155 while p.poll() is None: |
35 | 156 p.write_contents() |
157 p.write_contents() | |
33 | 158 |
159 if p.returncode != 0: | |
34 | 160 self._write_to_stderr("[C kernel] Executable exited with code {}".format(p.returncode)) |
2
b46b2e5b6c08
Improve error management by separating stdout and stderr
Brendan Rius <brendan@omixy.com>
parents:
0
diff
changeset
|
161 return {'status': 'ok', 'execution_count': self.execution_count, 'payload': [], 'user_expressions': {}} |
7
aa54c85303b6
Remove temporary files when shutting down the kernel
Brendan Rius <brendan@omixy.com>
parents:
4
diff
changeset
|
162 |
aa54c85303b6
Remove temporary files when shutting down the kernel
Brendan Rius <brendan@omixy.com>
parents:
4
diff
changeset
|
163 def do_shutdown(self, restart): |
aa54c85303b6
Remove temporary files when shutting down the kernel
Brendan Rius <brendan@omixy.com>
parents:
4
diff
changeset
|
164 """Cleanup the created source code files and executables when shutting down the kernel""" |
aa54c85303b6
Remove temporary files when shutting down the kernel
Brendan Rius <brendan@omixy.com>
parents:
4
diff
changeset
|
165 self.cleanup_files() |