Skip to content

Commit d327df5

Browse files
committed
Add option to read notebooks from a file.
1 parent 7670c71 commit d327df5

1 file changed

Lines changed: 49 additions & 9 deletions

File tree

run_notebook.py

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,35 +68,75 @@ def print_success_or_failure(summary):
6868
return len(failures)
6969

7070

71-
def check_notebook(notebook):
71+
def check_notebook(notebook, dry_run=False):
7272
try:
73-
run_notebook(notebook)
73+
dry_run or run_notebook(notebook)
7474
except subprocess.CalledProcessError:
7575
return False
7676
else:
7777
return True
7878

7979

80+
def match_by_pattern(strings, patterns):
81+
"""Match strings by patterns.
82+
83+
Parameters
84+
----------
85+
strings : iterable of str
86+
Collection of strings.
87+
patterns : iterable of str
88+
List of glob-style patterns to remove.
89+
90+
Returns
91+
-------
92+
list of str
93+
All strings that match any of the patterns.
94+
"""
95+
def string_matches_one_of(string, patterns):
96+
for pattern in patterns:
97+
if fnmatch(string, pattern):
98+
return True
99+
return False
100+
101+
matches = []
102+
for string in strings:
103+
if string_matches_one_of(string, patterns):
104+
matches.append(string)
105+
106+
return matches
107+
108+
109+
def read_notebooks_from_file(file_like):
110+
if file_like:
111+
return [nb.strip() for nb in file_like if nb.strip()]
112+
else:
113+
return []
114+
115+
80116
def main():
81117
import argparse
82-
parser = argparse.ArgumentParser()
83-
parser.add_argument('notebook', type=str, nargs='+',
118+
parser = argparse.ArgumentParser(
119+
description='Run Jupyter notebooks.')
120+
parser.add_argument('notebook', type=str, nargs='*',
84121
help='Notebook to test.')
85122
parser.add_argument('--skip', type=str, action='append', default=[],
86123
help='Notebooks to skip.')
124+
parser.add_argument('--dry-run', action='store_true',
125+
help='Find notebooks but do not do anything')
126+
parser.add_argument('--file', type=argparse.FileType('r'),
127+
help='Read notebooks from a file.')
87128

88129
args = parser.parse_args()
89130

90-
skip = set()
91-
for pattern in args.skip:
92-
skip |= set([nb for nb in args.notebook if fnmatch(nb, pattern)])
131+
notebooks = read_notebooks_from_file(args.file) + args.notebook
132+
skip = match_by_pattern(notebooks, args.skip)
93133

94134
summary = []
95-
for notebook in args.notebook:
135+
for notebook in notebooks:
96136
if notebook in skip:
97137
result = notebook, None
98138
else:
99-
result = notebook, check_notebook(notebook)
139+
result = notebook, check_notebook(notebook, dry_run=args.dry_run)
100140
print_summary([result])
101141
summary.append(result)
102142

0 commit comments

Comments
 (0)