From b359036d1df5243b3440d0144d0817643ba6625b Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Mon, 7 Oct 2024 21:54:59 +0200 Subject: [PATCH] Avoid raw print. --- changelogs/fragments/630-stderr.yml | 2 ++ src/antsibull/build_ansible_commands.py | 12 ++++++++---- src/antsibull/build_changelog.py | 3 ++- src/antsibull/changelog.py | 20 +++++++++++++------- 4 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 changelogs/fragments/630-stderr.yml diff --git a/changelogs/fragments/630-stderr.yml b/changelogs/fragments/630-stderr.yml new file mode 100644 index 00000000..10526c24 --- /dev/null +++ b/changelogs/fragments/630-stderr.yml @@ -0,0 +1,2 @@ +minor_changes: + - "Prefer stderr for error messages during building Ansible, and the logging facility for warnings in changelog related code (https://github.com/ansible-community/antsibull/pull/630)." diff --git a/src/antsibull/build_ansible_commands.py b/src/antsibull/build_ansible_commands.py index 46ce69cd..77e45cd8 100644 --- a/src/antsibull/build_ansible_commands.py +++ b/src/antsibull/build_ansible_commands.py @@ -443,7 +443,8 @@ def prepare_command() -> int: print( f"{build_filename} is for version {build_ansible_version} but we need" f' {app_ctx.extra["ansible_version"].major}' - f'.{app_ctx.extra["ansible_version"].minor}' + f'.{app_ctx.extra["ansible_version"].minor}', + file=sys.stderr, ) return 1 @@ -481,7 +482,8 @@ def prepare_command() -> int: except ValueError as exc: print( "Error while validating existing dependencies against" - f" build version ranges and constraints: {exc}" + f" build version ranges and constraints: {exc}", + file=sys.stderr, ) return 1 @@ -649,9 +651,11 @@ def rebuild_single_command() -> int: if dep_errors: is_error = app_ctx.extra["ansible_version"] >= PypiVer("6.3.0") warning_error = "ERROR" if is_error else "WARNING" - print(f"{warning_error}: found collection dependency errors!") + print( + f"{warning_error}: found collection dependency errors!", file=sys.stderr + ) for error in dep_errors: - print(f"{warning_error}: {error}") + print(f"{warning_error}: {error}", file=sys.stderr) if is_error: return 3 diff --git a/src/antsibull/build_changelog.py b/src/antsibull/build_changelog.py index 2c3df2ba..12ac3053 100644 --- a/src/antsibull/build_changelog.py +++ b/src/antsibull/build_changelog.py @@ -748,6 +748,7 @@ def _get_changelog_bytes(changelog: Changelog, text_format: TextFormat) -> bytes def _append_core_porting_guide_bytes( renderer: AbstractRenderer, changelog: Changelog ) -> None: + flog = mlog.fields(func="_append_core_porting_guide_bytes") core_porting_guide = changelog.core_collector.porting_guide if core_porting_guide: lines = core_porting_guide.decode("utf-8").splitlines() @@ -766,7 +767,7 @@ def _append_core_porting_guide_bytes( continue append_lines.append(line) if not found_empty: - print("WARNING: cannot find TOC of ansible-core porting guide!") + flog.warning("Cannot find TOC of ansible-core porting guide!") if append_lines: renderer.add_text( "\n".join(append_lines), text_format=TextFormat.RESTRUCTURED_TEXT diff --git a/src/antsibull/changelog.py b/src/antsibull/changelog.py index 75f7ec14..c4b10fcc 100644 --- a/src/antsibull/changelog.py +++ b/src/antsibull/changelog.py @@ -34,6 +34,7 @@ from antsibull_core.ansible_core import get_ansible_core from antsibull_core.dependency_files import DependencyFileData, DepsFile from antsibull_core.galaxy import CollectionDownloader, GalaxyContext +from antsibull_core.logging import log from antsibull_core.schemas.collection_meta import ( CollectionsMetadata, RemovalInformation, @@ -47,6 +48,8 @@ from packaging.version import Version as PypiVer from semantic_version import Version as SemVer +mlog = log.fields(mod=__name__) + class ChangelogData: """ @@ -198,6 +201,7 @@ def __init__(self, collection: str, versions: t.ValuesView[str]): async def _get_changelog( self, version: SemVer, collection_downloader: CollectionDownloader ) -> ChangelogData | None: + flog = mlog.fields(func="_get_changelog") path = await collection_downloader.download(self.collection, version) changelog_bytes = read_changelog_file(path) if changelog_bytes is None: @@ -208,8 +212,8 @@ async def _get_changelog( self.collection, str(version), changelog_data ) except Exception as exc: # pylint: disable=broad-except - print( - f"WARNING: cannot load changelog of {self.collection} {version} due to {exc}" + flog.warning( + f"Cannot load changelog of {self.collection} {version} due to {exc}" ) return None @@ -727,6 +731,7 @@ def _populate_ansible_changelog( collection_metadata: CollectionsMetadata, ansible_version: PypiVer, ) -> None: + flog = mlog.fields(func="_populate_ansible_changelog") for collection, metadata in collection_metadata.collections.items(): if metadata.removal: fragment_version = _get_removal_entry( @@ -737,8 +742,8 @@ def _populate_ansible_changelog( if version in ansible_changelog.changes.releases: ansible_changelog.changes.add_fragment(fragment, version) else: - print( - f"WARNING: found changelog entry for {version}, which does not yet exist" + flog.warning( + f"Found changelog entry for {version}, which does not yet exist" ) for collection, removed_metadata in collection_metadata.removed_collections.items(): @@ -750,8 +755,8 @@ def _populate_ansible_changelog( if version in ansible_changelog.changes.releases: ansible_changelog.changes.add_fragment(fragment, version) else: - print( - f"WARNING: found changelog entry for {version}, which does not yet exist" + flog.warning( + f"Found changelog entry for {version}, which does not yet exist" ) @@ -763,6 +768,7 @@ def get_changelog( ansible_changelog: ChangelogData | None = None, galaxy_context: GalaxyContext | None = None, ) -> Changelog: + flog = mlog.fields(func="get_changelog") dependencies: dict[str, DependencyFileData] = {} ansible_changelog = ansible_changelog or ChangelogData.ansible(directory=deps_dir) @@ -781,7 +787,7 @@ def get_changelog( deps.deps.pop("_python", None) version = PypiVer(deps.ansible_version) if version > ansible_version: - print( + flog.info( f"Ignoring {path}, since {deps.ansible_version}" f" is newer than {ansible_version}" )