Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix: detect source checkout for app updates (#1186)

Dongyu8ffa08d89ede
diff --git a/deeptutor/services/app_update.py b/deeptutor/services/app_update.py
index 844c454b6..c565e9ea5 100644
--- a/deeptutor/services/app_update.py
+++ b/deeptutor/services/app_update.py
@@ -166,6 +166,15 @@ def _distribution_direct_url() -> dict[str, Any] | None:
return payload if isinstance(payload, dict) else {}
+def _running_from_source_checkout() -> bool:
+ """Return whether the imported package lives in a Git source checkout."""
+ try:
+ checkout_root = Path(__file__).resolve().parents[2]
+ except IndexError:
+ return False
+ return (checkout_root / ".git").exists() and (checkout_root / "pyproject.toml").is_file()
+
+
def detect_installation() -> Installation:
"""Classify only layouts whose update ownership is unambiguous."""
@@ -178,6 +187,15 @@ def detect_installation() -> Installation:
reason="Container images are updated and recreated by the Docker host.",
)
+ if _running_from_source_checkout():
+ return Installation(
+ mode="source",
+ current_version=__version__,
+ automatic_update=False,
+ command="git pull && pip install -e .",
+ reason="Source checkouts stay under the developer's Git workflow.",
+ )
+
direct_url = _distribution_direct_url()
if direct_url is None:
return Installation(
diff --git a/tests/services/test_app_update.py b/tests/services/test_app_update.py
index 7b7711aef..3a2a009eb 100644
--- a/tests/services/test_app_update.py
+++ b/tests/services/test_app_update.py
@@ -151,6 +151,7 @@ def test_detect_installation_keeps_source_and_docker_host_managed(
assert app_update.detect_installation().mode == "docker"
monkeypatch.setattr(app_update, "_running_in_container", lambda: False)
+ monkeypatch.setattr(app_update, "_running_from_source_checkout", lambda: False)
monkeypatch.setattr(
app_update,
"_distribution_direct_url",
@@ -166,6 +167,33 @@ def test_detect_installation_keeps_source_and_docker_host_managed(
assert installation.automatic_update is True
+def test_detect_installation_prefers_source_checkout_when_metadata_is_shadowed(
+ monkeypatch: pytest.MonkeyPatch,
+ tmp_path: Path,
+) -> None:
+ checkout_root = tmp_path / "DeepTutor"
+ module_path = checkout_root / "deeptutor" / "services" / "app_update.py"
+ module_path.parent.mkdir(parents=True)
+ module_path.touch()
+ (checkout_root / ".git").mkdir()
+ (checkout_root / "pyproject.toml").write_text(
+ '[project]\nname = "deeptutor"\n',
+ encoding="utf-8",
+ )
+
+ monkeypatch.setattr(app_update, "__file__", str(module_path))
+ monkeypatch.setattr(app_update, "_running_in_container", lambda: False)
+ monkeypatch.setattr(app_update, "_distribution_direct_url", lambda: {})
+ monkeypatch.setattr(app_update.sys, "prefix", str(tmp_path / "venv"))
+ monkeypatch.setattr(app_update.sys, "base_prefix", str(tmp_path / "base"))
+
+ installation = app_update.detect_installation()
+
+ assert installation.mode == "source"
+ assert installation.automatic_update is False
+ assert installation.command == "git pull && pip install -e ."
+
+
def test_update_job_store_persists_trusted_lifecycle(tmp_path: Path) -> None:
store = UpdateJobStore(tmp_path / "update")
job = store.create(current_version="1.6.1", target_version="1.7.0")