Compare commits

..
52 Commits
Author SHA1 Message Date
arcanis 2b3b0558ea Release 2.24.0 2026-11-30 23:35:02 +02:00
arcanis e1565398f4 fix: separate quoted and unquoted string parsing
there is anoter error caused by inner shell expression, which fails
shlex parsing
2026-11-30 23:21:50 +02:00
arcanis 1fcb4cdb84 feat: add events rotation trigger to remove old events 2026-11-26 18:12:27 +02:00
arcanis c70640c492 feat: add enum support in filtering 2026-11-25 15:21:52 +02:00
arcanis aff4bb70a9 feat: add events amount selector 2026-11-25 15:05:59 +02:00
arcanis afc2e749b9 feat: add support of download button and make build logs link permanent 2026-11-25 14:58:00 +02:00
arcanis 941a51d496 test: fix tests for oauth client update 2026-11-24 10:04:07 +02:00
arcanis 11787f740e type: fix types for Queue, related to docs generation 2026-11-24 09:57:31 +02:00
arcanis 239149f2b4 refactor: use itertools instead of sum for combining iterables 2026-11-20 12:06:56 +02:00
arcanis b28c156e78 type: remove annotations import, because they are no longer needed in
python 3.14+
2026-11-20 11:37:29 +02:00
arcanis a428b62993 docs: fix types in docstrings 2026-11-19 15:50:36 +02:00
arcanis 0bd22a049c type: add type annotation pylint check
Also run pylint against tests tree
2026-11-18 14:30:17 +02:00
arcanis 71c5d58264 fix: remove packages with broken dependencies from build plan and mark
them as failed
2026-11-18 11:20:43 +02:00
arcanis 22c763ed52 build: update frontend dependencies 2026-11-18 10:36:39 +02:00
arcanis 36b7972b26 style: add more pylint checks and update code for them 2026-11-18 10:36:19 +02:00
arcanis 4f839a7e6a feat: simplify buildplan api and keep original reasons 2026-11-18 10:35:46 +02:00
arcanis a10b1a9364 fix: keep discovered subpackages in place 2026-11-16 19:51:53 +02:00
arcanis f0d523492d feat: implement detailed build plan 2026-11-15 03:25:01 +02:00
arcanis dc4f2be65b build: update regress job to actually check that there are not updates 2026-11-06 14:01:36 +02:00
arcanis 9ba74413e3 feat: add module entrypoint
small useless feature, but the application can now be run with python -m
ahriman
2026-11-06 13:40:47 +02:00
arcanis d3b569bfa2 Release 2.23.6 2026-10-30 16:33:14 +02:00
arcanis 64a7e9b288 docs: take note on parametrized tests 2026-10-30 16:17:07 +02:00
arcanis 5ad8a2a7d0 fix: improve parsing of escaped symbols inside string 2026-10-30 16:16:50 +02:00
arcanis c56a319267 fix: handle array expansion without regex 2026-10-30 13:25:39 +02:00
arcanis 96558886bd fix: handle array append correctly 2026-10-29 14:27:14 +02:00
arcanis 4a745321ec docs: update contribution guide with ai policy 2026-10-26 13:05:18 +02:00
arcanis 4446c950ec Release 2.23.5 2026-10-25 02:17:21 +03:00
arcanis 8d569e6075 fix: pylint check 2026-10-25 02:16:59 +03:00
arcanis c375236327 Release 2.23.4 2026-10-25 02:12:43 +03:00
arcanis ddbb4b06ff fix: commit forgotten test file 2026-10-25 02:12:24 +03:00
arcanis 2d66b67123 Release 2.23.3 2026-10-25 02:09:35 +03:00
arcanis 27cfcd07eb fix: handle empty assignment correctly in PKGBUILD 2026-10-25 02:09:13 +03:00
arcanis 2080f1e06f Release 2.23.2 2026-10-25 01:45:45 +03:00
arcanis 2544d44051 fix: refresh databases on repo-update command 2026-10-24 16:48:23 +03:00
arcanis b07ad8cc0c build: clear pacman cache to resolve hashsums mismatch 2026-10-24 12:37:10 +03:00
arcanis c38cb5f680 feat: restore iterable method in parser 2026-10-24 12:20:29 +03:00
arcanis f776b33310 feat: preserve links to packages
This changes url handling in status page. Firstly, instead of using
hash for repository, now they are handled by query parameter. Secondly,
it adds a query parameter for package base. And lastly, it changes
behaviour for clicking on the package link, now it opens status page,
which allows to e.g. open a tab in background
2026-10-22 11:33:06 +03:00
arcanis eafa1b417a build: replace aiohttp-sse-git with aiohttp-sse
The upstream has released new version, which includes all required fixes
for the newest aiohttp
2026-10-20 16:23:56 +03:00
arcanis d0aff05be5 Release 2.23.1 2026-10-20 00:31:19 +03:00
arcanis 8a6b71a425 docs: commit docs requirements.txt 2026-10-20 00:30:57 +03:00
arcanis 0e7e2a9128 Release 2.23.0 2026-10-20 00:28:44 +03:00
arcanis bb1fb86036 fix: fix docker build with new dependencies 2026-10-20 00:00:40 +03:00
arcanis 6e7651a176 fix: handle version subcommand correctly
After package split it was looking for metapackage and lookup was broken
because it doesn't contain any python metadata
2026-10-19 23:26:23 +03:00
arcanis 9e5468313e feat: migrate pkgbuild parser to tree-sitter (#168) 2026-10-19 23:10:58 +03:00
arcanis 9b65e01d2f feat: show notification on hold action 2026-10-19 10:22:42 +03:00
arcanis 7f28e47ac4 fix: add files to git with force flag
It has been reported that AUR repositories might contain .gitignore,
which prevents PKGBUILD/.SRCINFO/whatever to be added without --force
flag. This commit simply adds force flag for all those operations, which
should be fine, because we don't commit wildcards

Closes #167
2026-10-17 12:17:40 +03:00
arcanis 0b371b3e77 feat: add tooltip for held packages 2026-10-16 13:09:17 +03:00
arcanis ef47c6fa72 feat: distinguish between repo update and package update in ui 2026-10-16 13:03:48 +03:00
arcanis 81ea175034 bug: always keep existing package version in archive 2026-10-16 12:11:51 +03:00
arcanis b73be7abcc feat: only remove packages from build queue which were actually built 2026-10-16 11:49:52 +03:00
arcanis 4537969d5d fix: handle is_held flag in all update processes
Previos implementation has ignored this flag in dependencies checks,
local package update and so on
2026-10-14 13:44:37 +03:00
arcanis 47c39f93b7 feat: align tables vertically in center 2026-10-13 13:22:00 +03:00
327 changed files with 4959 additions and 3387 deletions

No files matched your search

+6 -5
View File
@@ -10,15 +10,15 @@ echo -e '[arcanisrepo]\nServer = https://repo.arcanis.me/$arch\nSigLevel = Never
# refresh the image
pacman -Syyu --noconfirm
# main dependencies
pacman -S --noconfirm devtools git npm pyalpm python-bcrypt python-filelock python-inflection python-pyelftools python-requests python-systemd sudo
pacman -S --noconfirm --asdeps devtools git pyalpm python-bcrypt python-filelock python-inflection python-pyelftools python-requests python-tree-sitter python-tree-sitter-bash
# make dependencies
pacman -S --noconfirm --asdeps base-devel python-build python-hatchling python-installer python-tox python-wheel
pacman -S --noconfirm --asdeps base-devel npm python-build python-hatchling python-installer python-tox python-wheel
# optional dependencies
if [[ -z $MINIMAL_INSTALL ]]; then
# web server
pacman -S --noconfirm python-aioauth-client python-aiohttp python-aiohttp-apispec-git python-aiohttp-cors python-aiohttp-jinja2 python-aiohttp-security python-aiohttp-session python-aiohttp-sse-git python-cryptography python-jinja
pacman -S --noconfirm --asdeps python-aioauth-client python-aiohttp python-aiohttp-apispec-git python-aiohttp-cors python-aiohttp-jinja2 python-aiohttp-security python-aiohttp-session python-aiohttp-sse python-cryptography python-jinja
# additional features
pacman -S --noconfirm gnupg ipython python-boto3 python-cerberus python-matplotlib rsync
pacman -S --noconfirm --asdeps gnupg ipython python-boto3 python-cerberus python-matplotlib python-systemd rsync
fi
# FIXME since 1.0.4 devtools requires dbus to be run, which doesn't work now in container
cp "docker/systemd-nspawn.sh" "/usr/local/bin/systemd-nspawn"
@@ -41,6 +41,7 @@ systemd-machine-id-setup
# remove unused dependencies
pacman -Qdtq | pacman -Rscn --noconfirm -
find "/var/cache/pacman/pkg" -type f -delete
# initial setup command as root
[[ -z $MINIMAL_INSTALL ]] && WEB_ARGS=("--web-port" "8080")
@@ -60,6 +61,6 @@ sudo -u ahriman -- ahriman --log-handler console package-add --now ahriman
# check if package was actually installed
test -n "$(find "/var/lib/ahriman/repository/github/x86_64" -name "ahriman*pkg*")"
# run package check
sudo -u ahriman -- ahriman repo-update
sudo -u ahriman -- ahriman --log-handler console repo-update --refresh
# stop web service lol
[[ -z $WEB_PID ]] || kill $WEB_PID
+2 -2
View File
@@ -103,11 +103,11 @@ jobs:
- name: Update packages
run: |
sudo -u ahriman ahriman \
! sudo -u ahriman ahriman \
--log-handler "$AHRIMAN_OUTPUT" \
repo-update \
--exit-code \
|| true
--refresh
- name: Add patch
run: |
+21 -23
View File
@@ -1,10 +1,14 @@
[tool.pylint.main]
init-hook = "sys.path.append('tools')"
load-plugins = [
"pylint.extensions.docparams",
"pylint.extensions.bad_builtin",
"pylint.extensions.check_elif",
"pylint.extensions.docparams",
"pylint.extensions.overlapping_exceptions",
"pylint.extensions.typing",
"pylint_plugins.definition_order",
"pylint_plugins.import_order",
"pylint_plugins.type_annotations",
]
[tool.pylint.classes]
@@ -17,29 +21,23 @@ max-parents = 15
[tool.pylint."messages control"]
disable = [
"raw-checker-failed",
"bad-inline-option",
"locally-disabled",
"file-ignored",
"suppressed-message",
"useless-suppression",
"deprecated-pragma",
"use-symbolic-message-instead",
"use-implicit-booleaness-not-comparison-to-string",
"use-implicit-booleaness-not-comparison-to-zero",
"missing-module-docstring",
"line-too-long",
"no-name-in-module",
"import-outside-toplevel",
"invalid-name",
"raise-missing-from",
"wrong-import-order",
"too-few-public-methods",
"too-many-instance-attributes",
"broad-exception-caught",
"fixme",
"too-many-arguments",
"duplicate-code",
"cyclic-import",
"duplicate-code",
"fixme",
"import-outside-toplevel",
"line-too-long",
"missing-module-docstring",
"no-name-in-module",
"raise-missing-from",
"raw-checker-failed",
"suppressed-message",
"too-few-public-methods",
"too-many-arguments",
"too-many-instance-attributes",
"too-many-positional-arguments",
"wrong-import-order",
]
[tool.pylint.string]
check-quote-consistency = true
+12 -3
View File
@@ -147,9 +147,6 @@ Again, the most checks can be performed by `tox` command, though some additional
* Import order must be the following:
```python
# optional imports from future module
from __future__ import annotations
# Module import for those which are installed into environment (no matter standard library or not)...
import os
# ...one per line...
@@ -222,6 +219,7 @@ Again, the most checks can be performed by `tox` command, though some additional
* On the other hand, it is allowed to change method signatures, however, it is recommended to add new parameters as optional if possible. Deprecated API can be dropped during major release.
* Enumerations (`Enum` classes) are allowed and recommended. However, it is recommended to use `StrEnum` class if there are from/to string conversions and `IntEnum` otherwise.
* `Generator` return type is not allowed. Generator functions must return generic `Iterator` object. Documentation should be described as `Yields`, however, because of pylint checks. Unfortunately, `Iterable` return type is not available for generators also, because of specific `contextlib.contextmanager` case.
* Tests must not be parametrized. It is better to write separate test, which covers specific line of code rather than testing all over again.
### Other checks
@@ -229,6 +227,17 @@ The projects also uses typing checks (provided by `mypy`) and some linter checks
Frontend checks normally are performed by `eslint` (e.g. `npx run eslint`).
## AI usage policy
AI tools may be used when working on the project, including writing code, tests, and documentation, debugging problems, and preparing issue reports. Contributors remain responsible for everything they submit, including content generated or suggested by AI.
* Review and understand every submitted change. You must be able to explain the implementation, respond to review feedback, and correct any problems.
* Do not list AI tools as commit authors or co-authors, including in `Co-authored-by` trailers. Authorship must remain with the human contributors responsible for the changes.
* Discussions in issues and pull requests must be led by humans. Participate personally, explain your reasoning, and respond to questions and review feedback yourself. AI tools may help with wording or translation, but must not conduct discussions on your behalf.
* Verify claims, references, and reproduction steps before submitting an issue or pull request. Do not submit unreviewed AI output or claim that checks passed unless they were run and you verified the results.
Maintainers may close submissions that do not meet these requirements. Using AI does not transfer responsibility for reviewing or fixing a contribution to maintainers.
## Developers how to
### Run automated checks
@@ -60,12 +60,14 @@ triggers[] = ahriman.core.gitremote.RemotePullTrigger
triggers[] = ahriman.core.report.ReportTrigger
triggers[] = ahriman.core.upload.UploadTrigger
triggers[] = ahriman.core.gitremote.RemotePushTrigger
triggers[] = ahriman.core.housekeeping.EventsRotationTrigger
triggers[] = ahriman.core.housekeeping.LogsRotationTrigger
triggers[] = ahriman.core.housekeeping.ArchiveRotationTrigger
; List of well-known triggers. Used only for configuration purposes.
triggers_known[] = ahriman.core.gitremote.RemotePullTrigger
triggers_known[] = ahriman.core.gitremote.RemotePushTrigger
triggers_known[] = ahriman.core.housekeeping.ArchiveRotationTrigger
triggers_known[] = ahriman.core.housekeeping.EventsRotationTrigger
triggers_known[] = ahriman.core.housekeeping.LogsRotationTrigger
triggers_known[] = ahriman.core.report.ReportTrigger
triggers_known[] = ahriman.core.upload.UploadTrigger
@@ -2,6 +2,11 @@
; Keep amount of last built packages in archive. 0 means keep all packages
keep_built_packages = 1
[events-rotation]
; Keep the most recent audit events for each object. 0 removes all events.
keep_last_events = 1000
[logs-rotation]
; Keep last build logs for each package
keep_last_logs = 5
@@ -747,7 +747,12 @@ _shtab_ahriman() {
compgen -W "${current_action_choices[*]}" -- "$completing_word")
fi
if [[ ${#COMPREPLY[@]} = 0 ]]; then
# native fallback matches are not necessarily filenames (e.g. variables)
compopt +o filenames 2>/dev/null || : # bash>=4
fi
return 0
}
complete -F _shtab_ahriman ahriman
complete -o bashdefault -F _shtab_ahriman ahriman
@@ -1,4 +1,4 @@
.TH AHRIMAN "1" "2026\-10\-13" "ahriman 2.22.2" "ArcH linux ReposItory MANager"
.TH AHRIMAN "1" "2026\-11\-30" "ahriman 2.24.0" "ArcH linux ReposItory MANager"
.SH NAME
ahriman \- ArcH linux ReposItory MANager
.SH SYNOPSIS
@@ -1095,7 +1095,17 @@ typeset -A opt_args
if [[ $zsh_eval_context[-1] == eval ]]; then
# eval/source/. command, register function for later
compdef _shtab_ahriman -N ahriman
_shtab_ahriman_register() {
# `compdef` is defined by `compinit`
if (( $+functions[compdef] )); then
compdef _shtab_ahriman -N ahriman
# drop registration hook (avoid multiple `compdef` calls)
add-zsh-hook -d precmd _shtab_ahriman_register
fi
}
# register completer before next prompt
autoload -Uz add-zsh-hook
add-zsh-hook precmd _shtab_ahriman_register
else
# autoload from fpath, call function directly
_shtab_ahriman "$@"
+2
View File
@@ -12,6 +12,8 @@ dependencies = [
"inflection",
"pyelftools",
"requests",
"tree-sitter",
"tree-sitter-bash",
]
description = "ArcH linux ReposItory MANager, core package"
dynamic = [
+1 -1
View File
@@ -17,4 +17,4 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
__version__ = "2.22.2"
__version__ = "2.24.0"
+26
View File
@@ -0,0 +1,26 @@
#
# 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/>.
#
import sys
from ahriman.application.ahriman import run
if __name__ == "__main__":
sys.exit(run())
@@ -22,7 +22,7 @@ from collections.abc import Callable, Iterable
from ahriman.application.application.application_packages import ApplicationPackages
from ahriman.application.application.application_repository import ApplicationRepository
from ahriman.core.formatters import UpdatePrinter
from ahriman.core.tree import Tree
from ahriman.models.build_plan import BuildPlan, BuildPlanReason
from ahriman.models.package import Package
from ahriman.models.result import Result
@@ -45,7 +45,7 @@ class Application(ApplicationPackages, ApplicationRepository):
>>> application.add(["ahriman"], PackageSource.AUR)
>>>
>>> # check for updates
>>> updates = application.updates([], aur=True, local=True, manual=True, vcs=True)
>>> updates = application.updates([], aur=True, local=True, manual=True, vcs=True, check_files=False)
>>> # updates for specified packages
>>> application.update(updates)
@@ -95,43 +95,39 @@ class Application(ApplicationPackages, ApplicationRepository):
"""
self.repository.triggers.on_stop()
def print_updates(self, packages: list[Package], *, log_fn: Callable[[str], None]) -> None:
def print_updates(self, plan: BuildPlan, log_fn: Callable[[str], None], *, bump_pkgrel: bool = True) -> None:
"""
print list of packages to be built. This method will build dependency tree and print updates accordingly
print packages and their reasons, grouped by dependency step
Args:
packages(list[Package]): package list to be printed
plan(BuildPlan): packages and selection reasons to be printed
log_fn(Callable[[str], None]): logger function to log updates
bump_pkgrel(bool, optional): whether duplicate releases are expected to increment
"""
local_versions = {package.base: package.version for package in self.repository.packages()}
UpdatePrinter(plan, local_versions, bump_pkgrel=bump_pkgrel)(verbose=True, log_fn=log_fn)
tree = Tree.resolve(packages)
for level in tree:
for package in level:
UpdatePrinter(package, local_versions.get(package.base))(verbose=True, log_fn=log_fn, separator=" -> ")
def with_dependencies(self, packages: list[Package], *, process_dependencies: bool) -> list[Package]:
def with_dependencies(self, plan: BuildPlan, *, process_dependencies: bool) -> BuildPlan:
"""
add missing dependencies to list of packages. This will extract known packages, check dependencies of
the supplied packages and add packages which are not presented in the list of known packages.
add missing dependencies to a build plan and record which packages require them
Args:
packages(list[Package]): list of source packages of which dependencies have to be processed
plan(BuildPlan): selected packages and their reasons
process_dependencies(bool): if set to ``False``, dependencies will not be processed
Returns:
list[Package]: updated packages list. Packager for dependencies will be copied from the original package
BuildPlan: selected packages and their dependencies
Examples:
In the most cases, in order to avoid build failure, it is required to add missing packages, which can be
done by calling::
>>> application = ...
>>> packages = application.with_dependencies(packages, process_dependencies=True)
>>> application.print_updates(packages, log_fn=print)
>>> plan = application.with_dependencies(plan, process_dependencies=True)
>>> application.update(plan)
"""
if not process_dependencies or not packages:
return packages
if not process_dependencies or not plan:
return plan
def missing_dependencies(sources: Iterable[Package]) -> dict[str, str | None]:
# append list of known packages with packages which are in current sources
@@ -157,20 +153,30 @@ class Application(ApplicationPackages, ApplicationRepository):
leaf = Package.from_build(source_dir, self.repository.repository_id.architecture, packager)
else:
leaf = Package.from_aur(package_name, packager, include_provides=True)
portion[leaf.base] = leaf
# for split packages we need to load all available subpackages here
if (existing := portion.get(leaf.base)) is not None:
leaf.packages.update(existing.packages)
portion[leaf.base] = leaf
# register package in the database
self.reporter.set_unknown(leaf)
return portion
known_packages = self._known_packages()
with_dependencies: dict[str, Package] = {}
for package in packages:
for package in plan:
with self.in_package_context(package.base, package.version): # use the same context for the logger
try:
with_dependencies |= new_packages(package)
packages = new_packages(package)
except Exception:
self.logger.exception("could not process dependencies of %s, skip the package", package.base)
plan.remove(package.base)
self.reporter.set_failed(package.base)
continue
return list(with_dependencies.values())
# update existing package
plan.add(packages.pop(package.base))
# append dependencies
plan.update(packages.values(), BuildPlanReason.REQUIRED_BY, package.base)
return plan
@@ -38,14 +38,14 @@ class ApplicationProperties(LazyLogging):
"""
def __init__(self, repository_id: RepositoryId, configuration: Configuration, *, report: bool,
refresh_pacman_database: PacmanSynchronization = PacmanSynchronization.Disabled) -> None:
refresh_pacman_database: PacmanSynchronization = PacmanSynchronization.DISABLED) -> None:
"""
Args:
repository_id(RepositoryId): repository unique identifier
configuration(Configuration): configuration instance
report(bool): force enable or disable reporting
refresh_pacman_database(PacmanSynchronization, optional): pacman database synchronization level
(Default value = PacmanSynchronization.Disabled)
(Default value = PacmanSynchronization.DISABLED)
"""
self.configuration = configuration
self.repository_id = repository_id
@@ -23,6 +23,7 @@ from ahriman.application.application.application_properties import ApplicationPr
from ahriman.application.application.workers import Updater
from ahriman.core.build_tools.sources import Sources
from ahriman.core.exceptions import UnknownPackageError
from ahriman.models.build_plan import BuildPlan, BuildPlanReason
from ahriman.models.package import Package
from ahriman.models.packagers import Packagers
from ahriman.models.result import Result
@@ -158,6 +159,8 @@ class ApplicationRepository(ApplicationProperties):
# process already built packages if any
if built_packages := self.repository.packages_built(): # speedup a bit
build_result = self.repository.process_update(built_packages, packagers)
for package in build_result.success:
self.database.build_queue_clear(package.base)
self.on_result(build_result)
result.merge(build_result)
@@ -172,13 +175,15 @@ class ApplicationRepository(ApplicationProperties):
for num, partition in enumerate(partitions):
self.logger.info("processing chunk #%i %s", num, [package.base for package in partition])
build_result = builder.update(partition, packagers, bump_pkgrel=bump_pkgrel)
for package in partition:
self.database.build_queue_clear(package.base)
self.on_result(build_result)
result.merge(build_result)
return result
def updates(self, filter_packages: Iterable[str], *,
aur: bool, local: bool, manual: bool, vcs: bool, check_files: bool) -> list[Package]:
aur: bool, local: bool, manual: bool, vcs: bool, check_files: bool) -> BuildPlan:
"""
get list of packages to run update process
@@ -189,25 +194,24 @@ class ApplicationRepository(ApplicationProperties):
manual(bool): include or exclude manual updates
vcs(bool): enable or disable checking of VCS packages
check_files(bool): check for broken dependencies
Returns:
list[Package]: list of out-of-dated packages
BuildPlan: selected packages and their reasons
"""
updates = {}
plan = BuildPlan()
# always add already built packages, because they will be always added
updates.update({
package.base: package
for package in self.repository.load_archives(self.repository.packages_built())
})
plan.update(self.repository.load_archives(self.repository.packages_built()), BuildPlanReason.PRE_BUILT)
if aur:
updates.update({package.base: package for package in self.repository.updates_aur(filter_packages, vcs=vcs)})
if local:
updates.update({package.base: package for package in self.repository.updates_local(vcs=vcs)})
if manual:
updates.update({package.base: package for package in self.repository.updates_manual()})
plan.update(self.repository.updates_manual(), BuildPlanReason.MANUAL_REQUEST)
if aur:
plan.update(self.repository.updates_aur(filter_packages, vcs=vcs), BuildPlanReason.REGULAR_UPDATE)
if local:
plan.update(self.repository.updates_local(vcs=vcs), BuildPlanReason.LOCAL_SOURCE_UPDATE)
if check_files:
updates.update({package.base: package for package in self.repository.updates_dependencies(filter_packages)})
plan.update(
self.repository.updates_dependencies(filter_packages),
BuildPlanReason.BROKEN_IMPLICIT_DEPENDENCY,
)
return [package for _, package in sorted(updates.items())]
return plan
@@ -41,7 +41,7 @@ class UpdatesIterator(Iterator[list[str] | None]):
Typical usage of this class is something like:
>>> application = ...
>>> iterator = UpdatesIterator(application, None)
>>> iterator = UpdatesIterator(application, 3600)
>>>
>>> for updates in iterator:
>>> print(updates)
@@ -17,8 +17,6 @@
# 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 __future__ import annotations
from collections.abc import Iterable
from ahriman.core.configuration import Configuration
@@ -85,7 +85,7 @@ class Add(Handler):
action=argparse.BooleanOptionalAction, default=True)
parser.add_argument("-n", "--now", help="run update function after", action="store_true")
parser.add_argument("-s", "--source", help="explicitly specify the package source for this command",
type=PackageSource, choices=tuple(PackageSource), default=PackageSource.Auto)
type=PackageSource, choices=tuple(PackageSource), default=PackageSource.AUTO)
parser.add_argument("-u", "--username", help="build as user", default=extract_user())
parser.add_argument("-v", "--variable", help="apply specified makepkg variables to the next build",
action="append")
@@ -50,10 +50,10 @@ class Archives(Handler):
application = Application(repository_id, configuration, report=True)
match args.action:
case Action.List:
case Action.LIST:
archives = application.repository.package_archives(args.package)
for package in archives:
PackagePrinter(package, BuildStatus(BuildStatusEnum.Success))(verbose=args.info)
PackagePrinter(package, BuildStatus(BuildStatusEnum.SUCCESS))(verbose=args.info)
Archives.check_status(args.exit_code, bool(archives))
@@ -75,7 +75,7 @@ class Archives(Handler):
action="store_true")
parser.add_argument("--info", help="show additional package information",
action=argparse.BooleanOptionalAction, default=False)
parser.set_defaults(action=Action.List, lock=None, quiet=True, report=False, unsafe=True)
parser.set_defaults(action=Action.LIST, lock=None, quiet=True, report=False, unsafe=True)
return parser
arguments = [_set_package_archives_parser]
@@ -50,11 +50,11 @@ class Change(Handler):
client = Application(repository_id, configuration, report=True).reporter
match args.action:
case Action.List:
case Action.LIST:
changes = client.package_changes_get(args.package)
ChangesPrinter(changes)(verbose=True, separator="")
Change.check_status(args.exit_code, changes.changes is not None)
case Action.Remove:
case Action.REMOVE:
client.package_changes_update(args.package, Changes())
@staticmethod
@@ -75,7 +75,7 @@ class Change(Handler):
parser.add_argument("package", help="package base")
parser.add_argument("-e", "--exit-code", help="return non-zero exit status if result is empty",
action="store_true")
parser.set_defaults(action=Action.List, lock=None, quiet=True, report=False, unsafe=True)
parser.set_defaults(action=Action.LIST, lock=None, quiet=True, report=False, unsafe=True)
return parser
@staticmethod
@@ -92,7 +92,7 @@ class Change(Handler):
parser = root.add_parser("package-changes-remove", help="remove package changes",
description="remove the package changes stored remotely")
parser.add_argument("package", help="package base")
parser.set_defaults(action=Action.Remove, exit_code=False, lock=None, quiet=True, report=False, unsafe=True)
parser.set_defaults(action=Action.REMOVE, exit_code=False, lock=None, quiet=True, report=False, unsafe=True)
return parser
arguments = [_set_package_changes_parser, _set_package_changes_remove_parser]
@@ -103,7 +103,7 @@ class Copy(Handler):
for source in package.packages.values()
if source.filename is not None
]
application.add(source_paths, PackageSource.Archive)
application.add(source_paths, PackageSource.ARCHIVE)
# copy metadata
application.reporter.package_changes_update(
@@ -112,6 +112,6 @@ class Copy(Handler):
application.reporter.package_dependencies_update(
package.base, source_application.reporter.package_dependencies_get(package.base)
)
application.reporter.package_update(package, BuildStatusEnum.Pending)
application.reporter.package_update(package, BuildStatusEnum.PENDING)
arguments = [_set_package_copy_parser]
@@ -46,10 +46,10 @@ class Hold(Handler):
client = Application(repository_id, configuration, report=True).reporter
match args.action:
case Action.Remove:
case Action.REMOVE:
for package in args.package:
client.package_hold_update(package, enabled=False)
case Action.Update:
case Action.UPDATE:
for package in args.package:
client.package_hold_update(package, enabled=True)
@@ -67,7 +67,7 @@ class Hold(Handler):
parser = root.add_parser("package-hold", help="hold package",
description="hold package from automatic updates")
parser.add_argument("package", help="package base", nargs="+")
parser.set_defaults(action=Action.Update, lock=None, quiet=True, report=False, unsafe=True)
parser.set_defaults(action=Action.UPDATE, lock=None, quiet=True, report=False, unsafe=True)
return parser
@staticmethod
@@ -84,7 +84,7 @@ class Hold(Handler):
parser = root.add_parser("package-unhold", help="unhold package",
description="remove package hold, allowing automatic updates")
parser.add_argument("package", help="package base", nargs="+")
parser.set_defaults(action=Action.Remove, lock=None, quiet=True, report=False, unsafe=True)
parser.set_defaults(action=Action.REMOVE, lock=None, quiet=True, report=False, unsafe=True)
return parser
arguments = [
@@ -56,15 +56,15 @@ class Patch(Handler):
application.on_start()
match args.action:
case Action.Update if args.variable is not None:
case Action.UPDATE if args.variable is not None:
patch = Patch.patch_create_from_function(args.variable, args.patch)
Patch.patch_set_create(application, args.package, patch)
case Action.Update:
case Action.UPDATE:
package_base, patch = Patch.patch_create_from_diff(args.package, repository_id.architecture, args.track)
Patch.patch_set_create(application, package_base, patch)
case Action.List:
case Action.LIST:
Patch.patch_set_list(application, args.package, args.variable, args.exit_code)
case Action.Remove:
case Action.REMOVE:
Patch.patch_set_remove(application, args.package, args.variable)
@staticmethod
@@ -89,7 +89,7 @@ class Patch(Handler):
"it must end with ()")
parser.add_argument("patch", help="path to file which contains function or variable value. If not set, "
"the value will be read from stdin", type=Path, nargs="?")
parser.set_defaults(action=Action.Update, architecture="", exit_code=False, lock=None, report=False,
parser.set_defaults(action=Action.UPDATE, architecture="", exit_code=False, lock=None, report=False,
repository="")
return parser
@@ -111,7 +111,7 @@ class Patch(Handler):
action="store_true")
parser.add_argument("-v", "--variable", help="if set, show only patches for specified PKGBUILD variables",
action="append")
parser.set_defaults(action=Action.List, architecture="", lock=None, report=False, repository="", unsafe=True)
parser.set_defaults(action=Action.LIST, architecture="", lock=None, report=False, repository="", unsafe=True)
return parser
@staticmethod
@@ -132,7 +132,7 @@ class Patch(Handler):
"to remove only specified PKGBUILD variables. In case if not set, "
"it will remove all patches related to the package",
action="append")
parser.set_defaults(action=Action.Remove, architecture="", exit_code=False, lock=None, report=False,
parser.set_defaults(action=Action.REMOVE, architecture="", exit_code=False, lock=None, report=False,
repository="")
return parser
@@ -157,7 +157,7 @@ class Patch(Handler):
parser.add_argument("package", help="path to directory with changed files for patch addition/update", type=Path)
parser.add_argument("-t", "--track", help="files which has to be tracked", action="append",
default=["*.diff", "*.patch"])
parser.set_defaults(action=Action.Update, architecture="", exit_code=False, lock=None, report=False,
parser.set_defaults(action=Action.UPDATE, architecture="", exit_code=False, lock=None, report=False,
repository="", variable=None)
return parser
@@ -51,11 +51,11 @@ class Pkgbuild(Handler):
client = Application(repository_id, configuration, report=True).reporter
match args.action:
case Action.List:
case Action.LIST:
changes = client.package_changes_get(args.package)
PkgbuildPrinter(changes)(verbose=True, separator="")
Pkgbuild.check_status(args.exit_code, changes.pkgbuild is not None)
case Action.Remove:
case Action.REMOVE:
changes = client.package_changes_get(args.package)
client.package_changes_update(args.package, replace(changes, pkgbuild=None))
@@ -77,7 +77,7 @@ class Pkgbuild(Handler):
parser.add_argument("package", help="package base")
parser.add_argument("-e", "--exit-code", help="return non-zero exit status if result is empty",
action="store_true")
parser.set_defaults(action=Action.List, lock=None, quiet=True, report=False, unsafe=True)
parser.set_defaults(action=Action.LIST, lock=None, quiet=True, report=False, unsafe=True)
return parser
@staticmethod
@@ -94,7 +94,7 @@ class Pkgbuild(Handler):
parser = root.add_parser("package-pkgbuild-remove", help="remove package pkgbuild",
description="remove the package PKGBUILD stored remotely")
parser.add_argument("package", help="package base")
parser.set_defaults(action=Action.Remove, exit_code=False, lock=None, quiet=True, report=False, unsafe=True)
parser.set_defaults(action=Action.REMOVE, exit_code=False, lock=None, quiet=True, report=False, unsafe=True)
return parser
arguments = [_set_package_pkgbuild_parser, _set_package_pkgbuild_remove_parser]
@@ -21,8 +21,10 @@ import argparse
from ahriman.application.application import Application
from ahriman.application.handlers.handler import Handler, SubParserAction
from ahriman.application.handlers.update import Update
from ahriman.core.configuration import Configuration
from ahriman.core.utils import extract_user
from ahriman.models.build_plan import BuildPlan, BuildPlanReason
from ahriman.models.build_status import BuildStatusEnum
from ahriman.models.package import Package
from ahriman.models.packagers import Packagers
@@ -52,12 +54,14 @@ class Rebuild(Handler):
packages = Rebuild.extract_packages(application, args.status, from_database=args.from_database)
packages = application.repository.packages_depend_on(packages, args.depends_on)
Rebuild.check_status(args.exit_code, packages)
plan = BuildPlan().update(packages, BuildPlanReason.REBUILD)
application.print_updates(plan, Update.log_fn(application, dry_run=args.dry_run), bump_pkgrel=args.increment)
if args.dry_run:
application.print_updates(packages, log_fn=print)
Rebuild.check_status(args.exit_code, plan)
return
result = application.update(packages, Packagers(args.username), bump_pkgrel=args.increment)
result = application.update(plan, Packagers(args.username), bump_pkgrel=args.increment)
Rebuild.check_status(args.exit_code, not result.is_empty)
@staticmethod
@@ -82,7 +82,7 @@ class Rollback(Handler):
parser.add_argument("-u", "--username", help="build as user", default=extract_user())
parser.set_defaults(aur=False, changes=False, check_files=False, dependencies=False, dry_run=False,
exit_code=True, increment=False, now=True, local=False, manual=False, refresh=False,
source=PackageSource.Archive, variable=None, vcs=False)
source=PackageSource.ARCHIVE, variable=None, vcs=False)
return parser
@staticmethod
@@ -23,6 +23,7 @@ from ahriman import __version__
from ahriman.application.handlers.handler import Handler, SubParserAction
from ahriman.core.configuration import Configuration
from ahriman.core.formatters import UpdatePrinter
from ahriman.models.build_plan import BuildPlan, BuildPlanReason
from ahriman.models.package import Package
from ahriman.models.repository_id import RepositoryId
@@ -56,7 +57,8 @@ class ServiceUpdates(Handler):
if same_version:
return
UpdatePrinter(remote, local_version)(verbose=True, separator=" -> ")
plan = BuildPlan().update([remote], BuildPlanReason.REGULAR_UPDATE)
UpdatePrinter(plan, {remote.base: local_version})(verbose=True)
ServiceUpdates.check_status(args.exit_code, same_version)
@staticmethod
@@ -85,7 +85,7 @@ class Statistics(Handler):
parser.add_argument("package", help="fetch only events for the specified package", nargs="?")
parser.add_argument("--chart", help="create updates chart and save it to the specified path", type=Path)
parser.add_argument("-e", "--event", help="event type filter",
type=EventType, choices=tuple(EventType), default=EventType.PackageUpdated)
type=EventType, choices=tuple(EventType), default=EventType.PACKAGE_UPDATED)
parser.add_argument("--from-date", help="only fetch events which are newer than the date")
parser.add_argument("--limit", help="limit response by specified amount of events", type=int, default=-1)
parser.add_argument("--offset", help="skip specified amount of events", type=int, default=0)
@@ -158,7 +158,7 @@ class Statistics(Handler):
Args:
event_type(str): event type
events(list[Event]): list of events
chart_path(Path): path to save plot if any
chart_path(Path | None): path to save plot if any
"""
# event statistics
Statistics.event_stats(event_type, events)
@@ -175,7 +175,7 @@ class Statistics(Handler):
Args:
event_type(str): event type
events(list[Event]): list of events
chart_path(Path): path to save plot if any
chart_path(Path | None): path to save plot if any
"""
key: Callable[[Event], str] = lambda event: event.object_id
by_object_id = {
@@ -18,6 +18,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import argparse
import itertools
from collections.abc import Callable
@@ -56,9 +57,9 @@ class Status(Handler):
service_status = client.status_get()
StatusPrinter(service_status.status)(verbose=args.info)
if args.package:
packages: list[tuple[Package, BuildStatus]] = sum(
(client.package_get(base) for base in args.package),
start=[])
packages: list[tuple[Package, BuildStatus]] = list(
itertools.chain.from_iterable(client.package_get(base) for base in args.package)
)
else:
packages = client.package_get(None)
@@ -49,14 +49,14 @@ class StatusUpdate(Handler):
client = Application(repository_id, configuration, report=True).reporter
match args.action:
case Action.Update if args.package:
case Action.UPDATE if args.package:
# update packages statuses
for package in args.package:
client.package_status_update(package, args.status)
case Action.Update:
case Action.UPDATE:
# update service status
client.status_update(args.status)
case Action.Remove:
case Action.REMOVE:
for package in args.package:
client.package_remove(package)
@@ -76,7 +76,7 @@ class StatusUpdate(Handler):
epilog="Please note that this subcommand does not remove the package itself, it just "
"clears the status page.")
parser.add_argument("package", help="remove specified packages from status page", nargs="+")
parser.set_defaults(action=Action.Remove, lock=None, quiet=True, report=False, unsafe=True)
parser.set_defaults(action=Action.REMOVE, lock=None, quiet=True, report=False, unsafe=True)
return parser
@staticmethod
@@ -96,8 +96,8 @@ class StatusUpdate(Handler):
"If no packages supplied, service status will be updated",
nargs="*")
parser.add_argument("-s", "--status", help="new package build status",
type=BuildStatusEnum, choices=tuple(BuildStatusEnum), default=BuildStatusEnum.Success)
parser.set_defaults(action=Action.Update, lock=None, quiet=True, report=False, unsafe=True)
type=BuildStatusEnum, choices=tuple(BuildStatusEnum), default=BuildStatusEnum.SUCCESS)
parser.set_defaults(action=Action.UPDATE, lock=None, quiet=True, report=False, unsafe=True)
return parser
@staticmethod
@@ -114,8 +114,8 @@ class StatusUpdate(Handler):
parser = root.add_parser("repo-status-update", help="update repository status",
description="update repository status on the status page")
parser.add_argument("-s", "--status", help="new status",
type=BuildStatusEnum, choices=tuple(BuildStatusEnum), default=BuildStatusEnum.Success)
parser.set_defaults(action=Action.Update, lock=None, package=[], quiet=True, report=False, unsafe=True)
type=BuildStatusEnum, choices=tuple(BuildStatusEnum), default=BuildStatusEnum.SUCCESS)
parser.set_defaults(action=Action.UPDATE, lock=None, package=[], quiet=True, report=False, unsafe=True)
return parser
arguments = [
@@ -77,7 +77,7 @@ class UnsafeCommands(Handler):
check if command is unsafe
Args:
command(str): command to check
command(list[str]): command and arguments to check
unsafe_commands(list[str]): list of unsafe commands
parser(argparse.ArgumentParser): generated argument parser
"""
@@ -147,20 +147,19 @@ class Update(Handler):
application(Application): application instance
args(argparse.Namespace): command line args
"""
packages = application.updates(args.package, aur=args.aur, local=args.local, manual=args.manual, vcs=args.vcs,
check_files=args.check_files)
plan = application.updates(args.package, aur=args.aur, local=args.local, manual=args.manual, vcs=args.vcs,
check_files=args.check_files)
plan = application.with_dependencies(plan, process_dependencies=args.dependencies)
if args.changes: # generate changes if requested
application.changes(packages)
application.changes(plan)
application.print_updates(plan, Update.log_fn(application, dry_run=args.dry_run), bump_pkgrel=args.increment)
if args.dry_run: # exit from application if no build requested
Update.check_status(args.exit_code, packages) # status code check
Update.check_status(args.exit_code, plan) # status code check
return
packages = application.with_dependencies(packages, process_dependencies=args.dependencies)
packagers = Packagers(args.username, {package.base: package.packager for package in packages})
application.print_updates(packages, log_fn=application.logger.info)
result = application.update(packages, packagers, bump_pkgrel=args.increment)
packagers = Packagers(args.username, {package.base: package.packager for package in plan})
result = application.update(plan, packagers, bump_pkgrel=args.increment)
Update.check_status(args.exit_code, not result.is_empty)
arguments = [
@@ -53,17 +53,17 @@ class Users(Handler):
database = SQLite.load(configuration)
match args.action:
case Action.Update:
case Action.UPDATE:
user = Users.user_create(args)
# if password is left blank we are not going to require salt to be set
salt = configuration.get("auth", "salt", fallback="") if user.password else ""
database.user_update(user.hash_password(salt))
case Action.List:
case Action.LIST:
users = database.user_list(args.username, args.role)
for user in users:
UserPrinter(user)(verbose=True)
Users.check_status(args.exit_code, users)
case Action.Remove:
case Action.REMOVE:
database.user_remove(args.username)
@staticmethod
@@ -88,8 +88,8 @@ class Users(Handler):
"-p", "--password", help="user password. Blank password will be treated as empty password, "
"which is in particular must be used for OAuth2 authorization type.")
parser.add_argument("-R", "--role", help="user access level",
type=UserAccess, choices=tuple(UserAccess), default=UserAccess.Read)
parser.set_defaults(action=Action.Update, architecture="", exit_code=False, lock=None, quiet=True,
type=UserAccess, choices=tuple(UserAccess), default=UserAccess.READ)
parser.set_defaults(action=Action.UPDATE, architecture="", exit_code=False, lock=None, quiet=True,
report=False, repository="")
return parser
@@ -110,7 +110,7 @@ class Users(Handler):
parser.add_argument("-e", "--exit-code", help="return non-zero exit status if result is empty",
action="store_true")
parser.add_argument("-R", "--role", help="filter users by role", type=UserAccess, choices=tuple(UserAccess))
parser.set_defaults(action=Action.List, architecture="", lock=None, quiet=True, report=False, repository="",
parser.set_defaults(action=Action.LIST, architecture="", lock=None, quiet=True, report=False, repository="",
unsafe=True)
return parser
@@ -128,7 +128,7 @@ class Users(Handler):
parser = root.add_parser("user-remove", help="remove user",
description="remove user from the user mapping and update the configuration")
parser.add_argument("username", help="username for web service")
parser.set_defaults(action=Action.Remove, architecture="", exit_code=False, lock=None, quiet=True,
parser.set_defaults(action=Action.REMOVE, architecture="", exit_code=False, lock=None, quiet=True,
report=False, repository="")
return parser
@@ -19,6 +19,7 @@
#
import argparse
import copy
import itertools
from typing import Any
@@ -97,7 +98,7 @@ class Validate(Handler):
# create trigger loader instance
loader = TriggerLoader()
triggers = loader.selected_triggers(configuration) + loader.known_triggers(configuration)
triggers = itertools.chain(loader.selected_triggers(configuration), loader.known_triggers(configuration))
for trigger in set(triggers):
try:
@@ -18,12 +18,12 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import argparse
import itertools
import re
import sys
from collections.abc import Iterator
from importlib import metadata
from typing import ClassVar
from ahriman import __version__
from ahriman.application.handlers.handler import Handler, SubParserAction
@@ -35,13 +35,10 @@ from ahriman.models.repository_id import RepositoryId
class Versions(Handler):
"""
version handler
Attributes:
PEP423_PACKAGE_NAME(re.Pattern[str]): (class attribute) special regex for valid PEP423 package name
"""
ALLOW_MULTI_ARCHITECTURE_RUN = False # system-wide action
PEP423_PACKAGE_NAME: ClassVar[re.Pattern[str]] = re.compile(r"^[A-Za-z0-9._-]+")
_PEP423_PACKAGE_NAME = re.compile(r"^[A-Za-z0-9._-]+")
@classmethod
def run(cls, args: argparse.Namespace, repository_id: RepositoryId, configuration: Configuration, *,
@@ -57,7 +54,7 @@ class Versions(Handler):
"""
VersionPrinter(f"Module version {__version__}",
{"Python": sys.version})(verbose=False, separator=" ")
packages = Versions.package_dependencies("ahriman")
packages = Versions.package_dependencies("ahriman-core", "ahriman-triggers", "ahriman-web")
VersionPrinter("Installed packages", dict(packages))(verbose=False, separator=" ")
@staticmethod
@@ -77,15 +74,15 @@ class Versions(Handler):
return parser
@staticmethod
def package_dependencies(root: str) -> Iterator[tuple[str, str]]:
def package_dependencies(*roots: str) -> Iterator[tuple[str, str]]:
"""
extract list of ahriman package dependencies installed into system with their versions
extract installed root packages and their dependencies with their versions
Args:
root(str): root package name
*roots(str): root package names
Yields:
tuple[str, str]: map of installed dependency to its version
tuple[str, str]: map of installed package to its version
"""
def dependencies_by_key(key: str) -> Iterator[str]:
# in importlib it returns requires in the following format
@@ -95,15 +92,15 @@ class Versions(Handler):
except metadata.PackageNotFoundError:
return
for entry in requires or []:
yield from Versions.PEP423_PACKAGE_NAME.findall(entry)
yield from Versions._PEP423_PACKAGE_NAME.findall(entry)
keys: list[str] = []
portion = set(dependencies_by_key(root))
portion = set(roots)
while portion:
keys.extend(portion)
portion = {
key
for key in sum((list(dependencies_by_key(key)) for key in portion), start=[])
for key in itertools.chain.from_iterable(dependencies_by_key(key) for key in portion)
if key not in keys
}
+2 -2
View File
@@ -209,7 +209,7 @@ class Lock(LazyLogging):
self.check_user()
self.check_version()
self.lock()
self.reporter.status_update(BuildStatusEnum.Building)
self.reporter.status_update(BuildStatusEnum.BUILDING)
return self
def __exit__(self, exc_type: type[Exception] | None, exc_val: Exception | None,
@@ -226,6 +226,6 @@ class Lock(LazyLogging):
Literal[False]: always ``False`` (do not suppress any exception)
"""
self.clear()
status = BuildStatusEnum.Success if exc_val is None else BuildStatusEnum.Failed
status = BuildStatusEnum.SUCCESS if exc_val is None else BuildStatusEnum.FAILED
self.reporter.status_update(status)
return False
+1 -1
View File
@@ -88,7 +88,7 @@ class _Context:
iterate over keys in local storage
Returns:
str: context key iterator
Iterator[str]: context key iterator
"""
return iter(self._content)
+1 -1
View File
@@ -96,7 +96,7 @@ class Pacman(LazyLogging):
self.database_init(handle, self.repository_id.name, self.repository_id.architecture)
if use_ahriman_cache and refresh_database:
self.database_sync(handle, force=refresh_database == PacmanSynchronization.Force)
self.database_sync(handle, force=refresh_database == PacmanSynchronization.FORCE)
return handle
@@ -17,355 +17,326 @@
# 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 itertools
import re
import shlex
import tree_sitter_bash
from collections.abc import Iterator
from collections.abc import Iterable, Iterator
from enum import StrEnum
from typing import IO
from tree_sitter import Language, Node, Parser
from typing import IO, cast
from ahriman.core.configuration.shell_interpolator import ShellInterpolator
from ahriman.core.exceptions import PkgbuildParserError
from ahriman.models.pkgbuild_patch import PkgbuildPatch
class PkgbuildToken(StrEnum):
class PkgbuildNodeType(StrEnum):
"""
well-known tokens dictionary
tree-sitter node types used by parser
Attributes:
ArrayEnds(PkgbuildToken): array ends token
ArrayStarts(PkgbuildToken): array starts token
Comma(PkgbuildToken): comma token
Comment(PkgbuildToken): comment token
FunctionDeclaration(PkgbuildToken): function declaration token
FunctionEnds(PkgbuildToken): function ends token
FunctionStarts(PkgbuildToken): function starts token
NewLine(PkgbuildToken): new line token
APPEND(PkgbuildNodeType): append node
ARRAY(PkgbuildNodeType): array node
COMMENT(PkgbuildNodeType): comment node
CONCATENATION(PkgbuildNodeType): concatenation node
ERROR(PkgbuildNodeType): syntax error node
FUNCTION_DEFINITION(PkgbuildNodeType): function definition node
NUMBER(PkgbuildNodeType): numeric word node
RAW_STRING(PkgbuildNodeType): single-quoted string node
STRING(PkgbuildNodeType): double-quoted string node
STRING_CONTENT(PkgbuildNodeType): string content node
VARIABLE_ASSIGNMENT(PkgbuildNodeType): assignment node
WORD(PkgbuildNodeType): word node
"""
ArrayStarts = "("
ArrayEnds = ")"
Comma = ","
Comment = "#"
FunctionDeclaration = "()"
FunctionStarts = "{"
FunctionEnds = "}"
NewLine = "\n"
APPEND = "+="
ARRAY = "array"
COMMENT = "comment"
CONCATENATION = "concatenation"
ERROR = "ERROR"
FUNCTION_DEFINITION = "function_definition"
NUMBER = "number"
RAW_STRING = "raw_string"
STRING = "string"
STRING_CONTENT = "string_content"
VARIABLE_ASSIGNMENT = "variable_assignment"
WORD = "word"
class PkgbuildParser(shlex.shlex):
class PkgbuildNodeField(StrEnum):
"""
simple pkgbuild reader implementation in pure python, because others suck.
tree-sitter node fields used by parser
What is it:
Attributes:
BODY(PkgbuildNodeField): function body field
NAME(PkgbuildNodeField): variable or function name field
VALUE(PkgbuildNodeField): assignment value field
"""
#. Simple PKGBUILD parser written in python.
#. No shell execution, so it is free from random shell attacks.
#. Able to parse simple constructions (assignments, comments, functions, arrays).
BODY = "body"
NAME = "name"
VALUE = "value"
class PkgbuildParser(Iterable[PkgbuildPatch]):
"""
PKGBUILD parser implementation in python
What it is:
#. A PKGBUILD parser based on tree-sitter's bash grammar.
#. A parser for assignments, arrays, and functions.
#. A parser which does not execute shell code.
What it is not:
#. Fully functional shell parser.
#. Shell executor.
#. No parameter expansion.
#. A shell executor.
#. A shell interpreter. Variable and command substitutions are returned as written.
For more details what does it support, please, consult with the test cases.
Simple comma-separated brace expressions in array elements are expanded. Function bodies are returned from the
original source, with their formatting preserved. Assignments inside shell statements and control-flow nodes are
extracted without checking whether those statements would run.
Each iteration parses the stored source independently, starting from the beginning.
Examples:
This class is heavily based on :mod:`shlex` parser, but instead of strings operates with the
:class:`ahriman.models.pkgbuild_patch.PkgbuildPatch` objects. The main way to use it is to call :func:`parse()`
function and collect parsed objects, e.g.::
Parse package assignments::
>>> parser = PkgbuildParser(StringIO("input string"))
>>> for patch in parser.parse():
>>> print(f"{patch.key} = {patch.value}")
>>> source = b"pkgname=example\\npkgver=1\\n"
>>> for patch in PkgbuildParser(BytesIO(source)):
... print(patch.key, patch.value)
It doesn't store the state of the fields (but operates with the :mod:`shlex` parser state), so no shell
post-processing is performed (e.g. variable substitution).
Function bodies are returned as function patches::
>>> source = b"pkgver() { printf 1; }"
>>> for patch in PkgbuildParser(BytesIO(source)):
... print(patch.key, patch.value)
"""
_ARRAY_ASSIGNMENT = re.compile(r"^(?P<key>\w+)=$")
# in addition to usual assignment, functions can have dash
_FUNCTION_DECLARATION = re.compile(r"^(?P<key>[\w-]+)$")
_STRING_ASSIGNMENT = re.compile(r"^(?P<key>\w+)=(?P<value>.+)$")
_ESCAPED_CHARACTERS = {
"\\$": "$",
"\\`": "`",
"\\\"": "\"",
"\\\n": "",
}
def __init__(self, stream: IO[str]) -> None:
def __init__(self, stream: IO[bytes]) -> None:
"""
Args:
stream(IO[str]): input stream containing PKGBUILD content
stream(IO[bytes]): input PKGBUILD stream
"""
shlex.shlex.__init__(self, stream, posix=True, punctuation_chars=True)
self._io = stream # direct access without type casting
# ignore substitution and extend bash symbols
self.wordchars += "${}#:+-@!"
# in case of default behavior, it will ignore, for example, segment part of url outside of quotes
self.commenters = ""
self._content = stream.read()
@staticmethod
def _expand_array(array: list[str]) -> list[str]:
def text(node: Node) -> str:
"""
bash array expansion simulator. It takes raw array and tries to expand constructions like
``(first prefix-{mid1,mid2}-suffix last)`` into ``(first, prefix-mid1-suffix prefix-mid2-suffix last)``
read the original source text represented by a syntax tree node
Args:
array(list[str]): input array
node(Node): syntax tree node to read
Returns:
list[str]: either source array or expanded array if possible
Raises:
PkgbuildParserError: if there are errors in parser
str: source text for the node
"""
# we are using comma as marker for expansion (if any)
if PkgbuildToken.Comma not in array:
return array
# again sanity check, for expansion there are at least 3 elements (first, last and comma)
if len(array) < 3:
return array
return cast(bytes, node.text).decode("utf8", errors="backslashreplace")
result = []
buffer, prefix = [], None
for index, (first, second) in enumerate(itertools.pairwise(array)):
match (first, second):
# in this case we check if expansion should be started
# this condition matches "prefix{first", ","
case (_, PkgbuildToken.Comma) if PkgbuildToken.FunctionStarts in first:
prefix, part = first.rsplit(PkgbuildToken.FunctionStarts, maxsplit=1)
buffer.append(f"{prefix}{part}")
# the last element case, it matches either ",", "last}" or ",", "last}suffix"
# in case if there is suffix, it must be appended to all list elements
case (PkgbuildToken.Comma, _) if prefix is not None and PkgbuildToken.FunctionEnds in second:
part, suffix = second.rsplit(PkgbuildToken.FunctionEnds, maxsplit=1)
buffer.append(f"{prefix}{part}")
result.extend([f"{part}{suffix}" for part in buffer])
# reset state
buffer, prefix = [], None
# we have already got prefix string, so we are in progress of expansion
# we always operate the last element, so this matches ",", "next"
case (PkgbuildToken.Comma, _) if prefix is not None:
buffer.append(f"{prefix}{second}")
# exactly first element of the list
case (_, _) if prefix is None and index == 0:
result.append(first)
# any next normal element
case (_, _) if prefix is None:
result.append(second)
# small sanity check
if prefix is not None:
raise PkgbuildParserError("error in array expansion", array)
return result
def _is_escaped(self) -> bool:
@staticmethod
def unquote(value: str) -> str:
"""
check if the last element was quoted. ``shlex.shlex`` parser doesn't provide information about was the token
quoted or not, thus there is no difference between "'#'" (sharp in quotes) and "#" (sharp without quotes). This
method simply rolls back to the last non-space character and check if it is a quotation mark
Returns:
bool: ``True`` if the previous element of the stream is a quote or escaped and ``False`` otherwise
"""
current_position = self._io.tell()
last_char = penultimate_char = None
index = current_position - 1
while index > 0:
index, last_char = self._read_last(index)
if last_char.isspace():
index -= 1
continue
if index > 1:
_, penultimate_char = self._read_last(index - 1)
break
self._io.seek(current_position) # reset position of the stream
is_quoted = last_char is not None and last_char in self.quotes
is_escaped = penultimate_char is not None and penultimate_char in self.escape
return is_quoted or is_escaped
def _parse_array(self) -> list[str]:
"""
parse array from the PKGBUILD. This method will extract tokens from parser until it matches closing array,
modifying source parser state
Returns:
list[str]: extracted arrays elements
Raises:
PkgbuildParserError: if array is not closed
"""
def extract() -> Iterator[str]:
while token := self.get_token():
match token:
case _ if self._is_escaped():
pass
case PkgbuildToken.ArrayEnds:
break
case comment if comment.startswith(PkgbuildToken.Comment):
self._read_comment()
continue
yield token
if token != PkgbuildToken.ArrayEnds:
raise PkgbuildParserError("no closing array bracket found")
return self._expand_array(list(extract()))
def _parse_function(self) -> str:
"""
parse function from the PKGBUILD. This method will extract tokens from parser until it matches closing function,
modifying source parser state. Instead of trying to combine tokens together, it uses positions of the file
and reads content again in this range
Returns:
str: function body
Raises:
PkgbuildParserError: if function body wasn't found or parser input stream doesn't support position reading
"""
# find start and end positions
start_position = end_position = -1
counter = 0 # simple processing of the inner "{" and "}"
for token in self:
match token:
case _ if self._is_escaped():
continue
case PkgbuildToken.FunctionStarts:
if counter == 0:
start_position = self._io.tell() - 1
counter += 1
case PkgbuildToken.FunctionEnds:
end_position = self._io.tell()
if self.state != self.eof: # type: ignore[attr-defined]
end_position -= 1 # if we are not at the end of the file, position is _after_ the token
counter -= 1
if counter == 0:
break
case comment if comment.startswith(PkgbuildToken.Comment):
self._read_comment()
if not 0 < start_position < end_position:
raise PkgbuildParserError("function body wasn't found")
# read the specified interval from source stream
self._io.seek(start_position - 1) # start from the previous symbol
# we cannot use :func:`read()` here, because it reads characters, not bytes
content = ""
while self._io.tell() != end_position and (next_char := self._io.read(1)):
content += next_char
# special case of the end of file
if self.state == self.eof: # type: ignore[attr-defined]
content += self._io.read(1)
return content
def _parse_token(self, token: str) -> Iterator[PkgbuildPatch]:
"""
parse single token to the PKGBUILD field
remove shell quotes and escapes from a single value
Args:
token(str): current token
value(str): shell value to unquote
Returns:
str: unquoted value
"""
return "".join(shlex.split(value, comments=False, posix=True))
@staticmethod
def value(node: Node) -> str:
"""
extract a string value from a syntax tree node
Args:
node(Node): syntax tree node to extract
Returns:
str: extracted string value
"""
def parts(source: str) -> Iterator[str]:
for child in node.named_children:
# retain gaps (such as newlines) between children
prefix, _, source = source.partition(PkgbuildParser.text(child))
yield prefix
yield PkgbuildParser.value(child)
yield source
def unescape(source: str) -> str:
for original, replacement in PkgbuildParser._ESCAPED_CHARACTERS.items():
source = source.replace(original, replacement)
return source
# raw processing to avoid converting to text if not required
if node.type == PkgbuildNodeType.CONCATENATION:
return "".join(PkgbuildParser.value(child) for child in node.named_children)
value = PkgbuildParser.text(node)
match node.type:
case PkgbuildNodeType.STRING:
# this node is always quoted, so that we manually remove first and last characters
return "".join(parts(value[1:-1]))
case PkgbuildNodeType.STRING_CONTENT:
# unescape literal text only, substitutions are returned as written
value = PkgbuildParser.unquote(f'"{value}"')
return "\\".join(unescape(part) for part in value.split("\\\\"))
case PkgbuildNodeType.NUMBER | PkgbuildNodeType.RAW_STRING | PkgbuildNodeType.WORD:
return PkgbuildParser.unquote(value)
case _:
return value
def _assignment(self, node: Node) -> PkgbuildPatch:
"""
convert an assignment node
Args:
node(Node): assignment node to convert
Returns:
PkgbuildPatch: parsed assignment
"""
key = self.text(cast(Node, node.child_by_field_name(PkgbuildNodeField.NAME)))
value = node.child_by_field_name(PkgbuildNodeField.VALUE)
# bash permits assignments without a value (for example ``IFS=``).
# tree-sitter represents those assignments without a value node.
if value is None:
return PkgbuildPatch(key, "")
match value.type:
case PkgbuildNodeType.ARRAY:
values = []
for element in filter(lambda child: child.type != PkgbuildNodeType.COMMENT, value.named_children):
values.extend(self._expand_array(element))
return PkgbuildPatch(key, values)
case _:
return PkgbuildPatch(key, self.value(value))
def _expand_array(self, node: Node) -> list[str]:
"""
expand simple comma-separated brace groups in an array element
Args:
node(Node): array element to expand
Returns:
list[str]: expanded element values
"""
if node.type != PkgbuildNodeType.CONCATENATION:
return [self.value(node)]
values = [""]
children = node.named_children
while children:
match children[:3]:
case [
Node(type=PkgbuildNodeType.WORD, text=b"{"),
Node(type=PkgbuildNodeType.WORD) as content,
Node(type=PkgbuildNodeType.WORD, text=b"}"),
] if "," in (escaped := self.text(content).replace("\\,", ShellInterpolator.DATA_LINK_ESCAPE)):
parts = [
self.unquote(part).replace(ShellInterpolator.DATA_LINK_ESCAPE, ",")
for part in escaped.split(",")
]
children = children[3:]
case _:
parts = [self.value(children.pop(0))]
values = [prefix + part for prefix in values for part in parts]
return values
def _read(self, node: Node, values: dict[str | None, PkgbuildPatch]) -> Iterator[PkgbuildPatch]:
"""
find assignments and functions in a syntax tree node
Args:
node(Node): syntax tree node to inspect
values(dict[str | None, PkgbuildPatch]): cached assignments
Yields:
PkgbuildPatch: extracted a PKGBUILD node
PkgbuildPatch: parsed entries
"""
# simple assignment rule
if m := self._STRING_ASSIGNMENT.match(token):
key = m.group("key")
value = m.group("value")
yield PkgbuildPatch(key, value)
return
match node.type:
case PkgbuildNodeType.VARIABLE_ASSIGNMENT if any(child.type == PkgbuildNodeType.APPEND for child in node.children):
patch = self._assignment(node)
match values.get(patch.key), patch.value:
case PkgbuildPatch(value=str() as previous), str() as appended:
patch = PkgbuildPatch(patch.key, previous + appended)
case PkgbuildPatch(value=str() as previous), list() as appended:
patch = PkgbuildPatch(patch.key, [previous, *appended])
case PkgbuildPatch(value=list() as previous), list() as appended:
patch = PkgbuildPatch(patch.key, previous + appended)
values[patch.key] = patch
yield patch
if token.startswith(PkgbuildToken.Comment):
self._read_comment()
return
case PkgbuildNodeType.VARIABLE_ASSIGNMENT:
patch = self._assignment(node)
values[patch.key] = patch
yield patch
match self.get_token():
# array processing. Arrays will be sent as "key=", "(", values, ")"
case PkgbuildToken.ArrayStarts if m := self._ARRAY_ASSIGNMENT.match(token):
key = m.group("key")
value = self._parse_array()
yield PkgbuildPatch(key, value)
case PkgbuildNodeType.FUNCTION_DEFINITION:
name = cast(Node, node.child_by_field_name(PkgbuildNodeField.NAME))
body = cast(Node, node.child_by_field_name(PkgbuildNodeField.BODY))
yield PkgbuildPatch(f"{self.text(name)}()", self.text(body))
# functions processing. Function will be sent as "name", "()", "{", body, "}"
case PkgbuildToken.FunctionDeclaration if self._FUNCTION_DECLARATION.match(token):
key = f"{token}{PkgbuildToken.FunctionDeclaration}"
value = self._parse_function()
yield PkgbuildPatch(key, value) # this is not mistake, assign to token without ()
# special function case, where "(" and ")" are separated tokens, e.g. "pkgver ( )"
case PkgbuildToken.ArrayStarts if self._FUNCTION_DECLARATION.match(token):
next_token = self.get_token()
if next_token == PkgbuildToken.ArrayEnds: # replace closing bracket with "()"
next_token = PkgbuildToken.FunctionDeclaration
self.push_token(next_token) # type: ignore[arg-type]
yield from self._parse_token(token)
# some random token received without continuation, lets guess it is empty assignment (i.e. key=)
case other if other is not None:
yield from self._parse_token(other)
def _read_comment(self) -> None:
"""
read comment from the current position. This method doesn't check comment itself, just read the stream
until the comment line ends
"""
_, last_symbol = self._read_last()
if last_symbol != PkgbuildToken.NewLine:
self.instream.readline()
def _read_last(self, initial_index: int | None = None) -> tuple[int, str]:
"""
wrapper around read to read the last symbol from the input stream. This method is designed to process UTF-8
symbols correctly. This method does not reset current stream position
Args:
initial_index(int | None, optional): initial index to start reading from. If none set, the previous position
will be used (Default value = None)
Returns:
tuple[int, str]: last symbol and its position in the stream
Raises:
PkgbuildParserError: in case if stream reached starting position, but no valid symbols were found
"""
if initial_index is None:
initial_index = self._io.tell() - 1
if initial_index < 0:
raise PkgbuildParserError("stream is on starting position")
self._io.seek(initial_index)
while (position := self._io.tell()) > 0:
try:
return position, self._io.read(1)
except UnicodeDecodeError:
self._io.seek(position - 1)
raise PkgbuildParserError("reached starting position, no valid symbols found")
case _:
# descend into statements, but skip command arguments and values
for child in node.named_children:
if child.type not in (
PkgbuildNodeType.ARRAY,
PkgbuildNodeType.COMMENT,
PkgbuildNodeType.RAW_STRING,
PkgbuildNodeType.STRING,
PkgbuildNodeType.WORD,
):
yield from self._read(child, values)
def parse(self) -> Iterator[PkgbuildPatch]:
"""
parse source stream and yield parsed entries
parse source and yield extracted entries
Yields:
PkgbuildPatch: extracted a PKGBUILD node
PkgbuildPatch: parsed entries
Raises:
PkgbuildParserError: source contains invalid shell syntax
"""
for token in self:
yield from self._parse_token(token)
def walk(node: Node) -> Iterator[Node]:
yield node
for child in node.children:
yield from walk(child)
parser = Parser(Language(tree_sitter_bash.language()))
tree = parser.parse(self._content)
if tree.root_node.has_error:
error = next((node for node in walk(tree.root_node) if node.is_error or node.is_missing), tree.root_node)
raise PkgbuildParserError(
f"syntax error at line {error.start_point.row + 1}, column {error.start_point.column + 1}"
)
yield from self._read(tree.root_node, {})
def __iter__(self) -> Iterator[PkgbuildPatch]:
"""
iterate over the parsed entries
Returns:
Iterator[PkgbuildPatch]: parsed entries
"""
return self.parse()
@@ -165,7 +165,7 @@ class Sources(LazyLogging):
# extract local files...
files = ["PKGBUILD", ".SRCINFO"] + [str(path) for path in Pkgbuild.local_files(sources_dir)]
instance.add(sources_dir, *files)
instance.add(sources_dir, *files, force=True)
# ...and commit them
instance.commit(sources_dir)
@@ -232,7 +232,7 @@ class Sources(LazyLogging):
git_url, branch = remote.git_source()
check_output(*instance.git(), "push", "--quiet", git_url, branch, cwd=sources_dir, logger=instance.logger)
def add(self, sources_dir: Path, *pattern: str, intent_to_add: bool = False) -> None:
def add(self, sources_dir: Path, *pattern: str, intent_to_add: bool = False, force: bool = False) -> None:
"""
track found files via git
@@ -241,6 +241,7 @@ class Sources(LazyLogging):
*pattern(str): glob patterns
intent_to_add(bool, optional): record only the fact that it will be added later, acts as
--intent-to-add git flag (Default value = False)
force(bool, optional): allow adding ignored files (Default value = False)
"""
# glob directory to find files which match the specified patterns
found_files: list[Path] = []
@@ -251,6 +252,8 @@ class Sources(LazyLogging):
self.logger.info("found matching files %s", found_files)
# add them to index
args = ["--intent-to-add"] if intent_to_add else []
if force:
args.append("--force")
check_output(*self.git(), "add", *args, *[str(fn.relative_to(sources_dir)) for fn in found_files],
cwd=sources_dir, logger=self.logger)
@@ -134,7 +134,7 @@ class Configuration(configparser.RawConfigParser):
repository identifier
Returns:
RepositoryId: repository unique identifier
RepositoryId | None: repository unique identifier
"""
return self._repository_id
@@ -58,7 +58,7 @@ class Migrations(LazyLogging):
configuration(Configuration): configuration instance
Returns:
MigrationResult: current schema version
MigrationResult: schema versions before and after migration
"""
return Migrations(connection, configuration).run()
@@ -144,8 +144,8 @@ class Migrations(LazyLogging):
"""
perform migrations
Return:
MigrationResult: current schema version
Returns:
MigrationResult: schema versions before and after migration
"""
migrations = self.migrations()
@@ -35,7 +35,7 @@ class BuildOperations(Operations):
Args:
package_base(str | None): optional filter by package base
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -57,9 +57,9 @@ class BuildOperations(Operations):
retrieve packages from build queue
Args:
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
Return:
Returns:
list[Package]: list of packages to be built
"""
repository_id = repository_id or self._repository_id
@@ -81,7 +81,7 @@ class BuildOperations(Operations):
Args:
package(Package): package to be inserted
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -35,7 +35,7 @@ class ChangesOperations(Operations):
Args:
package_base(str): package base to search
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
Returns:
Changes: changes for the package base if available
@@ -69,7 +69,7 @@ class ChangesOperations(Operations):
Args:
package_base(str): package base to insert
changes(Changes): package changes (as in patch format)
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -101,7 +101,7 @@ class ChangesOperations(Operations):
Args:
package_base(str | None): optional filter by package base
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -36,10 +36,10 @@ class DependenciesOperations(Operations):
Args:
package_base(str | None): package base to search
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
Returns:
Dependencies: dependencies for the package base if available
dict[str, Dependencies]: dependencies keyed by package base, optionally filtered by package base
"""
repository_id = repository_id or self._repository_id
@@ -69,7 +69,7 @@ class DependenciesOperations(Operations):
Args:
package_base(str): package base
dependencies(Dependencies): package dependencies
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -97,7 +97,7 @@ class DependenciesOperations(Operations):
Args:
package_base(str | None): optional filter by package base
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -42,7 +42,7 @@ class EventOperations(Operations):
to_date(int | float | None, optional): maximal creation date, exclusive (Default value = None)
limit(int, optional): limit records to the specified count, -1 means unlimited (Default value = -1)
offset(int, optional): records offset (Default value = 0)
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
Returns:
list[Event]: list of audit log events
@@ -84,7 +84,7 @@ class EventOperations(Operations):
Args:
event(Event): event to insert
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -106,3 +106,33 @@ class EventOperations(Operations):
})
return self.with_connection(run, commit=True)
def events_rotate(self, keep_last_records: int, repository_id: RepositoryId | None = None) -> None:
"""
rotate events in storage, keeping only the last N records for each object
Args:
keep_last_records(int): number of last records to keep
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
def run(connection: Connection) -> None:
connection.execute(
"""
delete from auditlog
where rowid in (
select rowid from (
select rowid, row_number() over (
partition by object_id order by created desc, rowid desc
) as rn
from auditlog
where repository = :repository
)
where rn > :keep_last_records
)
""",
{"keep_last_records": keep_last_records, "repository": repository_id.id},
)
self.with_connection(run, commit=True)
@@ -40,9 +40,9 @@ class LogsOperations(Operations):
process_id(str | None, optional): process identifier to filter (Default value = None)
limit(int, optional): limit records to the specified count, -1 means unlimited (Default value = -1)
offset(int, optional): records offset (Default value = 0)
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
Return:
Returns:
list[LogRecord]: sorted package log records
"""
repository_id = repository_id or self._repository_id
@@ -79,7 +79,7 @@ class LogsOperations(Operations):
Args:
log_record(LogRecord): log record object
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -106,7 +106,7 @@ class LogsOperations(Operations):
Args:
package_base(str): package base to remove logs
version(str | None): package version. If set it will remove only logs belonging to another version
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -133,7 +133,7 @@ class LogsOperations(Operations):
Args:
keep_last_records(int): number of last records to keep
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -66,7 +66,7 @@ class Operations(LazyLogging):
dictionary factory based on official documentation
Args:
cursor(Cursor): cursor descriptor
cursor(sqlite3.Cursor): cursor descriptor
row(tuple[Any, ...]): fetched row
Returns:
@@ -82,7 +82,7 @@ class Operations(LazyLogging):
perform operation in connection
Args:
query(Callable[[Connection], T]): function to be called with connection
query(Callable[[sqlite3.Connection], T]): function to be called with connection
commit(bool, optional): if ``True`` commit() will be called on success (Default value = False)
Returns:
@@ -225,7 +225,7 @@ class PackageOperations(Operations):
Args:
package_base(str): package base name
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
enabled(bool): new hold status
"""
repository_id = repository_id or self._repository_id
@@ -248,7 +248,7 @@ class PackageOperations(Operations):
Args:
package_base(str): package base name
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -264,7 +264,7 @@ class PackageOperations(Operations):
Args:
package(Package): package properties
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -280,9 +280,9 @@ class PackageOperations(Operations):
get package list and their build statuses from database
Args:
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
Return:
Returns:
list[tuple[Package, BuildStatus]]: list of package properties and their statuses
"""
repository_id = repository_id or self._repository_id
@@ -303,7 +303,7 @@ class PackageOperations(Operations):
Args:
package_base(str): package base name
status(BuildStatus): new build status
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
"""
repository_id = repository_id or self._repository_id
@@ -116,7 +116,7 @@ class SQLite(
Args:
package_base(str): package base to remove
repository_id(RepositoryId, optional): repository unique identifier override (Default value = None)
repository_id(RepositoryId | None, optional): repository unique identifier override (Default value = None)
Examples:
This method completely removes the package from all tables and must be used, e.g. on package removal::
+3 -7
View File
@@ -238,16 +238,12 @@ class PkgbuildParserError(ValueError):
exception raises in case of PKGBUILD parser errors
"""
def __init__(self, reason: str, source: Any = None) -> None:
def __init__(self, reason: str) -> None:
"""
Args:
reason(str): parser error reason
source(Any, optional): source line if available (Default value = None)
"""
message = f"Could not parse PKGBUILD: {reason}"
if source is not None:
message += f", source: `{source}`"
ValueError.__init__(self, message)
ValueError.__init__(self, f"Could not parse PKGBUILD: {reason}")
class PasswordError(ValueError):
@@ -258,7 +254,7 @@ class PasswordError(ValueError):
def __init__(self, details: Any) -> None:
"""
Args:
details(Any); error details
details(Any): error details
"""
ValueError.__init__(self, f"Password error: {details}")
@@ -46,7 +46,7 @@ class Printer:
if not verbose and not prop.is_required:
continue
indent = "\t" * prop.indent
log_fn(f"{indent}{prop.name}{separator}{prop.value}")
log_fn(f"{indent}{prop.name}{separator if prop.separator is None else prop.separator}{prop.value}")
def properties(self) -> list[Property]:
"""
@@ -35,6 +35,7 @@ class RepositoryStatsPrinter(StringPrinter):
def __init__(self, repository_id: RepositoryId, statistics: RepositoryStats) -> None:
"""
Args:
repository_id(RepositoryId): repository unique identifier
statistics(RepositoryStats): repository statistics
"""
StringPrinter.__init__(self, str(repository_id))
@@ -17,30 +17,38 @@
# 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 collections.abc import Iterator
from ahriman.core.formatters.string_printer import StringPrinter
from ahriman.core.tree import Tree
from ahriman.core.utils import full_version, parse_version
from ahriman.models.build_plan import BuildPlan
from ahriman.models.package import Package
from ahriman.models.property import Property
class UpdatePrinter(StringPrinter):
"""
print content of the package update
print content of the packages update
Attributes:
package(Package): remote (new) package object
local_version(str | None): local version of the package if any
plan(BuildPlan): plan to display
local_versions(dict[str, str]): published package versions indexed by base
bump_pkgrel(bool): whether duplicate package releases are expected to increment
"""
def __init__(self, remote: Package, local_version: str | None) -> None:
def __init__(self, plan: BuildPlan, local_versions: dict[str, str], *, bump_pkgrel: bool = True) -> None:
"""
Args:
remote(Package): remote (new) package object
local_version(str | None): local version of the package if any
plan(BuildPlan): plan to display
local_versions(dict[str, str]): published package versions indexed by base
bump_pkgrel(bool, optional): whether duplicate package releases are expected to increment
(Default value = True)
"""
StringPrinter.__init__(self, remote.base)
self.package = remote
self.local_version = local_version
StringPrinter.__init__(self, "Build plan" if plan else "No packages selected")
self.plan = plan
self.local_versions = local_versions
self.bump_pkgrel = bump_pkgrel
def properties(self) -> list[Property]:
"""
@@ -49,9 +57,51 @@ class UpdatePrinter(StringPrinter):
Returns:
list[Property]: list of content properties
"""
if (pkgrel := self.package.next_pkgrel(self.local_version)) is not None:
epoch, pkgver, _ = parse_version(self.package.version)
return [
prop
for level in Tree.resolve(self.plan)
for package in level
for prop in self.properties_package(package)
]
def properties_package(self, package: Package) -> Iterator[Property]:
"""
generate list of properties for the specific package
Args:
package(Package): package object
Yields:
Property: list of properties belonging to the package
"""
# package header
yield Property(package.base, "", is_required=True)
# set version update message
local_version, remote_version = self.versions(package)
yield Property(local_version, remote_version, is_required=True, indent=2, separator=" -> ")
# set reason
item = self.plan.item(package.base)
reason = item.reason.value if item.requester is None else f"{item.reason} {item.requester}"
yield Property("Reason", reason, indent=2)
def versions(self, package: Package) -> tuple[str, str]:
"""
generate version property
Args:
package(Package): package whose current and updated versions are shown
Returns:
tuple[str, str]: extract current and updated versions for the package
"""
local_version = self.local_versions.get(package.base)
if self.bump_pkgrel and (pkgrel := package.next_pkgrel(local_version)) is not None:
epoch, pkgver, _ = parse_version(package.version)
effective_new_version = full_version(epoch, pkgver, pkgrel)
else:
effective_new_version = self.package.version
return [Property(self.local_version or "N/A", effective_new_version, is_required=True)]
effective_new_version = package.version
return local_version or "N/A", effective_new_version
@@ -55,7 +55,7 @@ class RemotePull(LazyLogging):
web_url="",
path=".",
branch=configuration.get(section, "pull_branch", fallback="master"),
source=PackageSource.Local,
source=PackageSource.LOCAL,
)
self.architecture = repository_id.architecture
self.repository_paths = configuration.repository_paths
@@ -62,7 +62,7 @@ class RemotePush(LazyLogging):
web_url="",
path=".",
branch=configuration.get(section, "push_branch", fallback="master"),
source=PackageSource.Local,
source=PackageSource.LOCAL,
)
def package_update(self, package: Package, target_dir: Path) -> str:
@@ -18,4 +18,5 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from ahriman.core.housekeeping.archive_rotation_trigger import ArchiveRotationTrigger
from ahriman.core.housekeeping.events_rotation_trigger import EventsRotationTrigger
from ahriman.core.housekeeping.logs_rotation_trigger import LogsRotationTrigger
@@ -87,7 +87,12 @@ class ArchiveRotationTrigger(Trigger):
if self.keep_built_packages == 0:
return
to_remove = repository.package_archives(package.base)
current = next(iter(repository.packages([package.base])), None)
to_remove = [
single
for single in repository.package_archives(package.base)
if current is None or single.version != current.version
]
for single in to_remove[:-self.keep_built_packages]:
self.logger.info("removing version %s of package %s", single.version, single.base)
@@ -0,0 +1,85 @@
#
# 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 ahriman.core import context
from ahriman.core.configuration import Configuration
from ahriman.core.status import Client
from ahriman.core.triggers import Trigger
from ahriman.models.package import Package
from ahriman.models.repository_id import RepositoryId
from ahriman.models.result import Result
class EventsRotationTrigger(Trigger):
"""
rotate events after build processes
Attributes:
keep_last_records(int): number of last records to keep
"""
CONFIGURATION_SCHEMA = {
"events-rotation": {
"type": "dict",
"schema": {
"keep_last_events": {
"type": "integer",
"required": True,
"coerce": "integer",
"min": 0,
},
},
},
}
def __init__(self, repository_id: RepositoryId, configuration: Configuration) -> None:
"""
Args:
repository_id(RepositoryId): repository unique identifier
configuration(Configuration): configuration instance
"""
Trigger.__init__(self, repository_id, configuration)
section = next(iter(self.configuration_sections(configuration)))
self.keep_last_records = configuration.getint(section, "keep_last_events")
@classmethod
def configuration_sections(cls, configuration: Configuration) -> list[str]:
"""
extract configuration sections from configuration
Args:
configuration(Configuration): configuration instance
Returns:
list[str]: read configuration sections belong to this trigger
"""
return list(cls.CONFIGURATION_SCHEMA.keys())
def on_result(self, result: Result, packages: list[Package]) -> None:
"""
run trigger
Args:
result(Result): build result
packages(list[Package]): list of all available packages
"""
ctx = context.get()
reporter = ctx.get(Client)
reporter.events_rotate(self.keep_last_records)
@@ -81,7 +81,7 @@ class SyncHttpClient(LazyLogging):
get or create session
Returns:
request.Session: created session object
requests.Session: created session object
"""
session = requests.Session()
@@ -52,9 +52,9 @@ class LogLoader:
"""
try to guess default log handler. In case if ``selected`` is set, it will return specified value with appended
_handler suffix. Otherwise, it will try to import journald handler and returns
:attr:`ahriman.models.log_handler.LogHandler.Journald` if library is available. Otherwise, it will check if
there is ``/dev/log`` device and returns :attr:`ahriman.models.log_handler.LogHandler.Syslog` in this
case. And, finally, it will fall back to :attr:`ahriman.models.log_handler.LogHandler.Console`
:attr:`ahriman.models.log_handler.LogHandler.JOURNALD` if library is available. Otherwise, it will check if
there is ``/dev/log`` device and returns :attr:`ahriman.models.log_handler.LogHandler.SYSLOG` in this
case. And, finally, it will fall back to :attr:`ahriman.models.log_handler.LogHandler.CONSOLE`
if none were found
Args:
@@ -67,10 +67,10 @@ class LogLoader:
return selected
if optional_module("systemd.journal"):
return LogHandler.Journald # journald import was found
return LogHandler.JOURNALD # journald import was found
if LogLoader.DEFAULT_SYSLOG_DEVICE.exists():
return LogHandler.Syslog
return LogHandler.Console
return LogHandler.SYSLOG
return LogHandler.CONSOLE
@staticmethod
def load(repository_id: RepositoryId, configuration: Configuration, handler: LogHandler, *,
@@ -107,10 +107,10 @@ class JinjaTemplate:
sort content before rendering
Args:
content(list[dict[str, str]]): content of the template
content(list[dict[str, Any]]): content of the template
Returns:
list[dict[str, str]]: sorted content according to comparator defined
list[dict[str, Any]]: sorted content according to comparator defined
"""
comparator: Callable[[dict[str, str]], Comparable] = lambda item: item["filename"]
return sorted(content, key=comparator)
@@ -122,6 +122,9 @@ class JinjaTemplate:
Args:
result(Result): build result
template_name(Path | str): name of the template or path to it (legacy configuration)
Returns:
str: rendered report
"""
templates = self.templates[:]
if isinstance(template_name, Path):
@@ -155,8 +158,8 @@ class JinjaTemplate:
homepage=self.homepage,
last_update=self.format_datetime(utcnow()),
link_path=self.link_path,
has_package_signed=SignSettings.Packages in self.sign_targets,
has_repo_signed=SignSettings.Repository in self.sign_targets,
has_package_signed=SignSettings.PACKAGES in self.sign_targets,
has_repo_signed=SignSettings.REPOSITORY in self.sign_targets,
packages=self.sort_content(content),
pgp_key=self.default_pgp_key,
repository=self.name,
@@ -17,8 +17,6 @@
# 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 __future__ import annotations
from ahriman.core.configuration import Configuration
from ahriman.core.exceptions import ReportError
from ahriman.core.log import LazyLogging
@@ -81,19 +79,19 @@ class Report(LazyLogging):
case ReportSettings.HTML:
from ahriman.core.report.html import HTML
return HTML(repository_id, configuration, section)
case ReportSettings.Email:
case ReportSettings.EMAIL:
from ahriman.core.report.email import Email
return Email(repository_id, configuration, section)
case ReportSettings.Console:
case ReportSettings.CONSOLE:
from ahriman.core.report.console import Console
return Console(repository_id, configuration, section)
case ReportSettings.Telegram:
case ReportSettings.TELEGRAM:
from ahriman.core.report.telegram import Telegram
return Telegram(repository_id, configuration, section)
case ReportSettings.RSS:
from ahriman.core.report.rss import RSS
return RSS(repository_id, configuration, section)
case ReportSettings.RemoteCall:
case ReportSettings.REMOTE_CALL:
from ahriman.core.report.remote_call import RemoteCall
return RemoteCall(repository_id, configuration, section)
case _:
+3 -3
View File
@@ -82,10 +82,10 @@ class RSS(Report, JinjaTemplate):
sort content before rendering
Args:
content(list[dict[str, str]]): content of the template
content(list[dict[str, Any]]): content of the template
Returns:
list[dict[str, str]]: sorted content according to comparator defined
list[dict[str, Any]]: sorted content according to comparator defined
"""
comparator: Callable[[dict[str, str]], Comparable] = \
lambda item: parsedate_to_datetime(item["build_date"])
@@ -103,7 +103,7 @@ class RSS(Report, JinjaTemplate):
"""
ctx = context.get()
reporter = ctx.get(Client)
events = reporter.event_get(EventType.PackageUpdated, None, limit=self.max_entries)
events = reporter.event_get(EventType.PACKAGE_UPDATED, None, limit=self.max_entries)
known_packages = {package.base: package for package in packages}
@@ -49,7 +49,7 @@ class EventLogger:
This method must be used as simple wrapper for :class:`ahriman.core.status.Client` methods, e.g.::
>>> do_something()
>>> self.event(package_base, EventType.PackageUpdated)
>>> self.event(package_base, EventType.PACKAGE_UPDATED)
"""
self.reporter.event_add(Event(event, package_base, message))
@@ -68,7 +68,7 @@ class EventLogger:
Examples:
This method must be used to perform action in context with time measurement::
>>> with self.in_event(package_base, EventType.PackageUpdated):
>>> with self.in_event(package_base, EventType.PACKAGE_UPDATED):
>>> do_something()
Additional parameter ``failure`` can be set in order to emit an event on exception occurred. If none set
@@ -117,7 +117,7 @@ class Executor(PackageInfo, Cleaner):
package_base(str): package base name
"""
try:
with self.in_event(package_base, EventType.PackageRemoved):
with self.in_event(package_base, EventType.PACKAGE_REMOVED):
self.reporter.package_remove(package_base)
except Exception:
self.logger.exception("could not remove base %s", package_base)
@@ -169,7 +169,7 @@ class Executor(PackageInfo, Cleaner):
with self.in_package_context(single.base, local_versions.get(single.base)), \
TemporaryDirectory(ignore_cleanup_errors=True) as dir_name:
try:
with self.in_event(single.base, EventType.PackageUpdated, failure=EventType.PackageUpdateFailed):
with self.in_event(single.base, EventType.PACKAGE_UPDATED, failure=EventType.PACKAGE_UPDATE_FAILED):
packager = self.packager(packagers, single.base)
local_version = local_versions.get(single.base) if bump_pkgrel else None
commit_sha = self._package_build(single, Path(dir_name), packager.packager_id, local_version)
@@ -57,7 +57,7 @@ class Repository(Executor, UpdateHandler):
@classmethod
def load(cls, repository_id: RepositoryId, configuration: Configuration, database: SQLite, *, report: bool,
refresh_pacman_database: PacmanSynchronization = PacmanSynchronization.Disabled) -> Self:
refresh_pacman_database: PacmanSynchronization = PacmanSynchronization.DISABLED) -> Self:
"""
load instance from argument list
@@ -67,7 +67,7 @@ class Repository(Executor, UpdateHandler):
database(SQLite): database instance
report(bool): force enable or disable reporting
refresh_pacman_database(PacmanSynchronization, optional): pacman database synchronization level
(Default value = PacmanSynchronization.Disabled)
(Default value = PacmanSynchronization.DISABLED)
Returns:
Self: fully loaded repository class instance
@@ -86,13 +86,13 @@ class RepositoryProperties(EventLogger, LazyLogging):
package_base(str): package base to lookup
Returns:
User | None: user found in database if any and empty object otherwise
User: user found in database if any and empty user object otherwise
"""
username = packagers.for_base(package_base)
if username is None: # none to search
return User(username="", password="", access=UserAccess.Read, packager_id=None, key=None) # nosec
return User(username="", password="", access=UserAccess.READ, packager_id=None, key=None) # nosec
if (user := self.database.user_get(username)) is not None: # found user
return user
# empty user with the username
return User(username=username, password="", access=UserAccess.Read, packager_id=None, key=None) # nosec
return User(username=username, password="", access=UserAccess.READ, packager_id=None, key=None) # nosec
@@ -17,6 +17,8 @@
# 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 itertools
from collections.abc import Iterable
from ahriman.core.build_tools.package_version import PackageVersion
@@ -36,6 +38,17 @@ class UpdateHandler(PackageInfo, Cleaner):
trait to get package update list
"""
def _updates_ignored(self) -> set[str]:
"""
collect package bases excluded from automatic updates
Returns:
set[str]: held and configured ignored package bases
"""
return set(self._ignore_list) | {
package.base for package, status in self.reporter.package_get(None) if status.is_held
}
def updates_aur(self, filter_packages: Iterable[str], *, vcs: bool) -> list[Package]:
"""
check AUR for updates
@@ -51,16 +64,14 @@ class UpdateHandler(PackageInfo, Cleaner):
# try to load package from base and if none found try to load by separated packages
for probe in [package.base] + sorted(package.packages.keys()):
try:
if package.remote.source == PackageSource.Repository:
if package.remote.source == PackageSource.REPOSITORY:
return Package.from_official(probe, self.pacman, None)
return Package.from_aur(probe, None)
except UnknownPackageError:
continue
raise UnknownPackageError(package.base)
ignore_list = self._ignore_list + [
package.base for package, status in self.reporter.package_get(None) if status.is_held
]
ignore_list = self._updates_ignored()
result: list[Package] = []
for local in self.packages(filter_packages):
@@ -76,10 +87,10 @@ class UpdateHandler(PackageInfo, Cleaner):
if PackageVersion(local).is_outdated(remote, self.configuration, calculate_version=vcs):
self.reporter.set_pending(local.base)
self.event(local.base, EventType.PackageOutdated, "Remote version is newer than local")
self.event(local.base, EventType.PACKAGE_OUTDATED, "Remote version is newer than local")
result.append(remote)
else:
self.reporter.package_status_update(local.base, BuildStatusEnum.Success)
self.reporter.package_status_update(local.base, BuildStatusEnum.SUCCESS)
except Exception:
self.reporter.set_failed(local.base)
self.logger.exception("could not load remote package %s", local.base)
@@ -106,13 +117,18 @@ class UpdateHandler(PackageInfo, Cleaner):
return files
ignore_list = self._updates_ignored()
result: list[Package] = []
for local in self.packages(filter_packages):
if local.base in ignore_list:
self.logger.info("package %s is held, skip update check", local.base)
continue
dependencies = self.reporter.package_dependencies_get(local.base)
if not dependencies.paths:
continue # skip check if no package dependencies found
required_packages = {dep for dep_packages in dependencies.paths.values() for dep in dep_packages}
required_packages = set(itertools.chain.from_iterable(dependencies.paths.values()))
filesystem = extract_files(required_packages)
for path, packages in dependencies.paths.items():
@@ -122,7 +138,7 @@ class UpdateHandler(PackageInfo, Cleaner):
# there are no packages found in filesystem with the same paths
self.reporter.set_pending(local.base)
self.event(local.base, EventType.PackageOutdated, "Implicit dependencies are broken")
self.event(local.base, EventType.PACKAGE_OUTDATED, "Implicit dependencies are broken")
result.append(local)
break
@@ -141,13 +157,17 @@ class UpdateHandler(PackageInfo, Cleaner):
"""
packages = {local.base: local for local in self.packages()}
local_versions = {package_base: package.version for package_base, package in packages.items()}
ignore_list = self._updates_ignored()
result: list[Package] = []
for cache_dir in self.paths.cache.iterdir():
if cache_dir.name in ignore_list:
self.logger.info("package %s is held, skip update check", cache_dir.name)
continue
with self.in_package_context(cache_dir.name, local_versions.get(cache_dir.name)):
try:
source = RemoteSource(
source=PackageSource.Local,
source=PackageSource.LOCAL,
git_url=cache_dir.absolute().as_uri(),
web_url="",
path=".",
@@ -162,11 +182,15 @@ class UpdateHandler(PackageInfo, Cleaner):
continue # we don't add packages automatically
if local.remote.is_remote:
continue # avoid checking AUR packages
if local.base in ignore_list:
self.logger.info("package %s is held, skip update check", local.base)
continue
if PackageVersion(local).is_outdated(remote, self.configuration, calculate_version=vcs):
self.reporter.set_pending(local.base)
self.event(local.base, EventType.PackageOutdated, "Locally pulled sources are outdated")
self.event(local.base, EventType.PACKAGE_OUTDATED, "Locally pulled sources are outdated")
result.append(remote)
except Exception:
self.logger.exception("could not process package at %s", cache_dir)
@@ -189,9 +213,8 @@ class UpdateHandler(PackageInfo, Cleaner):
self.reporter.set_unknown(local)
else:
self.reporter.set_pending(local.base)
self.event(local.base, EventType.PackageOutdated, "Manual update is requested")
self.event(local.base, EventType.PACKAGE_OUTDATED, "Manual update is requested")
self.clear_queue()
except Exception:
self.logger.exception("could not load packages from database")
+5 -5
View File
@@ -53,7 +53,7 @@ class GPG(SyncHttpClient):
Returns:
list[str]: command line arguments for repo-add command to sign database
"""
if SignSettings.Repository not in self.targets:
if SignSettings.REPOSITORY not in self.targets:
return []
if self.default_key is None:
self.logger.error("no default key set, skip repository sign")
@@ -88,7 +88,7 @@ class GPG(SyncHttpClient):
targets: set[SignSettings] = set()
for option in configuration.getlist("sign", "target", fallback=[]):
target = SignSettings.from_option(option)
if target == SignSettings.Disabled:
if target == SignSettings.DISABLED:
continue
targets.add(target)
default_key = configuration.get("sign", "key") if targets else None
@@ -103,7 +103,7 @@ class GPG(SyncHttpClient):
filepath(Path): path to the file which will be signed
Returns:
str: path to signature file
Path: path to signature file
"""
return filepath.parent / f"{filepath.name}.sig"
@@ -204,7 +204,7 @@ class GPG(SyncHttpClient):
# the file was already signed before, just use its signature
return [path, signature]
if SignSettings.Packages not in self.targets:
if SignSettings.PACKAGES not in self.targets:
return [path]
key = packager_key or self.default_key
@@ -226,7 +226,7 @@ class GPG(SyncHttpClient):
Returns:
list[Path]: list of generated files including original file
"""
if SignSettings.Repository not in self.targets:
if SignSettings.REPOSITORY not in self.targets:
return [path]
if self.default_key is None:
self.logger.error("no default key set, skip repository sign")
+6 -7
View File
@@ -17,13 +17,13 @@
# 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 __future__ import annotations
import argparse
import itertools
import uuid
from collections.abc import Callable, Iterable
from multiprocessing import Process, Queue
from multiprocessing import Process, get_context
from multiprocessing.queues import Queue
from threading import Lock, Thread
from ahriman.core.log import LazyLogging
@@ -57,8 +57,7 @@ class Spawn(Thread, LazyLogging):
self._lock = Lock()
self.active: dict[str, Process] = {}
# stupid pylint does not know that it is possible
self.queue: Queue[ProcessStatus | None] = Queue() # pylint: disable=unsubscriptable-object
self.queue: Queue[ProcessStatus | None] = get_context("spawn").Queue()
@staticmethod
def boolean_action_argument(name: str, value: bool) -> str:
@@ -76,7 +75,7 @@ class Spawn(Thread, LazyLogging):
@staticmethod
def process(callback: Callable[[argparse.Namespace, RepositoryId], bool], args: argparse.Namespace,
repository_id: RepositoryId, process_id: str, queue: Queue[ProcessStatus | None]) -> None: # pylint: disable=unsubscriptable-object
repository_id: RepositoryId, process_id: str, queue: Queue[ProcessStatus | None]) -> None:
"""
helper to run external process
@@ -119,7 +118,7 @@ class Spawn(Thread, LazyLogging):
continue # skip null values
flag = f"--{argument}"
if isinstance(value, list):
arguments.extend(list(sum(((flag, v) for v in value), ())))
arguments.extend(itertools.chain.from_iterable((flag, v) for v in value))
elif value:
arguments.extend([flag, value])
else:
+18 -8
View File
@@ -18,8 +18,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# pylint: disable=too-many-public-methods
from __future__ import annotations
from ahriman.core.configuration import Configuration
from ahriman.core.database import SQLite
from ahriman.models.build_status import BuildStatus, BuildStatusEnum
@@ -120,6 +118,18 @@ class Client:
"""
raise NotImplementedError
def events_rotate(self, keep_last_records: int) -> None:
"""
remove older events from storage
Args:
keep_last_records(int): number of last records to keep
Raises:
NotImplementedError: not implemented method
"""
raise NotImplementedError
def logs_rotate(self, keep_last_records: int) -> None:
"""
remove older logs from storage
@@ -164,7 +174,7 @@ class Client:
package_base(str): package base to retrieve
Returns:
list[Dependencies]: package implicit dependencies if available
Dependencies: package implicit dependencies if available
Raises:
NotImplementedError: not implemented method
@@ -341,7 +351,7 @@ class Client:
Args:
package_base(str): package base to update
"""
self.package_status_update(package_base, BuildStatusEnum.Building)
self.package_status_update(package_base, BuildStatusEnum.BUILDING)
def set_failed(self, package_base: str) -> None:
"""
@@ -350,7 +360,7 @@ class Client:
Args:
package_base(str): package base to update
"""
self.package_status_update(package_base, BuildStatusEnum.Failed)
self.package_status_update(package_base, BuildStatusEnum.FAILED)
def set_pending(self, package_base: str) -> None:
"""
@@ -359,7 +369,7 @@ class Client:
Args:
package_base(str): package base to update
"""
self.package_status_update(package_base, BuildStatusEnum.Pending)
self.package_status_update(package_base, BuildStatusEnum.PENDING)
def set_success(self, package: Package) -> None:
"""
@@ -368,7 +378,7 @@ class Client:
Args:
package(Package): current package properties
"""
self.package_update(package, BuildStatusEnum.Success)
self.package_update(package, BuildStatusEnum.SUCCESS)
def set_unknown(self, package: Package) -> None:
"""
@@ -380,7 +390,7 @@ class Client:
"""
if self.package_get(package.base):
return # skip update in case if package is already known
self.package_update(package, BuildStatusEnum.Unknown)
self.package_update(package, BuildStatusEnum.UNKNOWN)
def statistics(self) -> RepositoryStats:
"""
@@ -75,6 +75,15 @@ class LocalClient(Client):
"""
return self.database.event_get(event, object_id, from_date, to_date, limit, offset, self.repository_id)
def events_rotate(self, keep_last_records: int) -> None:
"""
remove older events from storage
Args:
keep_last_records(int): number of last records to keep
"""
self.database.events_rotate(keep_last_records, self.repository_id)
def logs_rotate(self, keep_last_records: int) -> None:
"""
remove older logs from storage
@@ -114,7 +123,7 @@ class LocalClient(Client):
package_base(str): package base to retrieve
Returns:
list[Dependencies]: package implicit dependencies if available
Dependencies: package implicit dependencies if available
"""
return self.database.dependencies_get(package_base, self.repository_id).get(package_base, Dependencies())
@@ -90,6 +90,15 @@ class Watcher(LazyLogging):
"""
return self.client.event_get(event, object_id, from_date, to_date, limit, offset)
async def events_rotate(self, keep_last_records: int) -> None:
"""
remove older events from storage
Args:
keep_last_records(int): number of last records to keep
"""
self.client.events_rotate(keep_last_records)
async def load(self) -> None:
"""
load packages from local database
@@ -151,7 +160,7 @@ class Watcher(LazyLogging):
package_base(str): package base to retrieve
Returns:
list[Dependencies]: package implicit dependencies if available
Dependencies: package implicit dependencies if available
"""
return self.client.package_dependencies_get(package_base)
@@ -197,7 +206,7 @@ class Watcher(LazyLogging):
self._known[package_base] = (package, replace(status, is_held=enabled))
self.client.package_hold_update(package_base, enabled=enabled)
await self.event_bus.broadcast(EventType.PackageHeld, package_base, is_held=enabled)
await self.event_bus.broadcast(EventType.PACKAGE_HELD, package_base, is_held=enabled)
async def package_logs_add(self, log_record: LogRecord) -> None:
"""
@@ -208,7 +217,7 @@ class Watcher(LazyLogging):
"""
self.client.package_logs_add(log_record)
await self.event_bus.broadcast(EventType.BuildLog, log_record.log_record_id.package_base, **log_record.view())
await self.event_bus.broadcast(EventType.BUILD_LOG, log_record.log_record_id.package_base, **log_record.view())
async def package_logs_get(self, package_base: str, version: str | None = None, process_id: str | None = None,
limit: int = -1, offset: int = 0) -> list[LogRecord]:
@@ -281,7 +290,7 @@ class Watcher(LazyLogging):
self._known.pop(package_base, None)
self.client.package_remove(package_base)
await self.event_bus.broadcast(EventType.PackageRemoved, package_base)
await self.event_bus.broadcast(EventType.PACKAGE_REMOVED, package_base)
async def package_status_update(self, package_base: str, status: BuildStatusEnum) -> None:
"""
@@ -296,7 +305,7 @@ class Watcher(LazyLogging):
self._known[package_base] = (package, BuildStatus(status, is_held=current_status.is_held))
self.client.package_status_update(package_base, status)
await self.event_bus.broadcast(EventType.PackageStatusChanged, package_base, status=status.value)
await self.event_bus.broadcast(EventType.PACKAGE_STATUS_CHANGED, package_base, status=status.value)
async def package_update(self, package: Package, status: BuildStatusEnum) -> None:
"""
@@ -312,7 +321,7 @@ class Watcher(LazyLogging):
self.client.package_update(package, status)
await self.event_bus.broadcast(
EventType.PackageUpdated, package.base, status=status.value, version=package.version,
EventType.PACKAGE_UPDATED, package.base, status=status.value, version=package.version,
)
async def packages(self) -> list[tuple[Package, BuildStatus]]:
@@ -340,7 +349,7 @@ class Watcher(LazyLogging):
"""
self.status = BuildStatus(status)
await self.event_bus.broadcast(EventType.ServiceStatusChanged, None, status=status.value)
await self.event_bus.broadcast(EventType.SERVICE_STATUS_CHANGED, None, status=status.value)
def __call__(self, package_base: str | None) -> Self:
"""
@@ -218,6 +218,18 @@ class WebClient(Client, SyncAhrimanClient):
return []
def events_rotate(self, keep_last_records: int) -> None:
"""
remove older events from storage
Args:
keep_last_records(int): number of last records to keep
"""
query = self.repository_id.query() + [("keep_last_records", str(keep_last_records))]
with contextlib.suppress(Exception):
self.make_request("DELETE", self._events_url(), params=query)
def logs_rotate(self, keep_last_records: int) -> None:
"""
remove older logs from storage
@@ -269,7 +281,7 @@ class WebClient(Client, SyncAhrimanClient):
package_base(str): package base to retrieve
Returns:
list[Dependencies]: package implicit dependencies if available
Dependencies: package implicit dependencies if available
"""
with contextlib.suppress(Exception):
response = self.make_request("GET", self._dependencies_url(package_base),
-2
View File
@@ -17,8 +17,6 @@
# 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 __future__ import annotations
from collections.abc import Iterable
from functools import partial
@@ -57,7 +57,7 @@ class RemoteService(Upload, HttpUpload):
get or create session
Returns:
request.Session: created session object
requests.Session: created session object
"""
return self.client.session
@@ -17,8 +17,6 @@
# 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 __future__ import annotations
from pathlib import Path
from ahriman.core.configuration import Configuration
@@ -79,16 +77,16 @@ class Upload(LazyLogging):
"""
section, provider_name = configuration.gettype(target, repository_id)
match UploadSettings.from_option(provider_name):
case UploadSettings.Rsync:
case UploadSettings.RSYNC:
from ahriman.core.upload.rsync import Rsync
return Rsync(repository_id, configuration, section)
case UploadSettings.S3:
from ahriman.core.upload.s3 import S3
return S3(repository_id, configuration, section)
case UploadSettings.GitHub:
case UploadSettings.GITHUB:
from ahriman.core.upload.github import GitHub
return GitHub(repository_id, configuration, section)
case UploadSettings.RemoteService:
case UploadSettings.REMOTE_SERVICE:
from ahriman.core.upload.remote_service import RemoteService
return RemoteService(repository_id, configuration, section)
case _:
+2 -2
View File
@@ -101,8 +101,8 @@ def check_output(*args: str, exception: Exception | Callable[[int, list[str], st
Args:
*args(str): command line arguments
exception(Exception | Callable[[int, list[str], str, str]] | None, optional): exception which has to be raised
instead of default subprocess exception. If callable us is supplied, the
exception(Exception | Callable[[int, list[str], str, str], Exception] | None, optional): exception to raise
instead of the default subprocess exception. If a callable is supplied, the
:exc:`subprocess.CalledProcessError` arguments will be passed (Default value = None)
cwd(Path | None, optional): current working directory (Default value = None)
input_data(str | None, optional): data which will be written to command stdin (Default value = None)
+6 -6
View File
@@ -25,11 +25,11 @@ class Action(StrEnum):
base action enumeration
Attributes:
List(Action): list available values
Remove(Action): remove everything from local storage
Update(Action): update local storage or add to
LIST(Action): list available values
REMOVE(Action): remove everything from local storage
UPDATE(Action): update local storage or add to
"""
List = "list"
Remove = "remove"
Update = "update"
LIST = "list"
REMOVE = "remove"
UPDATE = "update"
@@ -69,11 +69,13 @@ class AURPackage:
>>>
>>> from ahriman.core.alpm.pacman import Pacman
>>> from ahriman.core.configuration import Configuration
>>> from ahriman.models.pacman_synchronization import PacmanSynchronization
>>> from ahriman.models.repository_id import RepositoryId
>>>
>>> configuration = Configuration()
>>> pacman = Pacman(RepositoryId("x86_64", "aur"), configuration)
>>> metadata = pacman.package_get("pacman")
>>> pacman = Pacman(RepositoryId("x86_64", "aur"), configuration,
>>> refresh_database=PacmanSynchronization.DISABLED)
>>> metadata = pacman.package("pacman")
>>> package = AURPackage.from_pacman(next(metadata)) # load package from pyalpm wrapper
"""
@@ -17,8 +17,6 @@
# 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 __future__ import annotations
from enum import StrEnum
@@ -27,15 +25,15 @@ class AuthSettings(StrEnum):
web authorization type
Attributes:
Disabled(AuthSettings): authorization is disabled
Configuration(AuthSettings): configuration based authorization
OAuth(AuthSettings): OAuth based provider
DISABLED(AuthSettings): authorization is disabled
CONFIGURATION(AuthSettings): configuration based authorization
OAUTH(AuthSettings): OAuth based provider
PAM(AuthSettings): PAM based provider
"""
Disabled = "disabled"
Configuration = "configuration"
OAuth = "oauth2"
DISABLED = "disabled"
CONFIGURATION = "configuration"
OAUTH = "oauth2"
PAM = "pam"
@property
@@ -46,7 +44,7 @@ class AuthSettings(StrEnum):
Returns:
bool: ``False`` in case if authorization is disabled and ``True`` otherwise
"""
return self != AuthSettings.Disabled
return self != AuthSettings.DISABLED
@staticmethod
def from_option(value: str) -> AuthSettings:
@@ -61,10 +59,10 @@ class AuthSettings(StrEnum):
"""
match value.lower():
case "configuration" | "mapping":
return AuthSettings.Configuration
return AuthSettings.CONFIGURATION
case "oauth" | "oauth2":
return AuthSettings.OAuth
return AuthSettings.OAUTH
case "pam":
return AuthSettings.PAM
case _:
return AuthSettings.Disabled
return AuthSettings.DISABLED
@@ -0,0 +1,196 @@
#
# 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 collections.abc import Iterable, Iterator
from dataclasses import dataclass
from enum import StrEnum
from typing import Any, Self
from ahriman.models.package import Package
class BuildPlanReason(StrEnum):
"""
reason why package was selected for a build plan
Attributes:
BROKEN_IMPLICIT_DEPENDENCY(BuildPlanReason): package has a broken implicit dependency
LOCAL_SOURCE_UPDATE(BuildPlanReason): locally sourced package has an update
MANUAL_REQUEST(BuildPlanReason): package was manually queued
PRE_BUILT(BuildPlanReason): package has already been built
REBUILD(BuildPlanReason): package was selected for rebuild
REGULAR_UPDATE(BuildPlanReason): package has an AUR update
REQUIRED_BY(BuildPlanReason): package is required by another selected package
"""
BROKEN_IMPLICIT_DEPENDENCY = "broken implicit dependency"
LOCAL_SOURCE_UPDATE = "local source update"
MANUAL_REQUEST = "manual request"
PRE_BUILT = "pre-built"
REBUILD = "rebuild"
REGULAR_UPDATE = "regular update"
REQUIRED_BY = "required by"
@dataclass(frozen=True)
class BuildPlanItem:
"""
one package and the reason it appears in a build plan
Attributes:
package(Package): package selected for the plan
reason(BuildPlanReason): why the package was selected
requester(str | None): package that requires this package
"""
package: Package
reason: BuildPlanReason
requester: str | None = None
class BuildPlan(Iterable[Package]):
"""
in-memory preview of the packages selected for a build operation
"""
def __init__(self) -> None:
"""
initialize an empty build plan
"""
self._items: dict[str, BuildPlanItem] = {}
@property
def items(self) -> list[BuildPlanItem]:
"""
get plan items in stable package-base order
Returns:
list[BuildPlanItem]: selected plan items
"""
return [self._items[base] for base in sorted(self._items)]
@property
def packages(self) -> list[Package]:
"""
get packages in stable package-base order
Returns:
list[Package]: selected packages
"""
return [item.package for item in self.items]
def add(self, package: Package, reason: BuildPlanReason = BuildPlanReason.MANUAL_REQUEST,
requester: str | None = None) -> BuildPlanItem:
"""
add or update a package while preserving its first reason and requester
Args:
package(Package): package selected for the plan
reason(BuildPlanReason, optional): reason for selecting it (Default value = BuildPlanReason.MANUAL_REQUEST)
requester(str | None, optional): package requiring this dependency (Default value = None)
Returns:
BuildPlanItem: plan item for the package
"""
if (existing := self._items.get(package.base)) is not None:
reason, requester = existing.reason, existing.requester
item = self._items[package.base] = BuildPlanItem(package, reason, requester)
return item
def item(self, package_base: str) -> BuildPlanItem:
"""
get a plan item by package base
Args:
package_base(str): package base to look up
Returns:
BuildPlanItem: matching item
"""
return self._items[package_base]
def remove(self, package_base: str) -> None:
"""
remove a package from the plan
Args:
package_base(str): package base to remove
"""
del self._items[package_base]
def update(self, packages: Iterable[Package], reason: BuildPlanReason = BuildPlanReason.MANUAL_REQUEST,
requester: str | None = None) -> Self:
"""
bulk update packages with the same reason
Args:
packages(Iterable[Package]): list of new packages to be added
reason(BuildPlanReason, optional): reason for selecting it (Default value = BuildPlanReason.MANUAL_REQUEST)
requester(str | None, optional): package requiring these dependencies (Default value = None)
Returns:
Self: updated plan instance
"""
for package in packages:
self.add(package, reason, requester)
return self
def __contains__(self, item: Package) -> bool:
"""
check whether plan holds a package or not
Args:
item(Package): package to lookup
Returns:
bool: ``True`` in case if the package is already in the plan and ``False`` otherwise
"""
return item.base in self._items
def __eq__(self, other: Any) -> bool:
"""
check if other is the same object
Args:
other(Any): other object instance
Returns:
bool: ``True`` if the other object is the same and ``False`` otherwise
"""
if not isinstance(other, BuildPlan):
return False
return self.items == other.items
def __iter__(self) -> Iterator[Package]:
"""
iterate over selected packages in stable package-base order
Returns:
Iterator[Package]: iterator over selected packages
"""
return iter(self.packages)
def __len__(self) -> int:
"""
get number of selected packages
Returns:
int: number of selected packages
"""
return len(self._items)
+11 -11
View File
@@ -29,18 +29,18 @@ class BuildStatusEnum(StrEnum):
build status enumeration
Attributes:
Unknown(BuildStatusEnum): build status is unknown
Pending(BuildStatusEnum): package is out-of-dated and will be built soon
Building(BuildStatusEnum): package is building right now
Failed(BuildStatusEnum): package build failed
Success(BuildStatusEnum): package has been built without errors
UNKNOWN(BuildStatusEnum): build status is unknown
PENDING(BuildStatusEnum): package is out-of-dated and will be built soon
BUILDING(BuildStatusEnum): package is building right now
FAILED(BuildStatusEnum): package build failed
SUCCESS(BuildStatusEnum): package has been built without errors
"""
Unknown = "unknown"
Pending = "pending"
Building = "building"
Failed = "failed"
Success = "success"
UNKNOWN = "unknown"
PENDING = "pending"
BUILDING = "building"
FAILED = "failed"
SUCCESS = "success"
@dataclass(frozen=True)
@@ -54,7 +54,7 @@ class BuildStatus:
is_held(bool | None): whether package held or not
"""
status: BuildStatusEnum = BuildStatusEnum.Unknown
status: BuildStatusEnum = BuildStatusEnum.UNKNOWN
timestamp: int = field(default_factory=lambda: int(utcnow().timestamp()))
is_held: bool | None = field(default=None, kw_only=True)
+17 -17
View File
@@ -28,24 +28,24 @@ class EventType(StrEnum):
predefined event types
Attributes:
BuildLog(EventType): new build log line
PackageHeld(EventType): package hold status has been changed
PackageOutdated(EventType): package has been marked as out-of-date
PackageRemoved(EventType): package has been removed
PackageStatusChanged(EventType): package build status has been changed
PackageUpdateFailed(EventType): package update has been failed
PackageUpdated(EventType): package has been updated
ServiceStatusChanged(EventType): service status has been changed
BUILD_LOG(EventType): new build log line
PACKAGE_HELD(EventType): package hold status has been changed
PACKAGE_OUTDATED(EventType): package has been marked as out-of-date
PACKAGE_REMOVED(EventType): package has been removed
PACKAGE_STATUS_CHANGED(EventType): package build status has been changed
PACKAGE_UPDATE_FAILED(EventType): package update has been failed
PACKAGE_UPDATED(EventType): package has been updated
SERVICE_STATUS_CHANGED(EventType): service status has been changed
"""
BuildLog = "build-log"
PackageHeld = "package-held"
PackageOutdated = "package-outdated"
PackageRemoved = "package-removed"
PackageStatusChanged = "package-status-changed"
PackageUpdateFailed = "package-update-failed"
PackageUpdated = "package-updated"
ServiceStatusChanged = "service-status-changed"
BUILD_LOG = "build-log"
PACKAGE_HELD = "package-held"
PACKAGE_OUTDATED = "package-outdated"
PACKAGE_REMOVED = "package-removed"
PACKAGE_STATUS_CHANGED = "package-status-changed"
PACKAGE_UPDATE_FAILED = "package-update-failed"
PACKAGE_UPDATED = "package-updated"
SERVICE_STATUS_CHANGED = "service-status-changed"
class Event:
@@ -61,7 +61,7 @@ class Event:
"""
def __init__(self, event: str | EventType, object_id: str, message: str | None = None, created: int | None = None,
**kwargs: Any):
**kwargs: Any) -> None:
"""
Args:
event(str | EventType): event type
@@ -17,8 +17,6 @@
# 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 __future__ import annotations
from collections.abc import Iterable
from dataclasses import dataclass, field
from pathlib import Path
@@ -34,7 +32,7 @@ class FilesystemPackage:
Attributes:
package_name(str): package name
depends(set[str]): list of package dependencies
directories(set[Path]): list of directories this package contains
directories(list[Path]): list of directories this package contains
files(list[Path]): list of files this package contains
opt_depends(set[str]): list of package optional dependencies
"""
@@ -80,7 +78,8 @@ class FilesystemPackage:
include_optional(bool): include optional dependencies to check
Returns:
bool: whether this package depends on any other package in the list of packages
bool: ``True`` if this package has no dependencies among the supplied packages, ignoring packages
that have a required dependency on this package
"""
return not any(
package
@@ -25,11 +25,11 @@ class LogHandler(StrEnum):
log handler as described by default configuration
Attributes:
Console(LogHandler): write logs to console
Syslog(LogHandler): write logs to syslog device /dev/null
Journald(LogHandler): write logs to journald directly
CONSOLE(LogHandler): write logs to console
SYSLOG(LogHandler): write logs to syslog device /dev/null
JOURNALD(LogHandler): write logs to journald directly
"""
Console = "console"
Syslog = "syslog"
Journald = "journald"
CONSOLE = "console"
SYSLOG = "syslog"
JOURNALD = "journald"
@@ -50,13 +50,8 @@ class LogRecord:
Returns:
Self: log record object
"""
if "process_id" in dump:
log_record_id = LogRecordId(package_base, dump["version"], dump["process_id"])
else:
log_record_id = LogRecordId(package_base, dump["version"])
return cls(
log_record_id=log_record_id,
log_record_id=LogRecordId(package_base, dump["version"], dump.get("process_id", "")),
created=dump["created"],
message=dump["message"],
)
@@ -38,9 +38,9 @@ class MetricsTimer:
>>> with MetricsTimer() as timer:
>>> do_something()
>>> print("Time elapsed for first function: %f", timer.elapsed)
>>> print(f"Time elapsed for first function: {timer.elapsed:f}")
>>> do_something_different()
>>> print("Time elapsed for all functions: %f", timer.elapsed)
>>> print(f"Time elapsed for all functions: {timer.elapsed:f}")
"""
def __init__(self) -> None:
+9 -11
View File
@@ -17,8 +17,6 @@
# 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 __future__ import annotations
from collections.abc import Iterable
from dataclasses import dataclass
from pathlib import Path
@@ -44,8 +42,8 @@ class Package(LazyLogging):
Attributes:
base(str): package base name
packager(str | None): package packager if available
packages(dict[str, PackageDescription): map of package names to their properties.
Filled only on load from archive
packages(dict[str, PackageDescription]): map of package names to their properties.
Available properties depend on the source used to load the package
remote(RemoteSource): package remote source if applicable
version(str): package full version
@@ -56,16 +54,16 @@ class Package(LazyLogging):
it will contain every data available in the JSON body. Otherwise, if generate package from local archive::
>>> package = Package.from_archive(local_path, pacman)
>>> package = Package.from_archive(local_path)
it will probably miss file descriptions (in case if there are multiple packages which belong to the base).
The specific class load method must be defined based on the source provided. The following methods (mostly) must
be used: :func:`from_archive()`, :func:`from_aur()`, :func:`from_build()`, :func:`from_official()` for sources
:attr:`ahriman.models.package_source.PackageSource.Archive`,
:attr:`ahriman.models.package_source.PackageSource.ARCHIVE`,
:attr:`ahriman.models.package_source.PackageSource.AUR`,
:attr:`ahriman.models.package_source.PackageSource.Local` and
:attr:`ahriman.models.package_source.PackageSource.Repository` respectively:
:attr:`ahriman.models.package_source.PackageSource.LOCAL` and
:attr:`ahriman.models.package_source.PackageSource.REPOSITORY` respectively:
>>> ahriman_package = Package.from_aur("ahriman")
>>> pacman_package = Package.from_official("pacman", pacman)
@@ -192,7 +190,7 @@ class Package(LazyLogging):
return cls(
base=package.base or package.name,
version=package.version,
remote=RemoteSource(source=PackageSource.Archive),
remote=RemoteSource(source=PackageSource.ARCHIVE),
packages={package.name: description},
packager=package.packager,
)
@@ -255,7 +253,7 @@ class Package(LazyLogging):
version = full_version(pkgbuild.get("epoch"), pkgbuild["pkgver"], pkgbuild["pkgrel"])
remote = RemoteSource(
source=PackageSource.Local,
source=PackageSource.LOCAL,
git_url=path.absolute().as_uri(),
web_url=None,
path=".",
@@ -315,7 +313,7 @@ class Package(LazyLogging):
package = impl.info(name, pacman=pacman, include_provides=include_provides)
remote = RemoteSource(
source=PackageSource.Repository,
source=PackageSource.REPOSITORY,
git_url=Official.remote_git_url(package.package_base, package.repository),
web_url=Official.remote_web_url(package.package_base),
path=".",
@@ -57,11 +57,13 @@ class PackageDescription:
>>> from pathlib import Path
>>> from ahriman.core.alpm.pacman import Pacman
>>> from ahriman.core.configuration import Configuration
>>> from ahriman.models.pacman_synchronization import PacmanSynchronization
>>> from ahriman.models.repository_id import RepositoryId
>>>
>>> configuration = Configuration()
>>> pacman = Pacman(RepositoryId("x86_64", "aur"), configuration)
>>> pyalpm_description = next(package for package in pacman.package_get("pacman"))
>>> pacman = Pacman(RepositoryId("x86_64", "aur"), configuration,
>>> refresh_database=PacmanSynchronization.DISABLED)
>>> pyalpm_description = next(package for package in pacman.package("pacman"))
>>> description = PackageDescription.from_package(
>>> pyalpm_description, Path("/var/cache/pacman/pkg/pacman-6.0.1-4-x86_64.pkg.tar.zst"))
"""
@@ -17,8 +17,6 @@
# 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 __future__ import annotations
from enum import StrEnum
from pathlib import Path
from urllib.parse import urlparse
@@ -32,31 +30,31 @@ class PackageSource(StrEnum):
package source for addition enumeration
Attributes:
Auto(PackageSource): automatically determine type of the source
Archive(PackageSource): source is a package archive
AUTO(PackageSource): automatically determine type of the source
ARCHIVE(PackageSource): source is a package archive
AUR(PackageSource): source is an AUR package for which it should search
Directory(PackageSource): source is a directory which contains packages
Local(PackageSource): source is locally stored PKGBUILD
Remote(PackageSource): source is remote (http, ftp etc...) link
Repository(PackageSource): source is official repository
DIRECTORY(PackageSource): source is a directory which contains packages
LOCAL(PackageSource): source is locally stored PKGBUILD
REMOTE(PackageSource): source is remote (http, ftp etc...) link
REPOSITORY(PackageSource): source is official repository
Examples:
In case if source is unknown the :func:`resolve()` and the source
descriptor is available method must be used::
>>> real_source = PackageSource.Auto.resolve("ahriman", configuration.repository_paths)
>>> real_source = PackageSource.AUTO.resolve("ahriman", configuration.repository_paths)
the code above will ensure that the presudo-source :attr:`Auto`
the code above will ensure that the presudo-source :attr:`AUTO`
will not be processed later.
"""
Auto = "auto"
Archive = "archive"
AUTO = "auto"
ARCHIVE = "archive"
AUR = "aur"
Directory = "directory"
Local = "local"
Remote = "remote"
Repository = "repository"
DIRECTORY = "directory"
LOCAL = "local"
REMOTE = "remote"
REPOSITORY = "repository"
def resolve(self, source: str, paths: RepositoryPaths) -> PackageSource:
"""
@@ -69,21 +67,21 @@ class PackageSource(StrEnum):
Returns:
PackageSource: non-auto type of the package source
"""
if self != PackageSource.Auto:
if self != PackageSource.AUTO:
return self
maybe_url = urlparse(source) # handle file:// like paths
maybe_path = Path(maybe_url.path)
if maybe_url.scheme and maybe_url.scheme not in ("data", "file") and package_like(maybe_path):
return PackageSource.Remote
return PackageSource.REMOTE
try:
if (maybe_path / "PKGBUILD").is_file() or paths.cache_for(source).is_dir():
return PackageSource.Local
return PackageSource.LOCAL
if maybe_path.is_dir():
return PackageSource.Directory
return PackageSource.DIRECTORY
if maybe_path.is_file() and package_like(maybe_path):
return PackageSource.Archive
return PackageSource.ARCHIVE
except PermissionError:
# in some cases (e.g. if you run from your home directory with sudo)
# it will try to read files to which it has no access.
+1 -1
View File
@@ -27,7 +27,7 @@ class Packagers:
Attributes:
default(str | None): default packager username if any to be used if no override for the specified base was found
overrides: dict[str, str | None]: packager username override for specific package base
overrides(dict[str, str | None]): packager username override for specific package base
"""
default: str | None = None
@@ -25,11 +25,11 @@ class PacmanSynchronization(IntEnum):
pacman database synchronization flag
Attributes:
Disabled(PacmanSynchronization): do not synchronize local database
Enabled(PacmanSynchronization): synchronize local database (same as pacman -Sy)
Force(PacmanSynchronization): force synchronize local database (same as pacman -Syy)
DISABLED(PacmanSynchronization): do not synchronize local database
ENABLED(PacmanSynchronization): synchronize local database (same as pacman -Sy)
FORCE(PacmanSynchronization): force synchronize local database (same as pacman -Syy)
"""
Disabled = 0
Enabled = 1
Force = 2
DISABLED = 0
ENABLED = 1
FORCE = 2
+12 -27
View File
@@ -19,12 +19,12 @@
#
from collections.abc import Iterator, Mapping
from dataclasses import dataclass
from io import StringIO
from io import BytesIO
from pathlib import Path
from typing import Any, ClassVar, IO, Self
from typing import Any, IO, Self
from urllib.parse import urlparse
from ahriman.core.alpm.pkgbuild_parser import PkgbuildParser, PkgbuildToken
from ahriman.core.alpm.pkgbuild_parser import PkgbuildParser
from ahriman.core.utils import srcinfo_property_list
from ahriman.models.pkgbuild_patch import PkgbuildPatch
@@ -35,14 +35,11 @@ class Pkgbuild(Mapping[str, Any]):
model and proxy for PKGBUILD properties
Attributes:
DEFAULT_ENCODINGS(str): (class attribute) default encoding to be applied on the file content
fields(dict[str, PkgbuildPatch]): PKGBUILD fields
"""
fields: dict[str, PkgbuildPatch]
DEFAULT_ENCODINGS: ClassVar[str] = "utf8"
@property
def variables(self) -> dict[str, str]:
"""
@@ -59,43 +56,31 @@ class Pkgbuild(Mapping[str, Any]):
}
@classmethod
def from_file(cls, path: Path, encoding: str | None = None) -> Self:
def from_file(cls, path: Path) -> Self:
"""
parse PKGBUILD from the file
Args:
path(Path): path to the PKGBUILD file
encoding(str | None, optional): the encoding of the file (Default value = None)
Returns:
Self: constructed instance of self
Raises:
EncodeError: if encoding is unknown
"""
# read raw bytes from file
with path.open("rb") as input_file:
content = input_file.read()
# decode bytes content based on either
encoding = encoding or cls.DEFAULT_ENCODINGS
io = StringIO(content.decode(encoding, errors="backslashreplace"))
return cls.from_io(io)
return cls.from_io(input_file)
@classmethod
def from_io(cls, stream: IO[str]) -> Self:
def from_io(cls, stream: IO[bytes]) -> Self:
"""
parse PKGBUILD from input stream
Args:
stream(IO[str]): input stream containing PKGBUILD content
stream(IO[bytes]): input stream containing PKGBUILD content
Returns:
Self: constructed instance of self
"""
parser = PkgbuildParser(stream)
fields = {patch.key: patch for patch in parser.parse()}
fields = {patch.key: patch for patch in PkgbuildParser(stream)}
# pkgbase is optional field, the pkgname must be used instead if not set
# however, pkgname is not presented is "package()" functions which we are parsing here too,
@@ -162,10 +147,10 @@ class Pkgbuild(Mapping[str, Any]):
"""
packages = [self["pkgname"]] if isinstance(self["pkgname"], str) else self["pkgname"]
def io(package_name: str) -> IO[str]:
def io(package_name: str) -> IO[bytes]:
# try to read package specific function and fallback to default otherwise
content = self.get(f"package_{package_name}") or self.get("package") or ""
return StringIO(content)
return BytesIO(content.encode("utf8"))
return {package: self.from_io(io(package)) for package in packages}
@@ -185,8 +170,8 @@ class Pkgbuild(Mapping[str, Any]):
"""
value = self.fields.get(item)
# if the key wasn't found and user didn't ask for function explicitly, we can try to get by function name
if value is None and not item.endswith(PkgbuildToken.FunctionDeclaration):
value = self.fields.get(f"{item}{PkgbuildToken.FunctionDeclaration}")
if value is None and not item.endswith("()"):
value = self.fields.get(f"{item}()")
# if we still didn't find anything, we can just raise the exception
if value is None:
@@ -20,9 +20,10 @@
import json
import shlex
from collections.abc import Iterator
from dataclasses import dataclass, fields
from pathlib import Path
from typing import Any, Iterator, Self
from typing import Any, Self
from ahriman.core.configuration.shell_template import ShellTemplate
from ahriman.core.utils import dataclass_view, filter_json
@@ -108,7 +109,7 @@ class PkgbuildPatch:
Args:
key(str | None): variable key
source(str): source value string to parse
source(str | list[str]): source string to parse or list to use as is
Returns:
Self: parsed patch object
@@ -31,9 +31,11 @@ class Property:
value(Any): property value
is_required(bool): if set to ``True`` then this property is required
indent(int): property indentation level
separator(str | None): optional override for separator
"""
name: str
value: Any
is_required: bool = field(default=False, kw_only=True)
indent: int = 1
separator: str | None = None
@@ -59,7 +59,7 @@ class RemoteSource:
Returns:
bool: ``True`` in case if package is well-known remote source (e.g. AUR) and ``False`` otherwise
"""
return self.source in (PackageSource.AUR, PackageSource.Repository)
return self.source in (PackageSource.AUR, PackageSource.REPOSITORY)
@property
def pkgbuild_dir(self) -> Path | None:
@@ -17,8 +17,6 @@
# 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 __future__ import annotations
from enum import StrEnum
@@ -27,22 +25,22 @@ class ReportSettings(StrEnum):
report targets enumeration
Attributes:
Disabled(ReportSettings): option which generates no report for testing purpose
DISABLED(ReportSettings): option which generates no report for testing purpose
HTML(ReportSettings): HTML report generation
Email(ReportSettings): email report generation
Console(ReportSettings): print result to console
Telegram(ReportSettings): Markdown report to telegram channel
EMAIL(ReportSettings): email report generation
CONSOLE(ReportSettings): print result to console
TELEGRAM(ReportSettings): Markdown report to telegram channel
RSS(ReportSettings): RSS report generation
RemoteCall(ReportSettings): remote ahriman server call
REMOTE_CALL(ReportSettings): remote ahriman server call
"""
Disabled = "disabled" # for testing purpose
DISABLED = "disabled" # for testing purpose
HTML = "html"
Email = "email"
Console = "console"
Telegram = "telegram"
EMAIL = "email"
CONSOLE = "console"
TELEGRAM = "telegram"
RSS = "rss"
RemoteCall = "remote-call"
REMOTE_CALL = "remote-call"
@staticmethod
def from_option(value: str) -> ReportSettings: # pylint: disable=too-many-return-statements
@@ -59,14 +57,14 @@ class ReportSettings(StrEnum):
case "html":
return ReportSettings.HTML
case "email":
return ReportSettings.Email
return ReportSettings.EMAIL
case "console":
return ReportSettings.Console
return ReportSettings.CONSOLE
case "telegram":
return ReportSettings.Telegram
return ReportSettings.TELEGRAM
case "rss":
return ReportSettings.RSS
case "ahriman" | "remote-call":
return ReportSettings.RemoteCall
return ReportSettings.REMOTE_CALL
case _:
return ReportSettings.Disabled
return ReportSettings.DISABLED
@@ -56,7 +56,7 @@ class RepositoryStats:
construct statistics from list of repository packages
Args:
packages(list[Packages]): list of repository packages
packages(list[Package]): list of repository packages
Returns:
Self: constructed statistics object
Loaded 100 of 327 files, more files were not shown because too many files have changed in this diff. Show more