|
| 1 | +import os |
| 2 | +import warnings |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from autoconf import exc |
| 6 | + |
| 7 | + |
| 8 | +class WorkspaceVersionMismatchError(exc.ConfigException): |
| 9 | + pass |
| 10 | + |
| 11 | + |
| 12 | +def check_version(library_version, workspace_root=None): |
| 13 | + """ |
| 14 | + Verify that the workspace at ``workspace_root`` matches ``library_version``. |
| 15 | +
|
| 16 | + Reads ``version.txt`` from ``workspace_root`` (defaults to the current |
| 17 | + working directory, which is where users run workspace scripts from). |
| 18 | + Raises ``WorkspaceVersionMismatchError`` if the file's version differs |
| 19 | + from ``library_version``. If ``version.txt`` does not exist (e.g. an |
| 20 | + older workspace clone or one cloned from ``main`` outside a release tag) |
| 21 | + a warning is emitted and the check is skipped. |
| 22 | +
|
| 23 | + Set ``PYAUTO_SKIP_WORKSPACE_VERSION_CHECK=1`` to disable the check |
| 24 | + entirely — intended for developers running source checkouts where |
| 25 | + workspace and library versions intentionally diverge. |
| 26 | + """ |
| 27 | + if os.environ.get("PYAUTO_SKIP_WORKSPACE_VERSION_CHECK") == "1": |
| 28 | + return |
| 29 | + |
| 30 | + root = Path(workspace_root) if workspace_root else Path.cwd() |
| 31 | + version_file = root / "version.txt" |
| 32 | + |
| 33 | + if not version_file.exists(): |
| 34 | + warnings.warn( |
| 35 | + f"No version.txt found at {version_file}. Cannot verify that the " |
| 36 | + f"workspace matches the installed library version ({library_version}). " |
| 37 | + f"If you cloned the workspace from main rather than a release tag, " |
| 38 | + f"set PYAUTO_SKIP_WORKSPACE_VERSION_CHECK=1 to silence this warning." |
| 39 | + ) |
| 40 | + return |
| 41 | + |
| 42 | + workspace_version = version_file.read_text().strip() |
| 43 | + |
| 44 | + if workspace_version != library_version: |
| 45 | + raise WorkspaceVersionMismatchError( |
| 46 | + f"Workspace version ({workspace_version}) at {root} does not match " |
| 47 | + f"the installed library version ({library_version}). Re-clone the " |
| 48 | + f"workspace at the matching tag:\n\n" |
| 49 | + f" git clone --branch {library_version} <workspace-repo-url>\n\n" |
| 50 | + f"Or set PYAUTO_SKIP_WORKSPACE_VERSION_CHECK=1 to override (intended " |
| 51 | + f"for source-checkout development)." |
| 52 | + ) |
0 commit comments