Compare commits

..

1 Commits

Author SHA1 Message Date
4d4d27489d feat: add pkgbuild subscommands 2026-03-11 01:49:23 +02:00
22 changed files with 129 additions and 273 deletions

View File

@@ -172,14 +172,6 @@ ahriman.models.packagers module
:no-undoc-members: :no-undoc-members:
:show-inheritance: :show-inheritance:
ahriman.models.pacman\_handle module
------------------------------------
.. automodule:: ahriman.models.pacman_handle
:members:
:no-undoc-members:
:show-inheritance:
ahriman.models.pacman\_synchronization module ahriman.models.pacman\_synchronization module
--------------------------------------------- ---------------------------------------------

View File

@@ -24,14 +24,13 @@ import tarfile
from collections.abc import Iterable, Iterator from collections.abc import Iterable, Iterator
from functools import cached_property from functools import cached_property
from pathlib import Path from pathlib import Path
from pyalpm import DB, Package, SIG_DATABASE_OPTIONAL, SIG_PACKAGE_OPTIONAL # type: ignore[import-not-found] from pyalpm import DB, Handle, Package, SIG_DATABASE_OPTIONAL, SIG_PACKAGE_OPTIONAL # type: ignore[import-not-found]
from string import Template from string import Template
from ahriman.core.alpm.pacman_database import PacmanDatabase from ahriman.core.alpm.pacman_database import PacmanDatabase
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.log import LazyLogging from ahriman.core.log import LazyLogging
from ahriman.core.utils import trim_package from ahriman.core.utils import trim_package
from ahriman.models.pacman_handle import PacmanHandle
from ahriman.models.pacman_synchronization import PacmanSynchronization from ahriman.models.pacman_synchronization import PacmanSynchronization
from ahriman.models.repository_id import RepositoryId from ahriman.models.repository_id import RepositoryId
@@ -62,16 +61,16 @@ class Pacman(LazyLogging):
self.refresh_database = refresh_database self.refresh_database = refresh_database
@cached_property @cached_property
def handle(self) -> PacmanHandle: def handle(self) -> Handle:
""" """
pyalpm handle pyalpm handle
Returns: Returns:
PacmanHandle: generated pyalpm handle instance Handle: generated pyalpm handle instance
""" """
return self.__create_handle(refresh_database=self.refresh_database) return self.__create_handle(refresh_database=self.refresh_database)
def __create_handle(self, *, refresh_database: PacmanSynchronization) -> PacmanHandle: def __create_handle(self, *, refresh_database: PacmanSynchronization) -> Handle:
""" """
create lazy handle function create lazy handle function
@@ -79,14 +78,14 @@ class Pacman(LazyLogging):
refresh_database(PacmanSynchronization): synchronize local cache to remote refresh_database(PacmanSynchronization): synchronize local cache to remote
Returns: Returns:
PacmanHandle: fully initialized pacman handle Handle: fully initialized pacman handle
""" """
pacman_root = self.configuration.getpath("alpm", "database") pacman_root = self.configuration.getpath("alpm", "database")
use_ahriman_cache = self.configuration.getboolean("alpm", "use_ahriman_cache") use_ahriman_cache = self.configuration.getboolean("alpm", "use_ahriman_cache")
database_path = self.repository_paths.pacman if use_ahriman_cache else pacman_root database_path = self.repository_paths.pacman if use_ahriman_cache else pacman_root
root = self.configuration.getpath("alpm", "root") root = self.configuration.getpath("alpm", "root")
handle = PacmanHandle(str(root), str(database_path)) handle = Handle(str(root), str(database_path))
for repository in self.configuration.getlist("alpm", "repositories"): for repository in self.configuration.getlist("alpm", "repositories"):
database = self.database_init(handle, repository, self.repository_id.architecture) database = self.database_init(handle, repository, self.repository_id.architecture)
@@ -100,12 +99,12 @@ class Pacman(LazyLogging):
return handle return handle
def database_copy(self, handle: PacmanHandle, database: DB, pacman_root: Path, *, use_ahriman_cache: bool) -> None: def database_copy(self, handle: Handle, database: DB, pacman_root: Path, *, use_ahriman_cache: bool) -> None:
""" """
copy database from the operating system root to the ahriman local home copy database from the operating system root to the ahriman local home
Args: Args:
handle(PacmanHandle): pacman handle which will be used for database copying handle(Handle): pacman handle which will be used for database copying
database(DB): pacman database instance to be copied database(DB): pacman database instance to be copied
pacman_root(Path): operating system pacman root pacman_root(Path): operating system pacman root
use_ahriman_cache(bool): use local ahriman cache instead of system one use_ahriman_cache(bool): use local ahriman cache instead of system one
@@ -134,12 +133,12 @@ class Pacman(LazyLogging):
with self.repository_paths.preserve_owner(): with self.repository_paths.preserve_owner():
shutil.copy(src, dst) shutil.copy(src, dst)
def database_init(self, handle: PacmanHandle, repository: str, architecture: str) -> DB: def database_init(self, handle: Handle, repository: str, architecture: str) -> DB:
""" """
create database instance from pacman handler and set its properties create database instance from pacman handler and set its properties
Args: Args:
handle(PacmanHandle): pacman handle which will be used for database initializing handle(Handle): pacman handle which will be used for database initializing
repository(str): pacman repository name (e.g. core) repository(str): pacman repository name (e.g. core)
architecture(str): repository architecture architecture(str): repository architecture
@@ -165,12 +164,12 @@ class Pacman(LazyLogging):
return database return database
def database_sync(self, handle: PacmanHandle, *, force: bool) -> None: def database_sync(self, handle: Handle, *, force: bool) -> None:
""" """
sync local database sync local database
Args: Args:
handle(PacmanHandle): pacman handle which will be used for database sync handle(Handle): pacman handle which will be used for database sync
force(bool): force database synchronization (same as ``pacman -Syy``) force(bool): force database synchronization (same as ``pacman -Syy``)
""" """
self.logger.info("refresh ahriman's home pacman database (force refresh %s)", force) self.logger.info("refresh ahriman's home pacman database (force refresh %s)", force)

View File

@@ -19,9 +19,11 @@
# #
from sqlite3 import Connection from sqlite3 import Connection
from ahriman.core.alpm.pacman import Pacman
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.utils import package_like from ahriman.core.utils import package_like
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.pacman_synchronization import PacmanSynchronization
__all__ = ["migrate_data", "steps"] __all__ = ["migrate_data", "steps"]
@@ -59,9 +61,12 @@ def migrate_package_depends(connection: Connection, configuration: Configuration
if not configuration.repository_paths.repository.is_dir(): if not configuration.repository_paths.repository.is_dir():
return return
_, repository_id = configuration.check_loaded()
pacman = Pacman(repository_id, configuration, refresh_database=PacmanSynchronization.Disabled)
package_list = [] package_list = []
for full_path in filter(package_like, configuration.repository_paths.repository.iterdir()): for full_path in filter(package_like, configuration.repository_paths.repository.iterdir()):
base = Package.from_archive(full_path) base = Package.from_archive(full_path, pacman)
for package, description in base.packages.items(): for package, description in base.packages.items():
package_list.append({ package_list.append({
"make_depends": description.make_depends, "make_depends": description.make_depends,

View File

@@ -19,9 +19,11 @@
# #
from sqlite3 import Connection from sqlite3 import Connection
from ahriman.core.alpm.pacman import Pacman
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.utils import package_like from ahriman.core.utils import package_like
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.pacman_synchronization import PacmanSynchronization
__all__ = ["migrate_data", "steps"] __all__ = ["migrate_data", "steps"]
@@ -56,9 +58,12 @@ def migrate_package_check_depends(connection: Connection, configuration: Configu
if not configuration.repository_paths.repository.is_dir(): if not configuration.repository_paths.repository.is_dir():
return return
_, repository_id = configuration.check_loaded()
pacman = Pacman(repository_id, configuration, refresh_database=PacmanSynchronization.Disabled)
package_list = [] package_list = []
for full_path in filter(package_like, configuration.repository_paths.repository.iterdir()): for full_path in filter(package_like, configuration.repository_paths.repository.iterdir()):
base = Package.from_archive(full_path) base = Package.from_archive(full_path, pacman)
for package, description in base.packages.items(): for package, description in base.packages.items():
package_list.append({ package_list.append({
"check_depends": description.check_depends, "check_depends": description.check_depends,

View File

@@ -19,9 +19,11 @@
# #
from sqlite3 import Connection from sqlite3 import Connection
from ahriman.core.alpm.pacman import Pacman
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.utils import package_like from ahriman.core.utils import package_like
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.pacman_synchronization import PacmanSynchronization
__all__ = ["migrate_data", "steps"] __all__ = ["migrate_data", "steps"]
@@ -62,9 +64,12 @@ def migrate_package_base_packager(connection: Connection, configuration: Configu
if not configuration.repository_paths.repository.is_dir(): if not configuration.repository_paths.repository.is_dir():
return return
_, repository_id = configuration.check_loaded()
pacman = Pacman(repository_id, configuration, refresh_database=PacmanSynchronization.Disabled)
package_list = [] package_list = []
for full_path in filter(package_like, configuration.repository_paths.repository.iterdir()): for full_path in filter(package_like, configuration.repository_paths.repository.iterdir()):
package = Package.from_archive(full_path) package = Package.from_archive(full_path, pacman)
package_list.append({ package_list.append({
"package_base": package.base, "package_base": package.base,
"packager": package.packager, "packager": package.packager,

View File

@@ -20,11 +20,13 @@
from dataclasses import replace from dataclasses import replace
from sqlite3 import Connection from sqlite3 import Connection
from ahriman.core.alpm.pacman import Pacman
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.repository import Explorer from ahriman.core.repository import Explorer
from ahriman.core.sign.gpg import GPG from ahriman.core.sign.gpg import GPG
from ahriman.core.utils import atomic_move, package_like, symlink_relative from ahriman.core.utils import atomic_move, package_like, symlink_relative
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.pacman_synchronization import PacmanSynchronization
from ahriman.models.repository_paths import RepositoryPaths from ahriman.models.repository_paths import RepositoryPaths
@@ -43,27 +45,29 @@ def migrate_data(connection: Connection, configuration: Configuration) -> None:
for repository_id in Explorer.repositories_extract(configuration): for repository_id in Explorer.repositories_extract(configuration):
paths = replace(configuration.repository_paths, repository_id=repository_id) paths = replace(configuration.repository_paths, repository_id=repository_id)
pacman = Pacman(repository_id, configuration, refresh_database=PacmanSynchronization.Disabled)
# create archive directory if required # create archive directory if required
if not paths.archive.is_dir(): if not paths.archive.is_dir():
with paths.preserve_owner(): with paths.preserve_owner():
paths.archive.mkdir(mode=0o755, parents=True) paths.archive.mkdir(mode=0o755, parents=True)
move_packages(paths) move_packages(paths, pacman)
def move_packages(repository_paths: RepositoryPaths) -> None: def move_packages(repository_paths: RepositoryPaths, pacman: Pacman) -> None:
""" """
move packages from repository to archive and create symbolic links move packages from repository to archive and create symbolic links
Args: Args:
repository_paths(RepositoryPaths): repository paths instance repository_paths(RepositoryPaths): repository paths instance
pacman(Pacman): alpm wrapper instance
""" """
for archive in filter(package_like, repository_paths.repository.iterdir()): for archive in filter(package_like, repository_paths.repository.iterdir()):
if not archive.is_file(follow_symlinks=False): if not archive.is_file(follow_symlinks=False):
continue # skip symbolic links if any continue # skip symbolic links if any
package = Package.from_archive(archive) package = Package.from_archive(archive, pacman)
artifacts = [archive] artifacts = [archive]
# check if there are signatures for this package and append it here too # check if there are signatures for this package and append it here too
if (signature := GPG.signature(archive)).exists(): if (signature := GPG.signature(archive)).exists():

View File

@@ -17,10 +17,14 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
from collections.abc import Callable
from functools import cmp_to_key
from ahriman.core import context from ahriman.core import context
from ahriman.core.alpm.pacman import Pacman
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.repository import Repository
from ahriman.core.triggers import Trigger from ahriman.core.triggers import Trigger
from ahriman.core.utils import package_like
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.repository_id import RepositoryId from ahriman.models.repository_id import RepositoryId
from ahriman.models.result import Result from ahriman.models.result import Result
@@ -74,20 +78,27 @@ class ArchiveRotationTrigger(Trigger):
""" """
return list(cls.CONFIGURATION_SCHEMA.keys()) return list(cls.CONFIGURATION_SCHEMA.keys())
def archives_remove(self, package: Package, repository: Repository) -> None: def archives_remove(self, package: Package, pacman: Pacman) -> None:
""" """
remove older versions of the specified package remove older versions of the specified package
Args: Args:
package(Package): package which has been updated to check for older versions package(Package): package which has been updated to check for older versions
repository(Repository): repository instance pacman(Pacman): alpm wrapper instance
""" """
# explicit guard to skip process in case if rotation is disabled # explicit guard to skip process in case if rotation is disabled
# this guard is supposed to speedup process # this guard is supposed to speedup process
if self.keep_built_packages == 0: if self.keep_built_packages == 0:
return return
to_remove = repository.package_archives(package.base) packages: dict[tuple[str, str], Package] = {}
# we can't use here load_archives, because it ignores versions
for full_path in filter(package_like, self.paths.archive_for(package.base).iterdir()):
local = Package.from_archive(full_path, pacman)
packages.setdefault((local.base, local.version), local).packages.update(local.packages)
comparator: Callable[[Package, Package], int] = lambda left, right: left.vercmp(right.version)
to_remove = sorted(packages.values(), key=cmp_to_key(comparator))
for single in to_remove[:-self.keep_built_packages]: for single in to_remove[:-self.keep_built_packages]:
self.logger.info("removing version %s of package %s", single.version, single.base) self.logger.info("removing version %s of package %s", single.version, single.base)
@@ -104,7 +115,7 @@ class ArchiveRotationTrigger(Trigger):
packages(list[Package]): list of all available packages packages(list[Package]): list of all available packages
""" """
ctx = context.get() ctx = context.get()
repository = ctx.get(Repository) pacman = ctx.get(Pacman)
for package in result.success: for package in result.success:
self.archives_remove(package, repository) self.archives_remove(package, pacman)

View File

@@ -57,7 +57,7 @@ class Executor(PackageInfo, Cleaner):
for path in filter(package_like, archive.iterdir()): for path in filter(package_like, archive.iterdir()):
# check if package version is the same # check if package version is the same
built = Package.from_archive(path) built = Package.from_archive(path, self.pacman)
if built.version != package.version: if built.version != package.version:
continue continue
@@ -117,7 +117,7 @@ class Executor(PackageInfo, Cleaner):
else: else:
built = task.build(path, PACKAGER=packager) built = task.build(path, PACKAGER=packager)
package.with_packages(built) package.with_packages(built, self.pacman)
for src in built: for src in built:
dst = self.paths.packages / src.name dst = self.paths.packages / src.name
atomic_move(src, dst) atomic_move(src, dst)

View File

@@ -19,8 +19,7 @@
# #
import copy import copy
from collections.abc import Callable, Iterable from collections.abc import Iterable
from functools import cmp_to_key
from pathlib import Path from pathlib import Path
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
@@ -87,7 +86,7 @@ class PackageInfo(RepositoryProperties):
# we are iterating over bases, not single packages # we are iterating over bases, not single packages
for full_path in packages: for full_path in packages:
try: try:
local = Package.from_archive(full_path) local = Package.from_archive(full_path, self.pacman)
if (source := sources.get(local.base)) is not None: # update source with remote if (source := sources.get(local.base)) is not None: # update source with remote
local.remote = source local.remote = source
@@ -103,27 +102,6 @@ class PackageInfo(RepositoryProperties):
self.logger.exception("could not load package from %s", full_path) self.logger.exception("could not load package from %s", full_path)
return list(result.values()) return list(result.values())
def package_archives(self, package_base: str) -> list[Package]:
"""
load list of packages known for this package base. This method unlike
:func:`ahriman.core.repository.package_info.PackageInfo.load_archives` scans archive directory and loads all
versions available for the ``package_base``
Args:
package_base(str): package base
Returns:
list[Package]: list of packages belonging to this base, sorted by version by ascension
"""
packages: dict[tuple[str, str], Package] = {}
# we can't use here load_archives, because it ignores versions
for full_path in filter(package_like, self.paths.archive_for(package_base).iterdir()):
local = Package.from_archive(full_path)
packages.setdefault((local.base, local.version), local).packages.update(local.packages)
comparator: Callable[[Package, Package], int] = lambda left, right: left.vercmp(right.version)
return sorted(packages.values(), key=cmp_to_key(comparator))
def package_changes(self, package: Package, last_commit_sha: str) -> Changes | None: def package_changes(self, package: Package, last_commit_sha: str) -> Changes | None:
""" """
extract package change for the package since last commit if available extract package change for the package since last commit if available

View File

@@ -31,7 +31,6 @@ from ahriman.core.log import LazyLogging
from ahriman.core.utils import dataclass_view, full_version, list_flatmap, parse_version, srcinfo_property_list from ahriman.core.utils import dataclass_view, full_version, list_flatmap, parse_version, srcinfo_property_list
from ahriman.models.package_description import PackageDescription from ahriman.models.package_description import PackageDescription
from ahriman.models.package_source import PackageSource from ahriman.models.package_source import PackageSource
from ahriman.models.pacman_handle import PacmanHandle
from ahriman.models.pkgbuild import Pkgbuild from ahriman.models.pkgbuild import Pkgbuild
from ahriman.models.remote_source import RemoteSource from ahriman.models.remote_source import RemoteSource
@@ -187,17 +186,18 @@ class Package(LazyLogging):
return sorted(packages) return sorted(packages)
@classmethod @classmethod
def from_archive(cls, path: Path) -> Self: def from_archive(cls, path: Path, pacman: Pacman) -> Self:
""" """
construct package properties from package archive construct package properties from package archive
Args: Args:
path(Path): path to package archive path(Path): path to package archive
pacman(Pacman): alpm wrapper instance
Returns: Returns:
Self: package properties Self: package properties
""" """
package = PacmanHandle.ephemeral().package_load(path) package = pacman.handle.load_pkg(str(path))
description = PackageDescription.from_package(package, path) description = PackageDescription.from_package(package, path)
return cls( return cls(
base=package.base or package.name, base=package.base or package.name,
@@ -400,16 +400,17 @@ class Package(LazyLogging):
""" """
return dataclass_view(self) return dataclass_view(self)
def with_packages(self, packages: Iterable[Path]) -> None: def with_packages(self, packages: Iterable[Path], pacman: Pacman) -> None:
""" """
replace packages descriptions with ones from archives replace packages descriptions with ones from archives
Args: Args:
packages(Iterable[Path]): paths to package archives packages(Iterable[Path]): paths to package archives
pacman(Pacman): alpm wrapper instance
""" """
self.packages = {} # reset state self.packages = {} # reset state
for package in packages: for package in packages:
archive = self.from_archive(package) archive = self.from_archive(package, pacman)
if archive.base != self.base: if archive.base != self.base:
continue continue

View File

@@ -1,81 +0,0 @@
#
# Copyright (c) 2021-2026 ahriman team.
#
# This file is part of ahriman
# (see https://github.com/arcan1s/ahriman).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from pathlib import Path
from pyalpm import Handle, Package # type: ignore[import-not-found]
from tempfile import TemporaryDirectory
from typing import Any, ClassVar, Self
class PacmanHandle:
"""
lightweight wrapper for pacman handle to be used for direct alpm operations (e.g. package load)
Attributes:
handle(Handle): pyalpm handle instance
"""
_ephemeral: ClassVar[Self | None] = None
def __init__(self, *args: Any, **kwargs: Any) -> None:
"""
Args:
*args(Any): positional arguments for :class:`pyalpm.Handle`
**kwargs(Any): keyword arguments for :class:`pyalpm.Handle`
"""
self.handle = Handle(*args, **kwargs)
@classmethod
def ephemeral(cls) -> Self:
"""
create temporary instance with no access to real databases
Returns:
Self: loaded class
"""
if cls._ephemeral is None:
# handle creates alpm version file, but we don't use it
# so it is ok to just remove it
with TemporaryDirectory(ignore_cleanup_errors=True) as dir_name:
cls._ephemeral = cls("/", dir_name)
return cls._ephemeral
def package_load(self, path: Path) -> Package:
"""
load package from path to the archive
Args:
path(Path): path to package archive
Returns:
Package: package instance
"""
return self.handle.load_pkg(str(path))
def __getattr__(self, item: str) -> Any:
"""
proxy methods for :class:`pyalpm.Handle`, because it doesn't allow subclassing
Args:
item(str): property name
Returns:
Any: attribute by its name
"""
return self.handle.__getattribute__(item)

View File

@@ -309,44 +309,6 @@ def test_subparsers_package_changes_remove_package_changes(parser: argparse.Argu
assert dir(args) == dir(reference_args) assert dir(args) == dir(reference_args)
def test_subparsers_package_pkgbuild(parser: argparse.ArgumentParser) -> None:
"""
package-pkgbuild command must imply action, exit code, lock, quiet, report and unsafe
"""
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "package-pkgbuild", "ahriman"])
assert args.action == Action.List
assert args.architecture == "x86_64"
assert not args.exit_code
assert args.lock is None
assert args.quiet
assert not args.report
assert args.repository == "repo"
assert args.unsafe
def test_subparsers_package_pkgbuild_remove(parser: argparse.ArgumentParser) -> None:
"""
package-pkgbuild-remove command must imply action, lock, quiet, report and unsafe
"""
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "package-pkgbuild-remove", "ahriman"])
assert args.action == Action.Remove
assert args.architecture == "x86_64"
assert args.lock is None
assert args.quiet
assert not args.report
assert args.repository == "repo"
assert args.unsafe
def test_subparsers_package_pkgbuild_remove_package_pkgbuild(parser: argparse.ArgumentParser) -> None:
"""
package-pkgbuild-remove must have same keys as package-pkgbuild
"""
args = parser.parse_args(["-a", "x86_64", "-r", "repo", "package-pkgbuild-remove", "ahriman"])
reference_args = parser.parse_args(["-a", "x86_64", "-r", "repo", "package-pkgbuild", "ahriman"])
assert dir(args) == dir(reference_args)
def test_subparsers_package_copy_option_architecture(parser: argparse.ArgumentParser) -> None: def test_subparsers_package_copy_option_architecture(parser: argparse.ArgumentParser) -> None:
""" """
package-copy command must correctly parse architecture list package-copy command must correctly parse architecture list

View File

@@ -34,7 +34,8 @@ def test_migrate_package_depends(connection: Connection, configuration: Configur
package_mock = mocker.patch("ahriman.models.package.Package.from_archive", return_value=package_ahriman) package_mock = mocker.patch("ahriman.models.package.Package.from_archive", return_value=package_ahriman)
migrate_package_depends(connection, configuration) migrate_package_depends(connection, configuration)
package_mock.assert_called_once_with(package_ahriman.packages[package_ahriman.base].filepath) package_mock.assert_called_once_with(
package_ahriman.packages[package_ahriman.base].filepath, pytest.helpers.anyvar(int))
connection.executemany.assert_called_once_with(pytest.helpers.anyvar(str, strict=True), [{ connection.executemany.assert_called_once_with(pytest.helpers.anyvar(str, strict=True), [{
"make_depends": package_ahriman.packages[package_ahriman.base].make_depends, "make_depends": package_ahriman.packages[package_ahriman.base].make_depends,
"opt_depends": package_ahriman.packages[package_ahriman.base].opt_depends, "opt_depends": package_ahriman.packages[package_ahriman.base].opt_depends,

View File

@@ -34,7 +34,8 @@ def test_migrate_package_depends(connection: Connection, configuration: Configur
package_mock = mocker.patch("ahriman.models.package.Package.from_archive", return_value=package_ahriman) package_mock = mocker.patch("ahriman.models.package.Package.from_archive", return_value=package_ahriman)
migrate_package_check_depends(connection, configuration) migrate_package_check_depends(connection, configuration)
package_mock.assert_called_once_with(package_ahriman.packages[package_ahriman.base].filepath) package_mock.assert_called_once_with(
package_ahriman.packages[package_ahriman.base].filepath, pytest.helpers.anyvar(int))
connection.executemany.assert_called_once_with(pytest.helpers.anyvar(str, strict=True), [{ connection.executemany.assert_called_once_with(pytest.helpers.anyvar(str, strict=True), [{
"check_depends": package_ahriman.packages[package_ahriman.base].check_depends, "check_depends": package_ahriman.packages[package_ahriman.base].check_depends,
"package": package_ahriman.base, "package": package_ahriman.base,

View File

@@ -34,7 +34,8 @@ def test_migrate_package_base_packager(connection: Connection, configuration: Co
package_mock = mocker.patch("ahriman.models.package.Package.from_archive", return_value=package_ahriman) package_mock = mocker.patch("ahriman.models.package.Package.from_archive", return_value=package_ahriman)
migrate_package_base_packager(connection, configuration) migrate_package_base_packager(connection, configuration)
package_mock.assert_called_once_with(package_ahriman.packages[package_ahriman.base].filepath) package_mock.assert_called_once_with(
package_ahriman.packages[package_ahriman.base].filepath, pytest.helpers.anyvar(int))
connection.executemany.assert_called_once_with(pytest.helpers.anyvar(str, strict=True), [{ connection.executemany.assert_called_once_with(pytest.helpers.anyvar(str, strict=True), [{
"package_base": package_ahriman.base, "package_base": package_ahriman.base,
"packager": package_ahriman.packager, "packager": package_ahriman.packager,

View File

@@ -7,6 +7,7 @@ from sqlite3 import Connection
from typing import Any from typing import Any
from unittest.mock import call as MockCall from unittest.mock import call as MockCall
from ahriman.core.alpm.pacman import Pacman
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.database.migrations.m016_archive import migrate_data, move_packages from ahriman.core.database.migrations.m016_archive import migrate_data, move_packages
from ahriman.models.package import Package from ahriman.models.package import Package
@@ -27,12 +28,12 @@ def test_migrate_data(connection: Connection, configuration: Configuration, mock
migrate_data(connection, configuration) migrate_data(connection, configuration)
migration_mock.assert_has_calls([ migration_mock.assert_has_calls([
MockCall(replace(configuration.repository_paths, repository_id=repository)) MockCall(replace(configuration.repository_paths, repository_id=repository), pytest.helpers.anyvar(int))
for repository in repositories for repository in repositories
]) ])
def test_move_packages(repository_paths: RepositoryPaths, package_ahriman: Package, def test_move_packages(repository_paths: RepositoryPaths, pacman: Pacman, package_ahriman: Package,
mocker: MockerFixture) -> None: mocker: MockerFixture) -> None:
""" """
must move packages to the archive directory must move packages to the archive directory
@@ -56,9 +57,9 @@ def test_move_packages(repository_paths: RepositoryPaths, package_ahriman: Packa
move_mock = mocker.patch("ahriman.core.database.migrations.m016_archive.atomic_move") move_mock = mocker.patch("ahriman.core.database.migrations.m016_archive.atomic_move")
symlink_mock = mocker.patch("pathlib.Path.symlink_to") symlink_mock = mocker.patch("pathlib.Path.symlink_to")
move_packages(repository_paths) move_packages(repository_paths, pacman)
archive_mock.assert_has_calls([ archive_mock.assert_has_calls([
MockCall(repository_paths.repository / filename) MockCall(repository_paths.repository / filename, pacman)
for filename in ("file.pkg.tar.xz", "file2.pkg.tar.xz") for filename in ("file.pkg.tar.xz", "file2.pkg.tar.xz")
]) ])
move_mock.assert_has_calls([ move_mock.assert_has_calls([

View File

@@ -3,11 +3,12 @@ import pytest
from dataclasses import replace from dataclasses import replace
from pathlib import Path from pathlib import Path
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
from typing import Any
from unittest.mock import call as MockCall from unittest.mock import call as MockCall
from ahriman.core.alpm.pacman import Pacman
from ahriman.core.configuration import Configuration from ahriman.core.configuration import Configuration
from ahriman.core.housekeeping import ArchiveRotationTrigger from ahriman.core.housekeeping import ArchiveRotationTrigger
from ahriman.core.repository import Repository
from ahriman.models.package import Package from ahriman.models.package import Package
from ahriman.models.result import Result from ahriman.models.result import Result
@@ -20,24 +21,26 @@ def test_configuration_sections(configuration: Configuration) -> None:
def test_archives_remove(archive_rotation_trigger: ArchiveRotationTrigger, package_ahriman: Package, def test_archives_remove(archive_rotation_trigger: ArchiveRotationTrigger, package_ahriman: Package,
repository: Repository, mocker: MockerFixture) -> None: pacman: Pacman, mocker: MockerFixture) -> None:
""" """
must remove older packages must remove older packages
""" """
packages = [] def package(version: Any, *args: Any, **kwargs: Any) -> Package:
for i in range(5): generated = replace(package_ahriman, version=str(version))
generated = replace(package_ahriman, version=str(i))
generated.packages = { generated.packages = {
key: replace(value, filename=str(i)) key: replace(value, filename=str(version))
for key, value in generated.packages.items() for key, value in generated.packages.items()
} }
packages.append(generated) return generated
mocker.patch("ahriman.core.repository.package_info.PackageInfo.package_archives", return_value=packages) mocker.patch("pathlib.Path.is_dir", return_value=True)
mocker.patch("ahriman.core.housekeeping.archive_rotation_trigger.package_like", return_value=True)
mocker.patch("pathlib.Path.glob", return_value=[Path(str(i)) for i in range(5)]) mocker.patch("pathlib.Path.glob", return_value=[Path(str(i)) for i in range(5)])
mocker.patch("pathlib.Path.iterdir", return_value=[Path(str(i)) for i in range(5)])
mocker.patch("ahriman.models.package.Package.from_archive", side_effect=package)
unlink_mock = mocker.patch("pathlib.Path.unlink", autospec=True) unlink_mock = mocker.patch("pathlib.Path.unlink", autospec=True)
archive_rotation_trigger.archives_remove(package_ahriman, repository) archive_rotation_trigger.archives_remove(package_ahriman, pacman)
unlink_mock.assert_has_calls([ unlink_mock.assert_has_calls([
MockCall(Path("0")), MockCall(Path("0")),
MockCall(Path("1")), MockCall(Path("1")),
@@ -45,15 +48,28 @@ def test_archives_remove(archive_rotation_trigger: ArchiveRotationTrigger, packa
def test_archives_remove_keep(archive_rotation_trigger: ArchiveRotationTrigger, package_ahriman: Package, def test_archives_remove_keep(archive_rotation_trigger: ArchiveRotationTrigger, package_ahriman: Package,
repository: Repository, mocker: MockerFixture) -> None: pacman: Pacman, mocker: MockerFixture) -> None:
""" """
must keep all packages if set to must keep all packages if set to
""" """
archives_mock = mocker.patch("ahriman.core.repository.package_info.PackageInfo.package_archives") def package(version: Any, *args: Any, **kwargs: Any) -> Package:
generated = replace(package_ahriman, version=str(version))
generated.packages = {
key: replace(value, filename=str(version))
for key, value in generated.packages.items()
}
return generated
mocker.patch("pathlib.Path.is_dir", return_value=True)
mocker.patch("ahriman.core.housekeeping.archive_rotation_trigger.package_like", return_value=True)
mocker.patch("pathlib.Path.glob", return_value=[Path(str(i)) for i in range(5)])
mocker.patch("pathlib.Path.iterdir", return_value=[Path(str(i)) for i in range(5)])
mocker.patch("ahriman.models.package.Package.from_archive", side_effect=package)
unlink_mock = mocker.patch("pathlib.Path.unlink", autospec=True)
archive_rotation_trigger.keep_built_packages = 0 archive_rotation_trigger.keep_built_packages = 0
archive_rotation_trigger.archives_remove(package_ahriman, repository) archive_rotation_trigger.archives_remove(package_ahriman, pacman)
archives_mock.assert_not_called() unlink_mock.assert_not_called()
def test_on_result(archive_rotation_trigger: ArchiveRotationTrigger, package_ahriman: Package, def test_on_result(archive_rotation_trigger: ArchiveRotationTrigger, package_ahriman: Package,

View File

@@ -118,7 +118,7 @@ def test_package_build(executor: Executor, package_ahriman: Package, mocker: Moc
init_mock.assert_called_once_with(pytest.helpers.anyvar(int), pytest.helpers.anyvar(int), None) init_mock.assert_called_once_with(pytest.helpers.anyvar(int), pytest.helpers.anyvar(int), None)
package_mock.assert_called_once_with(Path("local"), executor.architecture, None) package_mock.assert_called_once_with(Path("local"), executor.architecture, None)
lookup_mock.assert_called_once_with(package_ahriman) lookup_mock.assert_called_once_with(package_ahriman)
with_packages_mock.assert_called_once_with([Path(package_ahriman.base)]) with_packages_mock.assert_called_once_with([Path(package_ahriman.base)], executor.pacman)
rename_mock.assert_called_once_with(Path(package_ahriman.base), executor.paths.packages / package_ahriman.base) rename_mock.assert_called_once_with(Path(package_ahriman.base), executor.paths.packages / package_ahriman.base)

View File

@@ -91,30 +91,6 @@ def test_load_archives_different_version(package_info: PackageInfo, package_pyth
assert packages[0].version == package_python_schedule.version assert packages[0].version == package_python_schedule.version
def test_package_archives(package_info: PackageInfo, package_ahriman: Package, mocker: MockerFixture) -> None:
"""
must load package archives sorted by version
"""
from dataclasses import replace
from typing import Any
def package(version: Any, *args: Any, **kwargs: Any) -> Package:
generated = replace(package_ahriman, version=str(version))
generated.packages = {
key: replace(value, filename=str(version))
for key, value in generated.packages.items()
}
return generated
mocker.patch("ahriman.core.repository.package_info.package_like", return_value=True)
mocker.patch("pathlib.Path.iterdir", return_value=[Path(str(i)) for i in range(5)])
mocker.patch("ahriman.models.package.Package.from_archive", side_effect=package)
result = package_info.package_archives(package_ahriman.base)
assert len(result) == 5
assert [p.version for p in result] == [str(i) for i in range(5)]
def test_package_changes(package_info: PackageInfo, package_ahriman: Package, mocker: MockerFixture) -> None: def test_package_changes(package_info: PackageInfo, package_ahriman: Package, mocker: MockerFixture) -> None:
""" """
must load package changes must load package changes

View File

@@ -89,6 +89,22 @@ def pkgbuild_ahriman(resource_path_root: Path) -> Pkgbuild:
return Pkgbuild.from_file(pkgbuild) return Pkgbuild.from_file(pkgbuild)
@pytest.fixture
def pyalpm_handle(pyalpm_package_ahriman: MagicMock) -> MagicMock:
"""
mock object for pyalpm
Args:
pyalpm_package_ahriman(MagicMock): mock object for pyalpm package
Returns:
MagicMock: pyalpm mock
"""
mock = MagicMock()
mock.handle.load_pkg.return_value = pyalpm_package_ahriman
return mock
@pytest.fixture @pytest.fixture
def pyalpm_package_description_ahriman(package_description_ahriman: PackageDescription) -> MagicMock: def pyalpm_package_description_ahriman(package_description_ahriman: PackageDescription) -> MagicMock:
""" """

View File

@@ -148,14 +148,13 @@ def test_packages_full(package_ahriman: Package) -> None:
assert package_ahriman.packages_full == [package_ahriman.base, f"{package_ahriman.base}-git"] assert package_ahriman.packages_full == [package_ahriman.base, f"{package_ahriman.base}-git"]
def test_from_archive(package_ahriman: Package, pyalpm_package_ahriman: MagicMock, mocker: MockerFixture) -> None: def test_from_archive(package_ahriman: Package, pyalpm_handle: MagicMock, mocker: MockerFixture) -> None:
""" """
must construct package from alpm library must construct package from alpm library
""" """
mocker.patch("ahriman.models.pacman_handle.PacmanHandle.package_load", return_value=pyalpm_package_ahriman)
mocker.patch("ahriman.models.package_description.PackageDescription.from_package", mocker.patch("ahriman.models.package_description.PackageDescription.from_package",
return_value=package_ahriman.packages[package_ahriman.base]) return_value=package_ahriman.packages[package_ahriman.base])
generated = Package.from_archive(Path("path")) generated = Package.from_archive(Path("path"), pyalpm_handle)
generated.remote = package_ahriman.remote generated.remote = package_ahriman.remote
assert generated == package_ahriman assert generated == package_ahriman
@@ -166,12 +165,13 @@ def test_from_archive_empty_base(package_ahriman: Package, pyalpm_package_ahrima
""" """
must construct package with empty base from alpm library must construct package with empty base from alpm library
""" """
pyalpm_handle = MagicMock()
type(pyalpm_package_ahriman).base = PropertyMock(return_value=None) type(pyalpm_package_ahriman).base = PropertyMock(return_value=None)
mocker.patch("ahriman.models.pacman_handle.PacmanHandle.package_load", return_value=pyalpm_package_ahriman) pyalpm_handle.handle.load_pkg.return_value = pyalpm_package_ahriman
mocker.patch("ahriman.models.package_description.PackageDescription.from_package", mocker.patch("ahriman.models.package_description.PackageDescription.from_package",
return_value=package_ahriman.packages[package_ahriman.base]) return_value=package_ahriman.packages[package_ahriman.base])
generated = Package.from_archive(Path("path")) generated = Package.from_archive(Path("path"), pyalpm_handle)
generated.remote = package_ahriman.remote generated.remote = package_ahriman.remote
assert generated == package_ahriman assert generated == package_ahriman
@@ -362,7 +362,7 @@ def test_vercmp(package_ahriman: Package, mocker: MockerFixture) -> None:
vercmp_mock.assert_called_once_with(package_ahriman.version, "version") vercmp_mock.assert_called_once_with(package_ahriman.version, "version")
def test_with_packages(package_ahriman: Package, package_python_schedule: Package, def test_with_packages(package_ahriman: Package, package_python_schedule: Package, pacman: Pacman,
mocker: MockerFixture) -> None: mocker: MockerFixture) -> None:
""" """
must correctly replace packages descriptions must correctly replace packages descriptions
@@ -375,8 +375,8 @@ def test_with_packages(package_ahriman: Package, package_python_schedule: Packag
result = copy.deepcopy(package_ahriman) result = copy.deepcopy(package_ahriman)
package_ahriman.packages[package_ahriman.base].architecture = "i686" package_ahriman.packages[package_ahriman.base].architecture = "i686"
result.with_packages(paths) result.with_packages(paths, pacman)
from_archive_mock.assert_has_calls([ from_archive_mock.assert_has_calls([
MockCall(path) for path in paths MockCall(path, pacman) for path in paths
]) ])
assert result.packages[result.base] == package_ahriman.packages[package_ahriman.base] assert result.packages[result.base] == package_ahriman.packages[package_ahriman.base]

View File

@@ -1,37 +0,0 @@
import pytest
from pathlib import Path
from unittest.mock import MagicMock
from ahriman.models.pacman_handle import PacmanHandle
def test_package_load() -> None:
"""
must load package from archive path
"""
local = Path("local")
instance = PacmanHandle.ephemeral()
handle_mock = instance.handle = MagicMock()
instance.package_load(local)
handle_mock.load_pkg.assert_called_once_with(str(local))
PacmanHandle._ephemeral = None
def test_getattr() -> None:
"""
must proxy attribute access to underlying handle
"""
instance = PacmanHandle.ephemeral()
assert instance.dbpath
def test_getattr_not_found() -> None:
"""
must raise AttributeError for missing handle attributes
"""
instance = PacmanHandle.ephemeral()
with pytest.raises(AttributeError):
assert instance.random_attribute