|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from typing import Tuple, Optional, List |
| 4 | + |
| 5 | +import nbformat |
| 6 | +from nbconvert.preprocessors import ExecutePreprocessor |
| 7 | + |
| 8 | + |
| 9 | +SINGLE_NOTEBOOK_TIMEOUT = 1200 |
| 10 | + |
| 11 | + |
| 12 | +def should_skip(notebook_path: str, skip_list: List[str]) -> bool: |
| 13 | + return any(skip in notebook_path for skip in skip_list) |
| 14 | + |
| 15 | + |
| 16 | +def run_notebook(notebook_path: str, root: str) -> Tuple[bool, Optional[str]]: |
| 17 | + """Execute a single notebook.""" |
| 18 | + try: |
| 19 | + with open(notebook_path, encoding="utf-8") as f: |
| 20 | + nb = nbformat.read(f, as_version=4) |
| 21 | + |
| 22 | + ep = ExecutePreprocessor( |
| 23 | + timeout=SINGLE_NOTEBOOK_TIMEOUT, |
| 24 | + kernel_name="python3") |
| 25 | + ep.preprocess(nb, {"metadata": {"path": root}}) |
| 26 | + return True, None |
| 27 | + except Exception as e: |
| 28 | + return False, str(e) |
| 29 | + |
| 30 | + |
| 31 | +def run_all_notebooks(path: str = ".", skip_list: List[str] = None) -> None: |
| 32 | + abs_path = os.path.abspath(path) |
| 33 | + print(f"🔍 Scanning for notebooks in: {abs_path}\n") |
| 34 | + |
| 35 | + skip_list = skip_list or [] |
| 36 | + |
| 37 | + notebook_found: int = 0 |
| 38 | + success_notebooks: List[str] = [] |
| 39 | + failed_notebooks: List[Tuple[str, str]] = [] |
| 40 | + |
| 41 | + for root, _, files in os.walk(abs_path): |
| 42 | + for file in files: |
| 43 | + if file.endswith(".ipynb") and not file.startswith("."): |
| 44 | + notebook_path = os.path.join(root, file) |
| 45 | + |
| 46 | + if should_skip(notebook_path, skip_list): |
| 47 | + print(f"⏭️ Skipped: {notebook_path}") |
| 48 | + continue |
| 49 | + |
| 50 | + notebook_found += 1 |
| 51 | + print(f"▶️ Running: {notebook_path}") |
| 52 | + success, error = run_notebook(notebook_path, root) |
| 53 | + |
| 54 | + if success: |
| 55 | + print(f"✅ Success: {notebook_path}\n") |
| 56 | + success_notebooks.append(notebook_path) |
| 57 | + else: |
| 58 | + print(f"❌ Failed: {notebook_path}\nError: {error}\n") |
| 59 | + failed_notebooks.append((notebook_path, error)) |
| 60 | + |
| 61 | + # 📋 Summary |
| 62 | + print("🧾 Notebook Execution Summary") |
| 63 | + print(f"✅ {len(success_notebooks)} succeeded") |
| 64 | + print(f"❌ {len(failed_notebooks)} failed\n") |
| 65 | + |
| 66 | + if failed_notebooks: |
| 67 | + print("🚨 Failed notebooks:") |
| 68 | + for nb, error in failed_notebooks: |
| 69 | + last_line = error.strip().splitlines()[-1] if error else "Unknown error" |
| 70 | + print(f" - {nb}\n ↳ {last_line}") |
| 71 | + sys.exit(1) |
| 72 | + |
| 73 | + if notebook_found == 0: |
| 74 | + print("❌ No notebooks were found. Check the folder path or repo contents.") |
| 75 | + sys.exit(1) |
| 76 | + |
| 77 | + print("🏁 All notebooks completed successfully.") |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + args: List[str] = sys.argv[1:] |
| 82 | + |
| 83 | + # NOTE: Define skip list (can use full paths or substrings) |
| 84 | + skip_list = [ |
| 85 | + "build_person_directory.ipynb", # Skip due to "new_face_image_path" needed to be added manually |
| 86 | + ] |
| 87 | + |
| 88 | + if not args: |
| 89 | + run_all_notebooks("notebooks", skip_list=skip_list) |
| 90 | + else: |
| 91 | + failed: List[Tuple[str, str]] = [] |
| 92 | + for notebook_path in args: |
| 93 | + if should_skip(notebook_path, skip_list): |
| 94 | + print(f"⏭️ Skipped: {notebook_path}") |
| 95 | + continue |
| 96 | + |
| 97 | + if notebook_path.endswith(".ipynb") and os.path.isfile(notebook_path): |
| 98 | + print(f"▶️ Running: {notebook_path}") |
| 99 | + success, error = run_notebook(notebook_path, os.path.dirname(notebook_path)) |
| 100 | + if success: |
| 101 | + print(f"✅ Success: {notebook_path}\n") |
| 102 | + else: |
| 103 | + print(f"❌ Failed: {notebook_path}\nError: {error}\n") |
| 104 | + failed.append((notebook_path, error)) |
| 105 | + else: |
| 106 | + print(f"⚠️ Not a valid notebook file: {notebook_path}") |
| 107 | + failed.append((notebook_path, "Invalid path or not a .ipynb file")) |
| 108 | + |
| 109 | + # Summary |
| 110 | + print("🧾 Execution Summary") |
| 111 | + print(f"✅ {len(args) - len(failed)} succeeded") |
| 112 | + print(f"❌ {len(failed)} failed") |
| 113 | + |
| 114 | + if failed: |
| 115 | + print("🚨 Failed notebooks:") |
| 116 | + for nb, error in failed: |
| 117 | + last_line = error.strip().splitlines()[-1] if error else "Unknown error" |
| 118 | + print(f" - {nb}\n ↳ {last_line}") |
| 119 | + sys.exit(1) |
| 120 | + else: |
| 121 | + print("🏁 All selected notebooks completed successfully.") |
0 commit comments