feat: suppress info log during vcs version calculation

This commit is contained in:
Evgenii Alekseev 2024-09-25 03:46:33 +03:00
parent b357c96204
commit 528d7ce398
3 changed files with 45 additions and 12 deletions

View File

@ -99,3 +99,24 @@ class LazyLogging:
yield yield
finally: finally:
self._package_logger_reset() self._package_logger_reset()
@contextlib.contextmanager
def suppress_logging(self, log_level: int = logging.WARNING) -> Generator[None, None, None]:
"""
silence log messages in context
Args:
log_level(int, optional): the highest log level to keep (Default value = logging.WARNING)
Examples:
This function is designed to be used to suppress all log messages in context, e.g.:
>>> with self.suppress_logging():
>>> do_some_noisy_actions()
"""
current_level = self.logger.manager.disable
try:
logging.disable(log_level)
yield
finally:
logging.disable(current_level)

View File

@ -429,13 +429,14 @@ class Package(LazyLogging):
task = Task(self, configuration, repository_id.architecture, paths) task = Task(self, configuration, repository_id.architecture, paths)
try: try:
# create fresh chroot environment, fetch sources and - automagically - update PKGBUILD with self.suppress_logging():
task.init(paths.cache_for(self.base), [], None) # create fresh chroot environment, fetch sources and - automagically - update PKGBUILD
task.build(paths.cache_for(self.base), dry_run=True) task.init(paths.cache_for(self.base), [], None)
task.build(paths.cache_for(self.base), dry_run=True)
pkgbuild = Pkgbuild.from_file(paths.cache_for(self.base) / "PKGBUILD") pkgbuild = Pkgbuild.from_file(paths.cache_for(self.base) / "PKGBUILD")
return full_version(pkgbuild.get("epoch"), pkgbuild["pkgver"], pkgbuild["pkgrel"]) return full_version(pkgbuild.get("epoch"), pkgbuild["pkgver"], pkgbuild["pkgrel"])
except Exception: except Exception:
self.logger.exception("cannot determine version of VCS package") self.logger.exception("cannot determine version of VCS package")
finally: finally:

View File

@ -2,6 +2,7 @@ import logging
import pytest import pytest
from pytest_mock import MockerFixture from pytest_mock import MockerFixture
from unittest.mock import call as MockCall
from ahriman.core.alpm.repo import Repo from ahriman.core.alpm.repo import Repo
from ahriman.core.build_tools.task import Task from ahriman.core.build_tools.task import Task
@ -10,6 +11,17 @@ from ahriman.models.log_record_id import LogRecordId
from ahriman.models.package import Package from ahriman.models.package import Package
def test_logger(database: SQLite, repo: Repo) -> None:
"""
must set logger attribute
"""
assert database.logger
assert database.logger.name == "sql"
assert repo.logger
assert repo.logger.name == "ahriman.core.alpm.repo.Repo"
def test_logger_name(database: SQLite, repo: Repo, task_ahriman: Task) -> None: def test_logger_name(database: SQLite, repo: Repo, task_ahriman: Task) -> None:
""" """
must correctly generate logger name must correctly generate logger name
@ -77,12 +89,11 @@ def test_in_package_context_failed(database: SQLite, package_ahriman: Package, m
reset_mock.assert_called_once_with() reset_mock.assert_called_once_with()
def test_logger(database: SQLite, repo: Repo) -> None: def test_suppress_logging(database: SQLite, mocker: MockerFixture) -> None:
""" """
must set logger attribute must temporary disable log messages
""" """
assert database.logger disable_mock = mocker.patch("ahriman.core.log.lazy_logging.logging.disable")
assert database.logger.name == "sql" with database.suppress_logging():
pass
assert repo.logger disable_mock.assert_has_calls([MockCall(logging.WARNING), MockCall(logging.NOTSET)])
assert repo.logger.name == "ahriman.core.alpm.repo.Repo"