forked from landlab/tutorials
/
run_notebook.py
executable file
·199 lines (153 loc) · 5.52 KB
/
run_notebook.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
#! /usr/bin/env python
from __future__ import print_function
import os
import sys
import argparse
import tempfile
import subprocess
import re
from fnmatch import fnmatch
from six.moves import shlex_quote
from scripting.unix import system, check_output
from scripting.contexts import cd
from scripting.prompting import success, error, status
def convert_notebook(notebook):
script = check_output(['jupyter', 'nbconvert', '--to', 'python',
'--stdout', notebook])
p = re.compile(b"^get_ipython\(\)\.magic\(u?'matplotlib (?P<magic>\w+)'\)",
re.MULTILINE)
script = p.sub(b"# get_ipython().magic(u'matplotlib auto')", script)
p = re.compile(b"(?P<magic>^get_ipython\(\)\.magic\(u'pinfo[\w\s]+'\))",
re.MULTILINE)
script = p.sub(b"# \\1", script)
return script
def run_notebook(notebook):
with cd(os.path.dirname(notebook) or '.'):
script = convert_notebook(os.path.basename(notebook))
_, script_file = tempfile.mkstemp(prefix='.', suffix='.py', dir='.')
with open(script_file, 'wb') as fp:
fp.write(script)
try:
subprocess.check_call(['ipython', script_file])
except Exception:
raise
finally:
os.remove(script_file)
def print_summary(summary):
print('-' * 70)
for notebook, result in summary:
if result is None:
status('SKIP: ' + notebook)
elif result:
success(notebook)
else:
error(notebook)
print('-' * 70)
def print_success_or_failure(summary):
failures = [name for name, result in summary if result is False]
passes = [name for name, result in summary if result is True]
if failures:
print('FAILED (failures={nfails})'.format(nfails=len(failures)))
else:
print('OK Ran {ntests} tests'.format(ntests=len(passes)))
return len(failures)
def check_notebook(notebook, dry_run=False):
try:
dry_run or run_notebook(notebook)
except subprocess.CalledProcessError:
return False
else:
return True
def match_by_pattern(strings, patterns):
"""Match strings by patterns.
Parameters
----------
strings : iterable of str
Collection of strings.
patterns : iterable of str
List of glob-style patterns to remove.
Returns
-------
list of str
All strings that match any of the patterns.
"""
def string_matches_one_of(string, patterns):
for pattern in patterns:
if fnmatch(string, pattern):
return True
return False
matches = []
for string in strings:
if string_matches_one_of(string, patterns):
matches.append(string)
return matches
def read_notebooks_from_file(file_like):
if file_like:
return [nb.strip() for nb in file_like if nb.strip()]
else:
return []
def find_notebooks(base):
"""Find Jupyter notebooks.
Parameters
----------
base : str
Path to search for notebooks under.
Returns
-------
list of str
Paths to discovered notebooks.
"""
notebooks = []
for root, dirs, files in os.walk(base, topdown=True):
for dir in dirs:
if dir.startswith('.'):
dirs.remove(dir)
for fname in files:
file_path = os.path.join(root, fname)
if file_path.endswith('.ipynb'):
notebooks.append(file_path)
return notebooks
def find_command(args):
notebooks = find_notebooks(args.path)
if args.sort:
notebooks.sort()
print(os.linesep.join([nb for nb in notebooks]))
def run_command(args):
notebooks = read_notebooks_from_file(args.file) + args.notebook
excluded = match_by_pattern(notebooks, args.exclude)
summary = []
for notebook in notebooks:
if notebook in excluded:
result = notebook, None
else:
result = notebook, check_notebook(notebook, dry_run=args.dry_run)
print_summary([result])
summary.append(result)
print_summary(summary)
return print_success_or_failure(summary)
def main():
import argparse
parser = argparse.ArgumentParser(description='Run Jupyter notebooks.')
subparsers = parser.add_subparsers(title='subcommands',
description='valid subcommands',
help='additional help')
parser_find = subparsers.add_parser('find', help='Find notebooks.')
parser_find.set_defaults(func=find_command)
parser_find.add_argument('path', help='Path to search for notebooks.')
parser_find.add_argument('--sort', action='store_true',
help='Sort notebooks alphabetically.')
parser_run = subparsers.add_parser('run', help='Run notebooks.')
parser_run.set_defaults(func=run_command)
parser_run.add_argument('notebook', type=str, nargs='*', default=[],
help='Notebook to test.')
parser_run.add_argument('-e', '--exclude', metavar='PATTERN', type=str,
action='append', default=[],
help='Notebooks to exclude.')
parser_run.add_argument('--dry-run', action='store_true',
help='Find notebooks but do not do anything')
parser_run.add_argument('-f', '--file', type=argparse.FileType('r'),
help='Read notebooks from a file.')
args = parser.parse_args()
args.func(args)
if __name__ == '__main__':
sys.exit(main())