simplify symlionk creation

This commit is contained in:
2026-01-14 23:50:03 +02:00
parent 4dd578ce10
commit 9ccff5657b
3 changed files with 22 additions and 23 deletions

View File

@ -91,10 +91,11 @@ class ArchiveTree(LazyLogging):
has_file = False has_file = False
for file in archive.glob(f"{single.filename}*"): for file in archive.glob(f"{single.filename}*"):
symlink = root / file.name symlink = root / file.name
if symlink.exists(): try:
symlink.symlink_to(file.relative_to(symlink.parent, walk_up=True))
has_file = True
except FileExistsError:
continue # symlink is already created, skip processing continue # symlink is already created, skip processing
has_file = True
symlink.symlink_to(file.relative_to(symlink.parent, walk_up=True))
if has_file: if has_file:
repo.add(root / single.filename) repo.add(root / single.filename)

View File

@ -1,5 +1,6 @@
from pathlib import Path from pathlib import Path
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
from unittest.mock import call as MockCall
from ahriman.core.archive.archive_tree import ArchiveTree from ahriman.core.archive.archive_tree import ArchiveTree
from ahriman.core.utils import utcnow from ahriman.core.utils import utcnow
@ -23,29 +24,26 @@ def test_symlinks_create(archive_tree: ArchiveTree, package_ahriman: Package, pa
""" """
_original_exists = Path.exists _original_exists = Path.exists
def exists_mock(path: Path) -> bool: symlinks_mock = mocker.patch("pathlib.Path.symlink_to", side_effect=(None, FileExistsError, FileExistsError))
if path.name in (package.filename for package in package_python_schedule.packages.values()):
return True
return _original_exists(path)
symlinks_mock = mocker.patch("pathlib.Path.symlink_to")
add_mock = mocker.patch("ahriman.core.alpm.repo.Repo.add") add_mock = mocker.patch("ahriman.core.alpm.repo.Repo.add")
mocker.patch("pathlib.Path.glob", autospec=True, side_effect=lambda path, name: [path / name[:-1]]) mocker.patch("pathlib.Path.glob", autospec=True, side_effect=lambda path, name: [path / name[:-1]])
mocker.patch("pathlib.Path.exists", autospec=True, side_effect=exists_mock)
archive_tree.symlinks_create([package_ahriman, package_python_schedule]) archive_tree.symlinks_create([package_ahriman, package_python_schedule])
symlinks_mock.assert_called_once_with( symlinks_mock.assert_has_calls([
Path("..") / MockCall(Path("..") /
".." / ".." /
".." / ".." /
".." / ".." /
".." / ".." /
".." / ".." /
archive_tree.paths.archive_for(package_ahriman.base) archive_tree.paths.archive_for(package.base)
.relative_to(archive_tree.paths.root) .relative_to(archive_tree.paths.root)
.relative_to("archive") / .relative_to("archive") /
package_ahriman.packages[package_ahriman.base].filename single.filename
) )
for package in (package_ahriman, package_python_schedule)
for single in package.packages.values()
])
add_mock.assert_called_once_with( add_mock.assert_called_once_with(
archive_tree.repository_for() / package_ahriman.packages[package_ahriman.base].filename archive_tree.repository_for() / package_ahriman.packages[package_ahriman.base].filename
) )

View File

@ -25,7 +25,7 @@ def test_on_start(archive_trigger: ArchiveTrigger, mocker: MockerFixture) -> Non
def test_on_stop(archive_trigger: ArchiveTrigger, mocker: MockerFixture) -> None: def test_on_stop(archive_trigger: ArchiveTrigger, mocker: MockerFixture) -> None:
""" """
must create repository tree on load must fix broken symlinks on stop
""" """
symlinks_mock = mocker.patch("ahriman.core.archive.archive_tree.ArchiveTree.symlinks_fix") symlinks_mock = mocker.patch("ahriman.core.archive.archive_tree.ArchiveTree.symlinks_fix")
archive_trigger.on_stop() archive_trigger.on_stop()