|
| 1 | +# Copyright 2020 The TensorFlow Quantum Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Validate all Jupyter notebooks in the repository using nbformat.""" |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +import nbformat |
| 20 | +from nbformat.validator import NotebookValidationError |
| 21 | + |
| 22 | + |
| 23 | +def main(): |
| 24 | + """Check all notebooks for valid nbformat structure.""" |
| 25 | + failed = False |
| 26 | + notebooks = list(Path('.').rglob('*.ipynb')) |
| 27 | + print(f"Found notebooks: {notebooks}") |
| 28 | + for notebook_path in notebooks: |
| 29 | + try: |
| 30 | + with open(notebook_path, 'r', encoding='utf-8') as f: |
| 31 | + nb = nbformat.read(f, as_version=4) |
| 32 | + nbformat.validate(nb) |
| 33 | + print(f"✓ {notebook_path}") |
| 34 | + except (NotebookValidationError, OSError) as e: |
| 35 | + print(f"✗ {notebook_path} failed validation: {e}") |
| 36 | + failed = True |
| 37 | + |
| 38 | + if failed: |
| 39 | + sys.exit(1) |
| 40 | + else: |
| 41 | + print("All notebooks passed nbformat validation.") |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + main() |
0 commit comments