patch control subcommands

This commit is contained in:
2021-10-05 08:57:42 +03:00
parent 9f99dd3ff2
commit 3b6b2efcb1
9 changed files with 516 additions and 70 deletions

View File

@ -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

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
#
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)

View File

@ -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)

View File

@ -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 <http://www.gnu.org/licenses/>.
#
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"