|
2 | 2 | # dependencies = [ |
3 | 3 | # "mypy~=1.15.0", |
4 | 4 | # "stubgenlib~=0.1.0", |
5 | | -# "OpenImageIO @ file://@OIIO_PYTHON_WHEEL@", |
6 | 5 | # ] |
7 | 6 | # /// |
8 | 7 |
|
@@ -119,15 +118,57 @@ def set_defined_names(self, defined_names: set[str]) -> None: |
119 | 118 | mypy.stubgen.InspectionStubGenerator = InspectionStubGenerator # type: ignore[attr-defined,misc] |
120 | 119 | mypy.stubgenc.InspectionStubGenerator = InspectionStubGenerator # type: ignore[misc] |
121 | 120 |
|
| 121 | + |
| 122 | +def get_colored_diff(old_text: str, new_text: str): |
| 123 | + """ |
| 124 | + Generates a colored diff between two strings. |
| 125 | +
|
| 126 | + Returns: |
| 127 | + A string containing the colored diff output. |
| 128 | + """ |
| 129 | + import difflib |
| 130 | + |
| 131 | + red = '\033[31m' # ANSI escape code for red |
| 132 | + green = '\033[32m' # ANSI escape code for green |
| 133 | + reset = '\033[0m' # ANSI escape code to reset color |
| 134 | + |
| 135 | + diff = difflib.unified_diff(old_text.splitlines(keepends=True), new_text.splitlines(keepends=True), lineterm="") |
| 136 | + lines = [] |
| 137 | + for line in diff: |
| 138 | + if line.startswith('-'): |
| 139 | + lines.append(f"{red}{line}{reset}") |
| 140 | + elif line.startswith('+'): |
| 141 | + lines.append(f"{green}{line}{reset}") |
| 142 | + else: |
| 143 | + lines.append(line) |
| 144 | + return "".join(lines) |
| 145 | + |
| 146 | + |
122 | 147 | if __name__ == "__main__": |
| 148 | + import pathlib |
123 | 149 | import os |
124 | 150 | import sys |
125 | | - out_path = sys.argv[1] |
| 151 | + out_path = pathlib.Path(sys.argv[1]) |
| 152 | + validate_path = pathlib.Path(sys.argv[2]) |
126 | 153 | print(f"Stub output directory: {out_path}") |
127 | 154 | # perform import so we can see the traceback if it fails. |
128 | 155 | import OpenImageIO |
129 | | - sys.argv[1:] = ["-p", "OpenImageIO", "-o", out_path] |
| 156 | + sys.argv[1:] = ["-p", "OpenImageIO", "-o", str(out_path)] |
130 | 157 | mypy.stubgen.main() |
131 | | - dest = os.path.join(out_path, "OpenImageIO", "__init__.pyi") |
| 158 | + source = out_path.joinpath("OpenImageIO", "OpenImageIO.pyi") |
| 159 | + if not source.exists(): |
| 160 | + print("Stub generation failed") |
| 161 | + sys.exit(1) |
| 162 | + |
| 163 | + dest = out_path.joinpath("OpenImageIO", "__init__.pyi") |
132 | 164 | print(f"Renaming to {dest}") |
133 | | - os.rename(os.path.join(out_path, "OpenImageIO", "OpenImageIO.pyi"), dest) |
| 165 | + os.rename(source, dest) |
| 166 | + |
| 167 | + if "GITHUB_ACTIONS" in os.environ: |
| 168 | + # in CI, we don't overwrite the output path, we merely validate that what has |
| 169 | + # been committed to the repo is what we expect. |
| 170 | + old_text = validate_path.text() |
| 171 | + new_text = dest.text() |
| 172 | + if old_text != new_text: |
| 173 | + print(get_colored_diff(old_text, new_text)) |
| 174 | + sys.exit(2) |
0 commit comments