-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathconftest.py
More file actions
244 lines (208 loc) · 8.44 KB
/
conftest.py
File metadata and controls
244 lines (208 loc) · 8.44 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
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
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import shutil
import subprocess
from pathlib import Path
try:
import nbformat
except Exception:
nbformat = None
_executed_notebooks = set()
_passed_notebooks = set()
_backup_notebooks = set() # Track notebooks with backup files
def pytest_collection_modifyitems(config, items):
"""Filter tagged cells from notebooks before nbmake runs"""
# Only run if --nbmake flag is present
if not config.getoption('--nbmake', default=False):
return
kernel_name = config.getoption('--kernel', default="")
for item in items:
if _is_notebook_item(item):
notebook_path = item.path if hasattr(item, 'path') else item.fspath
filter_notebook(notebook_path, kernel_name)
def filter_notebook(notebook_path, kernel_name):
"""Remove cells tagged with 'skip-execution' and handle kernel specifications"""
nb = nbformat.read(notebook_path, as_version=4)
kernel_spec_updated = False
current_spec = nb.metadata.get("kernelspec", {})
spec_kernel_name = current_spec.get("name", "")
# Determine which kernel to use
if kernel_name:
# User provided --kernel flag, use that
target_kernel = kernel_name
else:
# No --kernel flag, use notebook's existing kernel
target_kernel = spec_kernel_name
# Handle kernel specification
if current_spec:
# Check if target kernel exists and differs from current spec
if target_kernel:
is_registered = is_kernel_registered(target_kernel)
if is_registered and target_kernel != spec_kernel_name:
# Update to the registered target kernel
nb.metadata["kernelspec"] = {
"display_name": target_kernel,
"language": "python",
"name": target_kernel
}
print(f"Updated kernel: '{spec_kernel_name}' → '{target_kernel}'")
kernel_spec_updated = True
elif not is_registered:
# Target kernel not registered, remove kernelspec
del nb.metadata["kernelspec"]
print(f"Warning: Kernel '{target_kernel}' not registered. Removed kernelspec - nbmake will use current interpreter.")
kernel_spec_updated = True
# else: kernel is registered and matches current spec, no change needed
# Filter cells
filtered_cells = []
for cell in nb.cells:
tags = cell.get('metadata', {}).get('tags', [])
if any(tag in ['skip-execution', 'skip', 'colab'] for tag in tags):
continue
filtered_cells.append(cell)
cell_skipped = len(filtered_cells) != len(nb.cells)
if cell_skipped:
nb.cells = filtered_cells
# Write modified notebook to disk if anything changed
# (nbmake reads from disk, so changes must be persisted)
if cell_skipped or kernel_spec_updated:
# Create backup of original notebook before modifying
notebook_path = Path(notebook_path)
backup_path = notebook_path.with_suffix(notebook_path.suffix + '.backup')
if backup_path.exists():
# Existing backup found - assume it's the true original
print(f"Found existing backup: {backup_path.name}")
else:
shutil.copy(notebook_path, backup_path)
print(f"Created backup: {backup_path.name}")
_backup_notebooks.add(notebook_path)
# Write modified notebook
nbformat.write(nb, notebook_path)
if cell_skipped:
print(f"Filtered {len(nb.cells)} → {len(filtered_cells)} cells in {notebook_path.name}")
if kernel_spec_updated:
print(f"Updated kernel spec in {notebook_path.name}")
def is_kernel_registered(kernel_name):
"""Check if a kernel is registered in Jupyter"""
if not kernel_name:
return False
try:
from jupyter_client.kernelspec import KernelSpecManager
ksm = KernelSpecManager()
return kernel_name in ksm.get_all_specs()
except Exception:
# If jupyter_client isn't available or fails, assume kernel doesn't exist
return False
def restore_notebooks():
"""Restore original notebooks from .backup files"""
if not _backup_notebooks:
return
restored = []
for notebook_path in _backup_notebooks:
backup_path = notebook_path.with_suffix(Path(notebook_path).suffix + '.backup')
if backup_path.exists():
shutil.move(backup_path, notebook_path)
restored.append(str(notebook_path))
if restored:
print(f"\n[notebook-restore] Restored {len(restored)} original notebook(s):")
for p in restored:
print(" -", p)
def pytest_addoption(parser):
parser.addoption(
"--nbmake-clean",
action="store",
default="on-success",
choices=["always", "on-success", "never"],
help="When to clear outputs from executed notebooks: always, on-success (default), never",
)
parser.addoption(
"--kernel",
action="store",
default="",
help="specify the kernel name",
)
def _is_notebook_item(item):
"""Check if item is a notebook, compatible with old and new pytest versions"""
try:
# Try newer pytest attribute first (pytest 7+)
if hasattr(item, 'path'):
return str(item.path).endswith('.ipynb')
# Fall back to older pytest attribute
return Path(str(item.fspath)).suffix == ".ipynb"
except Exception:
return False
def pytest_runtest_setup(item):
# record that this notebook is being executed
if _is_notebook_item(item):
notebook_path = Path(str(item.path)) if hasattr(item, 'path') else Path(str(item.fspath))
_executed_notebooks.add(notebook_path)
def pytest_runtest_makereport(item, call):
# called for setup/call/teardown — only consider the 'call' phase
if call.when != "call":
return
if not _is_notebook_item(item):
return
# success if no exception info
if call.excinfo is None:
notebook_path = Path(str(item.path)) if hasattr(item, 'path') else Path(str(item.fspath))
_passed_notebooks.add(notebook_path)
def _clear_outputs_with_nbformat(nb_path: Path):
if nbformat is None:
return False
nb = nbformat.read(str(nb_path), as_version=nbformat.NO_CONVERT)
changed = False
for cell in nb.cells:
if cell.get("outputs"):
cell["outputs"] = []
changed = True
if "execution_count" in cell and cell["execution_count"] is not None:
cell["execution_count"] = None
changed = True
if changed:
nbformat.write(nb, str(nb_path))
return changed
def _clear_outputs_with_nbconvert(nb_path: Path):
# fallback if nbformat isn't available or you prefer nbconvert
subprocess.run([
"jupyter", "nbconvert",
"--ClearOutputPreprocessor.enabled=True",
"--inplace", str(nb_path)
], check=False)
def pytest_sessionfinish(session, exitstatus):
# First, restore original notebooks (before cleaning outputs)
restore_notebooks()
mode = session.config.getoption("--nbmake-clean")
if mode == "never":
return
if mode == "always":
to_clean = _executed_notebooks
else: # on-success
to_clean = _passed_notebooks
if not to_clean:
return
cleaned = []
for nb in sorted(to_clean):
# prefer programmatic clearing; fallback to nbconvert
ok = False
if nbformat is not None:
try:
ok = _clear_outputs_with_nbformat(nb)
except Exception:
ok = False
if not ok:
_clear_outputs_with_nbconvert(nb)
cleaned.append(str(nb))
print(f"\n[nbmake-clean] cleaned outputs from {len(cleaned)} executed notebook(s):")
for p in cleaned:
print(" -", p)