Invalidate stale binary paths after lib moves

This commit is contained in:
Nick Sweeting 2026-06-11 01:16:02 -07:00
parent 426e23f33c
commit 24caacdd1d
No known key found for this signature in database
2 changed files with 22 additions and 10 deletions

View File

@ -588,7 +588,19 @@ class Binary(ModelWithHealthStats, ModelWithStateMachine):
@property
def is_valid(self) -> bool:
"""A binary is valid if it has a resolved path and is marked installed."""
return bool(self.abspath) and self.status == self.StatusChoices.INSTALLED
if not self.abspath or self.status != self.StatusChoices.INSTALLED:
return False
try:
abspath = Path(self.abspath).expanduser().resolve(strict=False)
if not abspath.exists():
return False
if self.binprovider not in {"", "env", "apt", "brew"}:
from archivebox.config.common import get_config
abspath.relative_to(get_config(include_machine=False).LIB_DIR)
except (OSError, ValueError):
return False
return True
@cached_property
def binary_info(self) -> dict:

View File

@ -337,9 +337,9 @@ class TestBinaryModel:
"""Binary.is_valid should be True for installed binaries with a resolved path."""
binary = Binary.objects.create(
machine=self.machine,
name="wget",
abspath="/usr/bin/wget",
version="1.21",
name="python",
abspath=sys.executable,
version=f"{sys.version_info.major}.{sys.version_info.minor}",
status=Binary.StatusChoices.INSTALLED,
)
@ -348,21 +348,21 @@ class TestBinaryModel:
def test_binary_manager_get_valid_binary(self):
"""BinaryManager.get_valid_binary() should find valid binaries."""
# Create invalid binary (no abspath)
Binary.objects.create(machine=self.machine, name="wget")
Binary.objects.create(machine=self.machine, name="python")
# Create valid binary
Binary.objects.create(
machine=self.machine,
name="wget",
abspath="/usr/bin/wget",
version="1.21",
name="python",
abspath=sys.executable,
version=f"{sys.version_info.major}.{sys.version_info.minor}",
status=Binary.StatusChoices.INSTALLED,
)
result = cast(BinaryManager, Binary.objects).get_valid_binary("wget")
result = cast(BinaryManager, Binary.objects).get_valid_binary("python")
assert result is not None
assert result.abspath == "/usr/bin/wget"
assert result.abspath == sys.executable
def test_binary_update_and_requeue(self):
"""Binary.update_and_requeue() should update fields and save."""