From 3b6b2efcb111a2893a494caa35ad534d919f5baf Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Tue, 5 Oct 2021 08:57:42 +0300 Subject: [PATCH] patch control subcommands --- README.md | 1 + src/ahriman/application/ahriman.py | 103 +++++++++++---- src/ahriman/application/handlers/__init__.py | 1 + src/ahriman/application/handlers/patch.py | 98 ++++++++++++++ src/ahriman/core/build_tools/sources.py | 77 ++++++++--- src/ahriman/models/action.py | 33 +++++ .../handlers/test_handler_patch.py | 123 ++++++++++++++++++ tests/ahriman/application/test_ahriman.py | 68 +++++++++- .../ahriman/core/build_tools/test_sources.py | 82 ++++++++---- 9 files changed, 516 insertions(+), 70 deletions(-) create mode 100644 src/ahriman/application/handlers/patch.py create mode 100644 src/ahriman/models/action.py create mode 100644 tests/ahriman/application/handlers/test_handler_patch.py diff --git a/README.md b/README.md index 7aac28a1..792129dd 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ Wrapper for managing custom repository inspired by [repo-scripts](https://github * Sign support with gpg (repository, package, per package settings). * Synchronization to remote services (rsync, s3) and report generation (html). * Dependency manager. +* Ability to patch AUR packages. * Repository status interface with optional authorization and control options: ![web interface](web.png) diff --git a/src/ahriman/application/ahriman.py b/src/ahriman/application/ahriman.py index 93b1a73d..d77e9846 100644 --- a/src/ahriman/application/ahriman.py +++ b/src/ahriman/application/ahriman.py @@ -25,6 +25,7 @@ from pathlib import Path from ahriman import version from ahriman.application import handlers +from ahriman.models.action import Action from ahriman.models.build_status import BuildStatusEnum from ahriman.models.package_source import PackageSource from ahriman.models.sign_settings import SignSettings @@ -35,13 +36,22 @@ from ahriman.models.user_access import UserAccess SubParserAction = argparse._SubParsersAction # pylint: disable=protected-access +def _formatter(prog: str) -> argparse.HelpFormatter: + """ + formatter for the help message + :param prog: application name + :return: formatter used by default + """ + return argparse.ArgumentDefaultsHelpFormatter(prog, width=120) + + def _parser() -> argparse.ArgumentParser: """ command line parser generator :return: command line parser for the application """ parser = argparse.ArgumentParser(prog="ahriman", description="ArcH Linux ReposItory MANager", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("-a", "--architecture", help="target architectures (can be used multiple times)", action="append") parser.add_argument("-c", "--configuration", help="configuration path", type=Path, default=Path("/etc/ahriman.ini")) @@ -57,7 +67,7 @@ def _parser() -> argparse.ArgumentParser: parser.add_argument("--unsafe", help="allow to run ahriman as non-ahriman user", action="store_true") parser.add_argument("-v", "--version", action="version", version=version.__version__) - subparsers = parser.add_subparsers(title="command", help="command to run", dest="command", required=True) + subparsers = parser.add_subparsers(title="command", help="command to run", dest="command") _set_add_parser(subparsers) _set_check_parser(subparsers) @@ -65,6 +75,9 @@ def _parser() -> argparse.ArgumentParser: _set_config_parser(subparsers) _set_init_parser(subparsers) _set_key_import_parser(subparsers) + _set_patch_add_parser(subparsers) + _set_patch_list_parser(subparsers) + _set_patch_remove_parser(subparsers) _set_rebuild_parser(subparsers) _set_remove_parser(subparsers) _set_remove_unknown_parser(subparsers) @@ -88,8 +101,7 @@ def _set_add_parser(root: SubParserAction) -> argparse.ArgumentParser: :param root: subparsers for the commands :return: created argument parser """ - parser = root.add_parser("add", help="add package", description="add package", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser = root.add_parser("add", help="add package", description="add package", formatter_class=_formatter) parser.add_argument("package", help="package base/name or archive path", nargs="+") parser.add_argument("--now", help="run update function after", action="store_true") parser.add_argument("--source", help="package source", choices=PackageSource, type=PackageSource, @@ -107,7 +119,7 @@ def _set_check_parser(root: SubParserAction) -> argparse.ArgumentParser: """ parser = root.add_parser("check", help="check for updates", description="check for updates. Same as update --dry-run --no-manual", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("package", help="filter check by package base", nargs="*") parser.add_argument("--no-vcs", help="do not check VCS packages", action="store_true") parser.set_defaults(handler=handlers.Update, no_aur=False, no_manual=True, dry_run=True) @@ -121,7 +133,7 @@ def _set_clean_parser(root: SubParserAction) -> argparse.ArgumentParser: :return: created argument parser """ parser = root.add_parser("clean", help="clean local caches", description="clear local caches", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("--no-build", help="do not clear directory with package sources", action="store_true") parser.add_argument("--no-cache", help="do not clear directory with package caches", action="store_true") parser.add_argument("--no-chroot", help="do not clear build chroot", action="store_true") @@ -139,7 +151,7 @@ def _set_config_parser(root: SubParserAction) -> argparse.ArgumentParser: """ parser = root.add_parser("config", help="dump configuration", description="dump configuration for specified architecture", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.set_defaults(handler=handlers.Dump, lock=None, quiet=True, no_report=True, unsafe=True) return parser @@ -152,7 +164,7 @@ def _set_init_parser(root: SubParserAction) -> argparse.ArgumentParser: """ parser = root.add_parser("init", help="create repository tree", description="create empty repository tree. Optional command for auto architecture support", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.set_defaults(handler=handlers.Init, no_report=True) return parser @@ -165,13 +177,59 @@ def _set_key_import_parser(root: SubParserAction) -> argparse.ArgumentParser: """ parser = root.add_parser("key-import", help="import PGP key", description="import PGP key from public sources to repository user", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("--key-server", help="key server for key import", default="pgp.mit.edu") parser.add_argument("key", help="PGP key to import from public server") parser.set_defaults(handler=handlers.KeyImport, architecture=[""], lock=None, no_report=True) return parser +def _set_patch_add_parser(root: SubParserAction) -> argparse.ArgumentParser: + """ + add parser for new patch subcommand + :param root: subparsers for the commands + :return: created argument parser + """ + parser = root.add_parser("patch-add", help="patches control", description="create/update for sources", + epilog="In order to add a patch set for the package you will need to clone " + "the AUR package manually, add required changes (e.g. external patches, " + "edit PKGBUILD) and run command, e.g. `ahriman patch path/to/directory`. " + "By default it tracks *.patch and *.diff files, but this behavior can be changed " + "by using --track option", + formatter_class=_formatter) + parser.add_argument("package", help="path to directory with changed files for patch addition/update") + parser.add_argument("-t", "--track", help="files which has to be tracked", action="append", + default=["*.diff", "*.patch"]) + parser.set_defaults(handler=handlers.Patch, action=Action.Update, architecture=[""], lock=None, no_report=True) + return parser + + +def _set_patch_list_parser(root: SubParserAction) -> argparse.ArgumentParser: + """ + add parser for list patches subcommand + :param root: subparsers for the commands + :return: created argument parser + """ + parser = root.add_parser("patch-list", help="patches control", description="list available patches for the package", + formatter_class=_formatter) + parser.add_argument("package", help="package base") + parser.set_defaults(handler=handlers.Patch, action=Action.List, architecture=[""], lock=None, no_report=True) + return parser + + +def _set_patch_remove_parser(root: SubParserAction) -> argparse.ArgumentParser: + """ + add parser for remove patches subcommand + :param root: subparsers for the commands + :return: created argument parser + """ + parser = root.add_parser("patch-remove", help="patches control", description="remove patches for the package", + formatter_class=_formatter) + parser.add_argument("package", help="package base") + parser.set_defaults(handler=handlers.Patch, action=Action.Remove, architecture=[""], lock=None, no_report=True) + return parser + + def _set_rebuild_parser(root: SubParserAction) -> argparse.ArgumentParser: """ add parser for rebuild subcommand @@ -179,7 +237,7 @@ def _set_rebuild_parser(root: SubParserAction) -> argparse.ArgumentParser: :return: created argument parser """ parser = root.add_parser("rebuild", help="rebuild repository", description="rebuild whole repository", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("--depends-on", help="only rebuild packages that depend on specified package", action="append") parser.set_defaults(handler=handlers.Rebuild) return parser @@ -191,8 +249,7 @@ def _set_remove_parser(root: SubParserAction) -> argparse.ArgumentParser: :param root: subparsers for the commands :return: created argument parser """ - parser = root.add_parser("remove", help="remove package", description="remove package", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser = root.add_parser("remove", help="remove package", description="remove package", formatter_class=_formatter) parser.add_argument("package", help="package name or base", nargs="+") parser.set_defaults(handler=handlers.Remove) return parser @@ -206,7 +263,7 @@ def _set_remove_unknown_parser(root: SubParserAction) -> argparse.ArgumentParser """ parser = root.add_parser("remove-unknown", help="remove unknown packages", description="remove packages which are missing in AUR", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("--dry-run", help="just perform check for packages without removal", action="store_true") parser.set_defaults(handler=handlers.RemoveUnknown) return parser @@ -219,7 +276,7 @@ def _set_report_parser(root: SubParserAction) -> argparse.ArgumentParser: :return: created argument parser """ parser = root.add_parser("report", help="generate report", description="generate report", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("target", help="target to generate report", nargs="*") parser.set_defaults(handler=handlers.Report) return parser @@ -245,7 +302,7 @@ def _set_setup_parser(root: SubParserAction) -> argparse.ArgumentParser: """ parser = root.add_parser("setup", help="initial service configuration", description="create initial service configuration, requires root", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("--build-command", help="build command prefix", default="ahriman") parser.add_argument("--from-configuration", help="path to default devtools pacman configuration", type=Path, default=Path("/usr/share/devtools/pacman-extra.conf")) @@ -267,7 +324,7 @@ def _set_sign_parser(root: SubParserAction) -> argparse.ArgumentParser: :return: created argument parser """ parser = root.add_parser("sign", help="sign packages", description="(re-)sign packages and repository database", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("package", help="sign only specified packages", nargs="*") parser.set_defaults(handler=handlers.Sign) return parser @@ -280,7 +337,7 @@ def _set_status_parser(root: SubParserAction) -> argparse.ArgumentParser: :return: created argument parser """ parser = root.add_parser("status", help="get package status", description="request status of the package", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("--ahriman", help="get service status itself", action="store_true") parser.add_argument("--status", help="filter packages by status", choices=BuildStatusEnum, type=BuildStatusEnum) parser.add_argument("package", help="filter status by package base", nargs="*") @@ -295,7 +352,7 @@ def _set_status_update_parser(root: SubParserAction) -> argparse.ArgumentParser: :return: created argument parser """ parser = root.add_parser("status-update", help="update package status", description="request status of the package", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument( "package", help="set status for specified packages. If no packages supplied, service status will be updated", @@ -314,7 +371,7 @@ def _set_sync_parser(root: SubParserAction) -> argparse.ArgumentParser: :return: created argument parser """ parser = root.add_parser("sync", help="sync repository", description="sync packages to remote server", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("target", help="target to sync", nargs="*") parser.set_defaults(handler=handlers.Sync) return parser @@ -326,8 +383,7 @@ def _set_update_parser(root: SubParserAction) -> argparse.ArgumentParser: :param root: subparsers for the commands :return: created argument parser """ - parser = root.add_parser("update", help="update packages", description="run updates", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser = root.add_parser("update", help="update packages", description="run updates", formatter_class=_formatter) parser.add_argument("package", help="filter check by package base", nargs="*") parser.add_argument("--dry-run", help="just perform check for updates, same as check command", action="store_true") parser.add_argument("--no-aur", help="do not check for AUR updates. Implies --no-vcs", action="store_true") @@ -347,7 +403,7 @@ def _set_user_parser(root: SubParserAction) -> argparse.ArgumentParser: "user", help="manage users for web services", description="manage users for web services with password and role. In case if password was not entered it will be asked interactively", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + formatter_class=_formatter) parser.add_argument("username", help="username for web service") parser.add_argument("--as-service", help="add user as service user", action="store_true") parser.add_argument( @@ -371,8 +427,7 @@ def _set_web_parser(root: SubParserAction) -> argparse.ArgumentParser: :param root: subparsers for the commands :return: created argument parser """ - parser = root.add_parser("web", help="start web server", description="start web server", - formatter_class=argparse.ArgumentDefaultsHelpFormatter) + parser = root.add_parser("web", help="start web server", description="start web server", formatter_class=_formatter) parser.set_defaults(handler=handlers.Web, lock=None, no_report=True, parser=_parser) return parser diff --git a/src/ahriman/application/handlers/__init__.py b/src/ahriman/application/handlers/__init__.py index 1c94dc7d..3aaf517d 100644 --- a/src/ahriman/application/handlers/__init__.py +++ b/src/ahriman/application/handlers/__init__.py @@ -24,6 +24,7 @@ from ahriman.application.handlers.clean import Clean from ahriman.application.handlers.dump import Dump from ahriman.application.handlers.init import Init from ahriman.application.handlers.key_import import KeyImport +from ahriman.application.handlers.patch import Patch from ahriman.application.handlers.rebuild import Rebuild from ahriman.application.handlers.remove import Remove from ahriman.application.handlers.remove_unknown import RemoveUnknown diff --git a/src/ahriman/application/handlers/patch.py b/src/ahriman/application/handlers/patch.py new file mode 100644 index 00000000..5fffe088 --- /dev/null +++ b/src/ahriman/application/handlers/patch.py @@ -0,0 +1,98 @@ +# +# Copyright (c) 2021 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 . +# +import argparse +import shutil + +from pathlib import Path +from typing import List, Type + +from ahriman.application.application import Application +from ahriman.application.handlers.handler import Handler +from ahriman.core.build_tools.sources import Sources +from ahriman.core.configuration import Configuration +from ahriman.models.action import Action +from ahriman.models.package import Package + + +class Patch(Handler): + """ + patch control handler + """ + + _print = print + + @classmethod + def run(cls: Type[Handler], args: argparse.Namespace, architecture: str, + configuration: Configuration, no_report: bool) -> None: + """ + callback for command line + :param args: command line args + :param architecture: repository architecture + :param configuration: configuration instance + :param no_report: force disable reporting + """ + application = Application(architecture, configuration, no_report) + + if args.action == Action.List: + Patch.patch_set_list(application, args.package) + elif args.action == Action.Remove: + Patch.patch_set_remove(application, args.package) + elif args.action == Action.Update: + Patch.patch_set_create(application, Path(args.package), args.track) + + @staticmethod + def patch_set_create(application: Application, sources_dir: Path, track: List[str]) -> None: + """ + create patch set for the package base + :param application: application instance + :param sources_dir: path to directory with the package sources + :param track: track files which match the glob before creating the patch + """ + package = Package.load(sources_dir, application.repository.pacman, application.repository.aur_url) + patch_dir = application.repository.paths.patches_for(package.base) + + if patch_dir.is_dir(): + shutil.rmtree(patch_dir) # remove old patches + patch_dir.mkdir(mode=0o755, parents=True) + + Sources.patch_create(sources_dir, patch_dir / "00-main.patch", *track) + + @staticmethod + def patch_set_list(application: Application, package_base: str) -> None: + """ + list patches available for the package base + :param application: application instance + :param package_base: package base + """ + patch_dir = application.repository.paths.patches_for(package_base) + if not patch_dir.is_dir(): + return + for patch_path in sorted(patch_dir.glob("*.patch")): + Patch._print(patch_path.name) + + @staticmethod + def patch_set_remove(application: Application, package_base: str) -> None: + """ + remove patch set for the package base + :param application: application instance + :param package_base: package base + """ + patch_dir = application.repository.paths.patches_for(package_base) + shutil.rmtree(patch_dir, ignore_errors=True) diff --git a/src/ahriman/core/build_tools/sources.py b/src/ahriman/core/build_tools/sources.py index 9ffb3157..604b3c23 100644 --- a/src/ahriman/core/build_tools/sources.py +++ b/src/ahriman/core/build_tools/sources.py @@ -20,6 +20,7 @@ import logging from pathlib import Path +from typing import List from ahriman.core.util import check_output @@ -27,10 +28,39 @@ from ahriman.core.util import check_output class Sources: """ helper to download package sources (PKGBUILD etc) + :cvar logger: class logger """ + logger = logging.getLogger("build_details") + _check_output = check_output + @staticmethod + def add(local_path: Path, *pattern: str) -> None: + """ + track found files via git + :param local_path: local path to git repository + :param pattern: glob patterns + """ + # glob directory to find files which match the specified patterns + found_files: List[Path] = [] + for glob in pattern: + found_files.extend(local_path.glob(glob)) + Sources.logger.info("found matching files %s", found_files) + # add them to index + Sources._check_output("git", "add", "--intent-to-add", *[str(fn.relative_to(local_path)) for fn in found_files], + exception=None, cwd=local_path, logger=Sources.logger) + + @staticmethod + def diff(local_path: Path, patch_path: Path) -> None: + """ + generate diff from the current version and write it to the output file + :param local_path: local path to git repository + :param patch_path: path to result patch + """ + patch = Sources._check_output("git", "diff", exception=None, cwd=local_path, logger=Sources.logger) + patch_path.write_text(patch) + @staticmethod def fetch(local_path: Path, remote: str, branch: str = "master") -> None: """ @@ -39,45 +69,56 @@ class Sources: :param remote: remote target (from where to fetch) :param branch: branch name to checkout, master by default """ - logger = logging.getLogger("build_details") # local directory exists and there is .git directory if (local_path / ".git").is_dir(): - logger.info("update HEAD to remote to %s", local_path) - Sources._check_output("git", "fetch", "origin", branch, exception=None, cwd=local_path, logger=logger) + Sources.logger.info("update HEAD to remote to %s", local_path) + Sources._check_output("git", "fetch", "origin", branch, + exception=None, cwd=local_path, logger=Sources.logger) else: - logger.info("clone remote %s to %s", remote, local_path) - Sources._check_output("git", "clone", remote, str(local_path), exception=None, logger=logger) + Sources.logger.info("clone remote %s to %s", remote, local_path) + Sources._check_output("git", "clone", remote, str(local_path), exception=None, logger=Sources.logger) # and now force reset to our branch - Sources._check_output("git", "checkout", "--force", branch, exception=None, cwd=local_path, logger=logger) + Sources._check_output("git", "checkout", "--force", branch, + exception=None, cwd=local_path, logger=Sources.logger) Sources._check_output("git", "reset", "--hard", f"origin/{branch}", - exception=None, cwd=local_path, logger=logger) + exception=None, cwd=local_path, logger=Sources.logger) @staticmethod - def load(local_path: Path, remote: str, patch_path: Path) -> None: + def load(local_path: Path, remote: str, patch_dir: Path) -> None: """ fetch sources from remote and apply patches :param local_path: local path to fetch :param remote: remote target (from where to fetch) - :param patch_path: path to directory with package patches + :param patch_dir: path to directory with package patches """ Sources.fetch(local_path, remote) - Sources.patch(local_path, patch_path) + Sources.patch_apply(local_path, patch_dir) @staticmethod - def patch(local_path: Path, patch_path: Path) -> None: + def patch_apply(local_path: Path, patch_dir: Path) -> None: """ apply patches if any :param local_path: local path to directory with git sources - :param patch_path: path to directory with package patches + :param patch_dir: path to directory with package patches """ # check if even there are patches - if not patch_path.is_dir(): + if not patch_dir.is_dir(): return # no patches provided - logger = logging.getLogger("build_details") # find everything that looks like patch and sort it - patches = sorted(patch_path.glob("*.patch")) - logger.info("found %s patches", patches) + patches = sorted(patch_dir.glob("*.patch")) + Sources.logger.info("found %s patches", patches) for patch in patches: - logger.info("apply patch %s", patch.name) + Sources.logger.info("apply patch %s", patch.name) Sources._check_output("git", "apply", "--ignore-space-change", "--ignore-whitespace", str(patch), - exception=None, cwd=local_path, logger=logger) + exception=None, cwd=local_path, logger=Sources.logger) + + @staticmethod + def patch_create(local_path: Path, patch_path: Path, *pattern: str) -> None: + """ + create patch set for the specified local path + :param local_path: local path to git repository + :param patch_path: path to result patch + :param pattern: glob patterns + """ + Sources.add(local_path, *pattern) + Sources.diff(local_path, patch_path) diff --git a/src/ahriman/models/action.py b/src/ahriman/models/action.py new file mode 100644 index 00000000..26c33da2 --- /dev/null +++ b/src/ahriman/models/action.py @@ -0,0 +1,33 @@ +# +# Copyright (c) 2021 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 . +# +from enum import Enum + + +class Action(Enum): + """ + base action enumeration + :cvar List: list available values + :cvar Remove: remove everything from local storage + :cvar Update: update local storage or add to + """ + + List = "list" + Remove = "remove" + Update = "update" diff --git a/tests/ahriman/application/handlers/test_handler_patch.py b/tests/ahriman/application/handlers/test_handler_patch.py new file mode 100644 index 00000000..51984fd5 --- /dev/null +++ b/tests/ahriman/application/handlers/test_handler_patch.py @@ -0,0 +1,123 @@ +import argparse + +from pathlib import Path +from pytest_mock import MockerFixture + +from ahriman.application.application import Application +from ahriman.application.handlers import Patch +from ahriman.core.configuration import Configuration +from ahriman.models.action import Action +from ahriman.models.package import Package + + +def _default_args(args: argparse.Namespace) -> argparse.Namespace: + """ + default arguments for these test cases + :param args: command line arguments fixture + :return: generated arguments for these test cases + """ + args.package = "ahriman" + args.remove = False + args.track = ["*.diff", "*.patch"] + return args + + +def test_run(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: + """ + must run command + """ + args = _default_args(args) + args.action = Action.Update + mocker.patch("pathlib.Path.mkdir") + application_mock = mocker.patch("ahriman.application.handlers.patch.Patch.patch_set_create") + + Patch.run(args, "x86_64", configuration, True) + application_mock.assert_called_once() + + +def test_run_list(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: + """ + must run command with list flag + """ + args = _default_args(args) + args.action = Action.List + mocker.patch("pathlib.Path.mkdir") + application_mock = mocker.patch("ahriman.application.handlers.patch.Patch.patch_set_list") + + Patch.run(args, "x86_64", configuration, True) + application_mock.assert_called_once() + + +def test_run_remove(args: argparse.Namespace, configuration: Configuration, mocker: MockerFixture) -> None: + """ + must run command with remove flag + """ + args = _default_args(args) + args.action = Action.Remove + mocker.patch("pathlib.Path.mkdir") + application_mock = mocker.patch("ahriman.application.handlers.patch.Patch.patch_set_remove") + + Patch.run(args, "x86_64", configuration, True) + application_mock.assert_called_once() + + +def test_patch_set_list(application: Application, mocker: MockerFixture) -> None: + """ + must list available patches for the command + """ + mocker.patch("pathlib.Path.is_dir", return_value=True) + glob_mock = mocker.patch("pathlib.Path.glob", return_value=[Path("local")]) + print_mock = mocker.patch("ahriman.application.handlers.patch.Patch._print") + + Patch.patch_set_list(application, "ahriman") + glob_mock.assert_called_with("*.patch") + print_mock.assert_called() + + +def test_patch_set_list_no_dir(application: Application, mocker: MockerFixture) -> None: + """ + must not fail if no patches directory found + """ + mocker.patch("pathlib.Path.is_dir", return_value=False) + glob_mock = mocker.patch("pathlib.Path.glob") + print_mock = mocker.patch("ahriman.application.handlers.patch.Patch._print") + + Patch.patch_set_list(application, "ahriman") + glob_mock.assert_not_called() + print_mock.assert_not_called() + + +def test_patch_set_create(application: Application, package_ahriman: Package, mocker: MockerFixture) -> None: + """ + must create patch set for the package + """ + mocker.patch("ahriman.models.package.Package.load", return_value=package_ahriman) + create_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.patch_create") + patch_dir = application.repository.paths.patches_for(package_ahriman.base) + + Patch.patch_set_create(application, Path("path"), ["*.patch"]) + create_mock.assert_called_with(Path("path"), patch_dir / "00-main.patch", "*.patch") + + +def test_patch_set_create_clear(application: Application, package_ahriman: Package, mocker: MockerFixture) -> None: + """ + must clear patches directory before new set creation + """ + mocker.patch("pathlib.Path.is_dir", return_value=True) + mocker.patch("ahriman.models.package.Package.load", return_value=package_ahriman) + mocker.patch("ahriman.core.build_tools.sources.Sources.patch_create") + remove_mock = mocker.patch("shutil.rmtree") + + Patch.patch_set_create(application, Path("path"), ["*.patch"]) + remove_mock.assert_called() + + +def test_patch_set_remove(application: Application, package_ahriman: Package, mocker: MockerFixture) -> None: + """ + must remove patch set for the package + """ + remove_mock = mocker.patch("shutil.rmtree") + patch_dir = application.repository.paths.patches_for(package_ahriman.base) + + Patch.patch_set_remove(application, package_ahriman.base) + remove_mock.assert_called_with(patch_dir, ignore_errors=True) diff --git a/tests/ahriman/application/test_ahriman.py b/tests/ahriman/application/test_ahriman.py index ad85ed5f..b8807551 100644 --- a/tests/ahriman/application/test_ahriman.py +++ b/tests/ahriman/application/test_ahriman.py @@ -4,6 +4,7 @@ from pathlib import Path from pytest_mock import MockerFixture from ahriman.application.handlers import Handler +from ahriman.models.action import Action from ahriman.models.build_status import BuildStatusEnum from ahriman.models.sign_settings import SignSettings from ahriman.models.user_access import UserAccess @@ -126,12 +127,77 @@ def test_subparsers_key_import(parser: argparse.ArgumentParser) -> None: def test_subparsers_key_import_architecture(parser: argparse.ArgumentParser) -> None: """ - check command must correctly parse architecture list + key-import command must correctly parse architecture list """ args = parser.parse_args(["-a", "x86_64", "key-import", "key"]) assert args.architecture == [""] +def test_subparsers_patch_add(parser: argparse.ArgumentParser) -> None: + """ + patch-add command must imply action, architecture list, lock and no-report + """ + args = parser.parse_args(["patch-add", "ahriman"]) + assert args.action == Action.Update + assert args.architecture == [""] + assert args.lock is None + assert args.no_report + + +def test_subparsers_patch_add_architecture(parser: argparse.ArgumentParser) -> None: + """ + patch-add command must correctly parse architecture list + """ + args = parser.parse_args(["-a", "x86_64", "patch-add", "ahriman"]) + assert args.architecture == [""] + + +def test_subparsers_patch_add_track(parser: argparse.ArgumentParser) -> None: + """ + patch-add command must correctly parse track files patterns + """ + args = parser.parse_args(["patch-add", "-t", "*.py", "ahriman"]) + assert args.track == ["*.diff", "*.patch", "*.py"] + + +def test_subparsers_patch_list(parser: argparse.ArgumentParser) -> None: + """ + patch-list command must imply action, architecture list, lock and no-report + """ + args = parser.parse_args(["patch-list", "ahriman"]) + assert args.action == Action.List + assert args.architecture == [""] + assert args.lock is None + assert args.no_report + + +def test_subparsers_patch_list_architecture(parser: argparse.ArgumentParser) -> None: + """ + patch-list command must correctly parse architecture list + """ + args = parser.parse_args(["-a", "x86_64", "patch-list", "ahriman"]) + assert args.architecture == [""] + + +def test_subparsers_patch_remove(parser: argparse.ArgumentParser) -> None: + """ + patch-remove command must imply action, architecture list, lock and no-report + """ + args = parser.parse_args(["patch-remove", "ahriman"]) + assert args.action == Action.Remove + assert args.architecture == [""] + assert args.lock is None + assert args.no_report + + +def test_subparsers_patch_remove_architecture(parser: argparse.ArgumentParser) -> None: + """ + patch-remove command must correctly parse architecture list + """ + args = parser.parse_args(["-a", "x86_64", "patch-remove", "ahriman"]) + assert args.architecture == [""] + + def test_subparsers_rebuild_architecture(parser: argparse.ArgumentParser) -> None: """ rebuild command must correctly parse architecture list diff --git a/tests/ahriman/core/build_tools/test_sources.py b/tests/ahriman/core/build_tools/test_sources.py index cad0716d..084acbad 100644 --- a/tests/ahriman/core/build_tools/test_sources.py +++ b/tests/ahriman/core/build_tools/test_sources.py @@ -7,6 +7,34 @@ from unittest import mock from ahriman.core.build_tools.sources import Sources +def test_add(mocker: MockerFixture) -> None: + """ + must add files to git + """ + glob_mock = mocker.patch("pathlib.Path.glob", return_value=[Path("local/1"), Path("local/2")]) + check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output") + + local = Path("local") + Sources.add(local, "pattern1", "pattern2") + glob_mock.assert_has_calls([mock.call("pattern1"), mock.call("pattern2")]) + check_output_mock.assert_called_with( + "git", "add", "--intent-to-add", "1", "2", "1", "2", + exception=None, cwd=local, logger=pytest.helpers.anyvar(int)) + + +def test_diff(mocker: MockerFixture) -> None: + """ + must calculate diff + """ + write_mock = mocker.patch("pathlib.Path.write_text") + check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output") + + local = Path("local") + Sources.diff(local, Path("patch")) + write_mock.assert_called_once() + check_output_mock.assert_called_with("git", "diff", exception=None, cwd=local, logger=pytest.helpers.anyvar(int)) + + def test_fetch_existing(mocker: MockerFixture) -> None: """ must fetch new package via clone command @@ -17,15 +45,10 @@ def test_fetch_existing(mocker: MockerFixture) -> None: local = Path("local") Sources.fetch(local, "remote", "master") check_output_mock.assert_has_calls([ - mock.call("git", "fetch", "origin", "master", - exception=pytest.helpers.anyvar(int), - cwd=local, logger=pytest.helpers.anyvar(int)), - mock.call("git", "checkout", "--force", "master", - exception=pytest.helpers.anyvar(int), - cwd=local, logger=pytest.helpers.anyvar(int)), + mock.call("git", "fetch", "origin", "master", exception=None, cwd=local, logger=pytest.helpers.anyvar(int)), + mock.call("git", "checkout", "--force", "master", exception=None, cwd=local, logger=pytest.helpers.anyvar(int)), mock.call("git", "reset", "--hard", "origin/master", - exception=pytest.helpers.anyvar(int), - cwd=local, logger=pytest.helpers.anyvar(int)) + exception=None, cwd=local, logger=pytest.helpers.anyvar(int)) ]) @@ -39,15 +62,10 @@ def test_fetch_new(mocker: MockerFixture) -> None: local = Path("local") Sources.fetch(local, "remote", "master") check_output_mock.assert_has_calls([ - mock.call("git", "clone", "remote", str(local), - exception=pytest.helpers.anyvar(int), - logger=pytest.helpers.anyvar(int)), - mock.call("git", "checkout", "--force", "master", - exception=pytest.helpers.anyvar(int), - cwd=local, logger=pytest.helpers.anyvar(int)), + mock.call("git", "clone", "remote", str(local), exception=None, logger=pytest.helpers.anyvar(int)), + mock.call("git", "checkout", "--force", "master", exception=None, cwd=local, logger=pytest.helpers.anyvar(int)), mock.call("git", "reset", "--hard", "origin/master", - exception=pytest.helpers.anyvar(int), - cwd=local, logger=pytest.helpers.anyvar(int)) + exception=None, cwd=local, logger=pytest.helpers.anyvar(int)) ]) @@ -56,14 +74,14 @@ def test_load(mocker: MockerFixture) -> None: must load packages sources correctly """ fetch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.fetch") - patch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.patch") + patch_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.patch_apply") Sources.load(Path("local"), "remote", Path("patches")) fetch_mock.assert_called_with(Path("local"), "remote") patch_mock.assert_called_with(Path("local"), Path("patches")) -def test_patches(mocker: MockerFixture) -> None: +def test_patch_apply(mocker: MockerFixture) -> None: """ must apply patches if any """ @@ -72,30 +90,28 @@ def test_patches(mocker: MockerFixture) -> None: check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output") local = Path("local") - Sources.patch(local, Path("patches")) + Sources.patch_apply(local, Path("patches")) glob_mock.assert_called_once() check_output_mock.assert_has_calls([ mock.call("git", "apply", "--ignore-space-change", "--ignore-whitespace", "01.patch", - exception=pytest.helpers.anyvar(int), - cwd=local, logger=pytest.helpers.anyvar(int)), + exception=None, cwd=local, logger=pytest.helpers.anyvar(int)), mock.call("git", "apply", "--ignore-space-change", "--ignore-whitespace", "02.patch", - exception=pytest.helpers.anyvar(int), - cwd=local, logger=pytest.helpers.anyvar(int)), + exception=None, cwd=local, logger=pytest.helpers.anyvar(int)), ]) -def test_patches_no_dir(mocker: MockerFixture) -> None: +def test_patch_apply_no_dir(mocker: MockerFixture) -> None: """ must not fail if no patches directory exists """ mocker.patch("pathlib.Path.is_dir", return_value=False) glob_mock = mocker.patch("pathlib.Path.glob") - Sources.patch(Path("local"), Path("patches")) + Sources.patch_apply(Path("local"), Path("patches")) glob_mock.assert_not_called() -def test_patches_no_patches(mocker: MockerFixture) -> None: +def test_patch_apply_no_patches(mocker: MockerFixture) -> None: """ must not fail if no patches exist """ @@ -103,5 +119,17 @@ def test_patches_no_patches(mocker: MockerFixture) -> None: mocker.patch("pathlib.Path.glob", return_value=[]) check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output") - Sources.patch(Path("local"), Path("patches")) + Sources.patch_apply(Path("local"), Path("patches")) check_output_mock.assert_not_called() + + +def test_patch_create(mocker: MockerFixture) -> None: + """ + must create patch set for the package + """ + add_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.add") + diff_mock = mocker.patch("ahriman.core.build_tools.sources.Sources.diff") + + Sources.patch_create(Path("local"), Path("patch"), "glob") + add_mock.assert_called_with(Path("local"), "glob") + diff_mock.assert_called_with(Path("local"), Path("patch"))