-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_notebooks.py
More file actions
executable file
·71 lines (55 loc) · 1.89 KB
/
test_notebooks.py
File metadata and controls
executable file
·71 lines (55 loc) · 1.89 KB
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
#!/usr/bin/env python3
# Clean the notebook cache to save memory online
# Tutorial notebooks are pushed to git lfs!
import os
import shutil as sh
import subprocess as sub
from tqdm.auto import tqdm
notebooks = []
# delete all '.ipynb_checkpoints' folders
del_folders = []
for r,d,f in os.walk('.'):
for folder in d:
if folder.endswith('.ipynb_checkpoints'):
del_folders.append(r + '/' + folder)
for folder in del_folders:
if os.path.exists(folder):
# delete folder even if it is not empty
sh.rmtree(folder,ignore_errors=True)
for r_,d_,f_ in os.walk('.'):
if not 'test_notebooks' in f_:
continue
for r,d,f in os.walk(r_):
if 'no_testing' in f:
continue
for file in f:
if file.endswith('.ipynb'):
notebooks.append(r + '/' + file)
print(f'found {notebooks[-1]}')
# delete jupyter_nbconvert.log if it exists
if os.path.exists('jupyter_nbconvert.log'):
os.remove('jupyter_nbconvert.log')
# delete pytest.log if it exists
if os.path.exists('pytest.log'):
os.remove('pytest.log')
# call pytest --nbmake on each notebook
# if pytest fails, exit and print error
# if pytest succeeds, continue
# print('Running pytest on each notebook...')
# print(f'found {notebooks}')
# for notebook in notebooks:
# if sub.run(['pytest', '--nbmake', notebook]).returncode != 0:
# exit()
# call pytest --nbmake on each notebook
# if pytest fails, exit and print error
# if pytest succeeds, continue
print('Running pytest on each notebook...')
print(f'found {notebooks}')
if sub.run(['pytest', '--nbmake', *notebooks]).returncode != 0:
exit()
# call jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace on each notebook
# use subprocess instead of os.system
# print stderr output to jupyter_nbconvert.log
if False:
for notebook in tqdm(notebooks):
sub.run(['jupyter', 'nbconvert', '--ClearOutputPreprocessor.enabled=True', '--inplace', notebook], stderr=open('jupyter_nbconvert.log', 'a'))