-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·262 lines (221 loc) · 7.4 KB
/
test.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env python3
#
# Tests all notebooks
#
import os
import re
import subprocess
import sys
import warnings
import lxml.etree
import markdown
import nbconvert
import requests
# Natural sort regex
_natural_sort_regex = re.compile(r'([0-9]+)')
# Don't check links twice
_checked_links = {}
def test_notebooks(links=False):
"""
Tests all example notebooks.
If ``links==True`` the notebook code will not be tested, but the notebook
links will be tested instead.
"""
# Known errors, or directories to avoid
ignore = [
'data',
'figures',
'models',
'venv',
]
# Work in progress
ignore.extend([
# 'real-data-2-capacitance-and-resistance.ipynb',
# 'real-data-3-xxx.ipynb',
'0-1-before-you-begin.ipynb',
])
# Extensions to check
allowed_extensions = ['.ipynb']
if links:
allowed_extensions.append('.md')
# Scan directory, running notebooks as we find them.
def scan(root, failed=None):
if failed is None:
failed = []
for filename in sorted(os.listdir(root), key=natural_sort_key):
if filename in ignore:
continue
path = os.path.join(root, filename)
# Test notebooks
if os.path.splitext(filename)[1] in allowed_extensions:
print('Testing ' + path + '.'*(max(0, 70 - len(path))), end='')
sys.stdout.flush()
if links:
res = check_links(root, filename)
else:
res = test_notebook(root, filename)
if res is None:
print('ok')
else:
if links:
failed.append((path, res))
else:
failed.append((path, *res))
print('FAIL')
# Recurse into subdirectories
elif os.path.isdir(path):
# Ignore hidden directories
if filename[:1] == '.':
continue
scan(path, failed)
return failed
failed = scan('.')
if failed:
if links:
for path, msg in failed:
print('-' * 79)
print('Link issues in: ' + path)
print(msg.strip())
print()
else:
for path, stdout, stderr in failed:
print('-' * 79)
print('Error output for: ' + path)
print((stdout + stderr).replace('\\n', '\n').strip())
print()
print('-' * 79)
print('Test failed (' + str(len(failed)) + ') error(s).')
return False
print('Test passed.')
return True
def test_notebook(root, path):
"""
Tests a notebook in a subprocess; returns ``None`` if it passes or a tuple
(stdout, stderr) if it fails.
"""
# Load notebook, convert to python
e = nbconvert.exporters.PythonExporter()
code, _ = e.from_filename(os.path.join(root, path))
# Remove coding statement, if present
code = '\n'.join([x for x in code.splitlines() if x[:9] != '# coding'])
# Tell matplotlib not to produce any figures
env = os.environ.copy()
env['MPLBACKEND'] = 'Template'
# Run in subprocess
cmd = [sys.executable, '-c', code]
curdir = os.getcwd()
try:
os.chdir(root)
p = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env
)
stdout, stderr = p.communicate()
# TODO: Use p.communicate(timeout=3600) if Python3 only
if p.returncode != 0:
# Show failing code, output and errors before returning
return (stdout.decode('utf-8'), stderr.decode('utf-8'))
except KeyboardInterrupt:
p.terminate()
return ('', 'Keyboard Interrupt')
finally:
os.chdir(curdir)
return None
def check_links(root, path):
"""
Checks all (Markdown) links in a given notebook, and checks that they
resolve. Returns ``None`` if succesfull, or an error message if one or more
links are broken.
"""
# Non-local link roots to convert to local root
convert = [
'https://nbviewer.jupyter.org/github/myokit/myokit-examples/blob/main/'
]
# Load contents
if os.path.splitext(path)[1].lower() == '.ipynb':
# Convert notebook
e = nbconvert.exporters.MarkdownExporter()
code, _ = e.from_filename(os.path.join(root, path))
else:
# Load as plain text
with open(os.path.join(root, path), 'r') as f:
code = f.read()
# Method to check a local link
def check_local(href):
if href[:1] == '/':
# Root = current working dir, not system root!
href = href[1:]
else:
# All else, interpret as local path relative to checked file
href = os.path.join(root, href)
if not os.path.exists(href):
return f'Unknown path: {href}'
# Method to check a remote link
def check_remote(href):
# Get cached response
try:
response = _checked_links[href]
except Exception:
response = None
# Make a HEAD request
if response is None:
try:
response = requests.head(href).status_code
except requests.exceptions.SSLError:
# Ignore SSL certificates that can't be retrieved
warnings.warn(f'SSLError when checking {href}')
response = 'ssl-error'
_checked_links[href] = response
if response not in [200, 301, 302, 'ssl-error']:
return f'HTTP {response}: {href}'
# Convert markdown to html
html = '<body>' + markdown.markdown(code) + '</body>'
doc = lxml.etree.fromstring(html)
# Scan over links, create error message
errors = []
for href in doc.xpath('//a//@href|//img//@src'):
# Ignore figures inside notebook
if href[:7] == 'output_' and href[-4:] == '.png':
continue
if href[:7] == 'output_' and href[-4:] == '.svg':
continue
# Convert selected http/https links to local root
for pre in convert:
if href.startswith(pre):
href = href[len(pre):]
if href[:1] != '/':
href = '/' + href
# Check link
if href[:7].lower() in ['http://', 'https:/']:
ok = check_remote(href)
else:
ok = check_local(href)
if ok is not None:
errors.append(ok)
# Join error messages and return
if errors:
return '\n'.join(errors)
return None
def natural_sort_key(s):
"""
Function to use as ``key`` in a sort, to get natural sorting of strings
(e.g. "2" before "10").
Example::
names.sort(key=natural_sort_key)
"""
# Code adapted from: http://stackoverflow.com/questions/4836710/
return [
int(text) if text.isdigit() else text.lower()
for text in _natural_sort_regex.split(s)]
if __name__ == '__main__':
links = 'links' in sys.argv
if links:
print('Checking links in all notebooks')
else:
print('Running all notebooks!')
print('This is used for regular online testing.')
print('If you are not interested in testing the notebooks,')
print()
print(' Press Ctrl+C to abort.')
print()
if not test_notebooks(links):
sys.exit(1)