/
ipynb_runner.py
executable file
·176 lines (151 loc) · 5.27 KB
/
ipynb_runner.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ipython
# Copyright (c) 2015, GEM Foundation.
#
# OpenQuake ipynb_runner is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
from pprint import pprint
from jupyter_client import KernelManager
from Queue import Empty
from nbformat import NotebookNode, reads
def run_cell(kc, cell, tout):
retval = kc.execute(cell.input)
retval = kc.get_shell_msg(timeout=tout)
print "CONTENT_STATUS: %s" % retval['content']['status']
if retval['content']['status'] == 'error':
print "ENAME: "
pprint(retval['content']['ename'])
print "EVALUE:"
pprint(retval['content']['evalue'])
print "TRACEBACK:"
for i in retval['content']['traceback']:
print i
outs = []
while True:
try:
msg = kc.get_iopub_msg(timeout=0.5)
except Empty:
break
msg_type = msg['msg_type']
if msg_type in ('status', 'execute_input'):
continue
elif msg_type == 'execute_input':
continue
elif msg_type == 'clear_output':
outs = []
continue
content = msg['content']
# print msg_type, content
out = NotebookNode(output_type=msg_type)
if msg_type == 'stream':
out.stream = content['name']
out.text = content['text']
elif msg_type in ('display_data', 'execute_result'):
out['metadata'] = content['metadata']
for mime, data in content['data'].iteritems():
attr = mime.split('/')[-1].lower()
# this gets most right, but fix svg+html, plain
attr = attr.replace('+xml', '').replace('plain', 'text')
setattr(out, attr, data)
if msg_type == 'execute_result':
out.prompt_number = content['execution_count']
elif msg_type == 'error':
out.ename = content['ename']
out.evalue = content['evalue']
out.traceback = content['traceback']
else:
print "unhandled iopub msg:", msg_type
# print "msg_type: %s" % msg_type
outs.append(out)
return retval['content']['status'], outs
def run_notebook(notebook):
f = open(notebook)
if not f:
return False
nb = reads(f.read(), 3)
km = KernelManager()
km.start_kernel(extra_arguments=['--pylab=inline'],
stderr=open('/tmp/km.stderr', 'w'))
kc = km.client()
kc.start_channels()
shell = kc.shell_channel
shell.get_msg()
successes = 0
failures = 0
errors = 0
for ws in nb.worksheets:
for cell in ws.cells:
if cell.cell_type != 'code':
continue
try:
status, outs = run_cell(kc, cell, 30)
except Exception as e:
# currently turned off to avoid jenkins hang
# print "failed to run cell:", repr(e)
# print cell.input
# print dir(cell)
errors += 1
continue
failed = False
# currently turned off to avoid jenkins hang
# print "Count outs: %d" % len(outs)
# print "Count cell_out: %d" % len(cell.outputs)
#for out, ref in zip(outs, cell.outputs):
# print "OUT[%s]" % outs
# print "EXP[%s]" % ref
# #if not compare_outputs(out, ref):
# # failed = True
# # break
if status != "ok" or failed:
failures += 1
else:
successes += 1
print
print "tested notebook %s" % nb.metadata.name
print " %3i cells successfully replicated" % successes
if failures:
print " %3i cells mismatched output" % failures
if errors:
print " %3i cells failed to complete" % errors
kc.stop_channels()
km.shutdown_kernel()
del km
if failures > 0 or errors > 0:
return False
else:
return True
def get_ipnb(spath):
li = []
if os.path.isfile(spath):
if spath.endswith('.ipynb'):
li.append(spath)
return li
elif os.path.isdir(spath):
if os.path.basename(spath) == '.ipynb_checkpoints':
return li
for root, subf, fnames in os.walk(spath):
if os.path.basename(root) == '.ipynb_checkpoints':
continue
for fname in fnames:
if fname.endswith('.ipynb'):
li.append(os.path.join(root, fname))
return li
def run_notebooks(paths):
notebooks = []
for f in paths:
notebooks += get_ipnb(f)
for notebook in notebooks:
run_notebook(notebook)
if __name__ == '__main__':
run_notebooks(sys.argv[1:])