Skip to content

Commit

Permalink
Avoid raw print.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Oct 7, 2024
1 parent afa7af1 commit b359036
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/630-stderr.yml
Original file line number Diff line number Diff line change
@@ -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)."
12 changes: 8 additions & 4 deletions src/antsibull/build_ansible_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion src/antsibull/build_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
20 changes: 13 additions & 7 deletions src/antsibull/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
"""
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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():
Expand All @@ -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"
)


Expand All @@ -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)
Expand All @@ -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}"
)
Expand Down

0 comments on commit b359036

Please sign in to comment.