Passed
Pull Request — dev (#850)
by Uwe
01:39
created

check_examples.notebook_run()   A

Complexity

Conditions 2

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 33
rs 9.304
c 0
b 0
f 0
cc 2
nop 1
1
import os
2
import subprocess
3
import tempfile
4
import warnings
5
from datetime import datetime
6
7
import matplotlib
8
import nbformat
9
from matplotlib import pyplot as plt
10
from termcolor import colored
11
12
warnings.filterwarnings("ignore", "", UserWarning)
13
matplotlib.use("Agg")
14
15
stop_at_error = False  # If True script will stop if error is raised
16
exclude_notebooks = False
17
exclude_python_scripts = False
18
19
20
def notebook_run(path):
21
    """
22
    Execute a notebook via nbconvert and collect output.
23
    Returns (parsed nb object, execution errors)
24
    """
25
    dirname, __ = os.path.split(path)
26
    os.chdir(dirname)
27
    with tempfile.NamedTemporaryFile(suffix=".ipynb") as fout:
28
        args = [
29
            "jupyter",
30
            "nbconvert",
31
            "--to",
32
            "notebook",
33
            "--execute",
34
            "--ExecutePreprocessor.timeout=60",
35
            "--output",
36
            fout.name,
37
            path,
38
        ]
39
        subprocess.check_call(args)
40
41
        fout.seek(0)
42
        nb = nbformat.read(fout, nbformat.current_nbformat)
43
44
    errors = [
45
        output
46
        for cell in nb.cells
47
        if "outputs" in cell
48
        for output in cell["outputs"]
49
        if output.output_type == "error"
50
    ]
51
52
    return nb, errors
53
54
55
fullpath = os.path.join(os.getcwd())
56
57
checker = {}
58
number = 0
59
60
start_check = datetime.now()
61
62
for root, dirs, files in sorted(os.walk(fullpath)):
63
    for name in sorted(files):
64
        if (
65
            name[-3:] == ".py"
66
            and not exclude_python_scripts
67
            and not name == "check_examples.py"
68
        ):
69
            fn = os.path.join(root, name)
70
            os.chdir(root)
71
            number += 1
72 View Code Duplication
            if stop_at_error is True:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
73
                print(fn)
74
                exec(open(fn).read())
75
                checker[name] = "okay"
76
            else:
77
                try:
78
                    exec(open(fn).read())
79
                    checker[name] = "okay"
80
                except Exception as e:
81
                    print(e)
82
                    checker[name] = "failed"
83
        elif name[-6:] == ".ipynb" and not exclude_notebooks:
84
            fn = os.path.join(root, name)
85
            os.chdir(root)
86
            number += 1
87 View Code Duplication
            if stop_at_error is True:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
88
                print(fn)
89
                notebook_run(fn)
90
                checker[name] = "okay"
91
            else:
92
                try:
93
                    notebook_run(fn)
94
                    checker[name] = "okay"
95
                except Exception as e:
96
                    print(e)
97
                    checker[name] = "failed"
98
        plt.close()
99
100
101
print("******* TEST RESULTS ***********************************")
102
103
print(
104
    "\n{0} examples tested in {1}.\n".format(
105
        number, datetime.now() - start_check
106
    )
107
)
108
109
for k, v in checker.items():
110
    if v == "failed":
111
        print(k, colored(v, "red"))
112
    else:
113
        print(k, colored(v, "green"))
114