Skip to content

Commit e759045

Browse files
authored
Merge pull request #94 from PyAutoLabs/feature/default-branch-release-to-main
feat(workspace): add version-mismatch check helper
2 parents b7247f8 + 9252484 commit e759045

3 files changed

Lines changed: 91 additions & 0 deletions

File tree

autoconf/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from .setup_colab import for_autolens
2020
from .setup_notebook import setup_notebook
2121
from .test_mode import test_mode_level, is_test_mode, skip_fit_output, skip_visualization, skip_checks
22+
from .workspace import check_version, WorkspaceVersionMismatchError
2223

2324

2425
__version__ = "2026.4.13.6"

autoconf/workspace.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
)

test_autoconf/test_workspace.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
from autoconf.workspace import check_version, WorkspaceVersionMismatchError
4+
5+
6+
def test_match(tmp_path):
7+
(tmp_path / "version.txt").write_text("2026.4.13.6\n")
8+
check_version("2026.4.13.6", workspace_root=tmp_path)
9+
10+
11+
def test_mismatch_raises(tmp_path):
12+
(tmp_path / "version.txt").write_text("2026.4.13.6\n")
13+
with pytest.raises(WorkspaceVersionMismatchError) as info:
14+
check_version("2025.1.1.1", workspace_root=tmp_path)
15+
assert "2026.4.13.6" in str(info.value)
16+
assert "2025.1.1.1" in str(info.value)
17+
18+
19+
def test_missing_file_warns(tmp_path):
20+
with pytest.warns(UserWarning, match="No version.txt"):
21+
check_version("2026.4.13.6", workspace_root=tmp_path)
22+
23+
24+
def test_env_override_skips_mismatch(tmp_path, monkeypatch):
25+
monkeypatch.setenv("PYAUTO_SKIP_WORKSPACE_VERSION_CHECK", "1")
26+
(tmp_path / "version.txt").write_text("2025.1.1.1\n")
27+
check_version("2026.4.13.6", workspace_root=tmp_path)
28+
29+
30+
def test_env_override_skips_missing_file(tmp_path, monkeypatch):
31+
monkeypatch.setenv("PYAUTO_SKIP_WORKSPACE_VERSION_CHECK", "1")
32+
check_version("2026.4.13.6", workspace_root=tmp_path)
33+
34+
35+
def test_default_root_is_cwd(tmp_path, monkeypatch):
36+
(tmp_path / "version.txt").write_text("2026.4.13.6\n")
37+
monkeypatch.chdir(tmp_path)
38+
check_version("2026.4.13.6")

0 commit comments

Comments
 (0)