Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check dependencies #416

Merged
merged 5 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelogs/fragments/416-deps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
minor_changes:
- "Add ``antsibull-build`` subcommand ``validate-deps`` which validates dependencies for an ``ansible_collections`` tree (https://github.com/ansible-community/antsibull/pull/416)."
- "Check collection dependencies during ``antsibull-build rebuild-single`` and warn about errors (https://github.com/ansible-community/antsibull/pull/416)."
9 changes: 9 additions & 0 deletions src/antsibull/build_ansible_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

from .build_changelog import ReleaseNotes
from .changelog import ChangelogData, get_changelog
from .dep_closure import check_collection_dependencies
from .utils.get_pkg_data import get_antsibull_data


Expand Down Expand Up @@ -447,6 +448,14 @@ def rebuild_single_command() -> int:
else:
make_dist_with_wheels(package_dir, app_ctx.extra['sdist_dir'])

# Check dependencies
dep_errors = check_collection_dependencies(os.path.join(package_dir, 'ansible_collections'))

if dep_errors:
print('WARNING: found collection dependency errors!')
for error in dep_errors:
print(error)

return 0


Expand Down
12 changes: 12 additions & 0 deletions src/antsibull/cli/antsibull_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
prepare_command, build_single_command, build_multiple_command, rebuild_single_command,
)
from ..build_changelog import build_changelog # noqa: E402
from ..dep_closure import validate_dependencies_command # noqa: E402
from ..new_ansible import new_ansible_command # noqa: E402
# pylint: enable=wrong-import-position

Expand All @@ -45,6 +46,7 @@
'collection': build_collection_command,
'changelog': build_changelog,
'rebuild-single': rebuild_single_command,
'validate-deps': validate_dependencies_command,
# Old names, deprecated
'new-acd': new_ansible_command,
'build-single': build_single_command,
Expand Down Expand Up @@ -84,6 +86,9 @@ def _normalize_commands(args: argparse.Namespace) -> None:


def _normalize_build_options(args: argparse.Namespace) -> None:
if args.command in ('validate-deps', ):
return

if not os.path.isdir(args.data_dir):
raise InvalidArgumentError(f'{args.data_dir} must be an existing directory')

Expand Down Expand Up @@ -300,6 +305,13 @@ def parse_args(program_name: str, args: List[str]) -> argparse.Namespace:
parents=[build_write_data_parser, cache_parser],
description='Build the Ansible changelog')

validate_deps = subparsers.add_parser('validate-deps',
description='Validate collection dependencies')

validate_deps.add_argument('collection_root',
help='Path to a ansible_collections directory containing a'
' collection tree to check.')

# Backwards compat
subparsers.add_parser('new-acd', add_help=False, parents=[new_parser])
subparsers.add_parser('build-single', add_help=False, parents=[build_single_parser])
Expand Down
85 changes: 85 additions & 0 deletions src/antsibull/dep_closure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# coding: utf-8
# Author: Toshio Kuratomi <[email protected]>
# License: GPLv3+
# Copyright: Ansible Project, 2021
"""Check collection dependencies."""

import json
import pathlib

from collections import namedtuple
from typing import Dict, List, Mapping

from semantic_version import Version as SemVer, SimpleSpec as SemVerSpec

from antsibull_core import app_context


CollectionRecord = namedtuple('CollectionRecord', ('version', 'dependencies'))


def parse_manifest(collection_dir: pathlib.Path) -> Mapping[str, CollectionRecord]:
'''Parse MANIFEST.json for a collection.'''
manifest = collection_dir.joinpath('MANIFEST.json')
with manifest.open() as f:
manifest_data = json.load(f)['collection_info']

collection_record = {
f'{manifest_data["namespace"]}.{manifest_data["name"]}':
CollectionRecord(manifest_data['version'], manifest_data['dependencies'])
}

return collection_record


def analyze_deps(collections: Mapping[str, CollectionRecord]) -> List[str]:
'''Analyze dependencies of a set of collections. Return list of errors found.'''
errors = []

# Look at dependencies
# make sure their dependencies are found
for collection_name, collection_info in collections.items():
for dep_name, dep_version_spec in collection_info.dependencies.items():
if dep_name not in collections:
errors.append(f'{collection_name} missing: {dep_name} ({dep_version_spec})')
continue

dependency_version = SemVer(collections[dep_name].version)
if dependency_version not in SemVerSpec(dep_version_spec):
errors.append(f'{collection_name} version_conflict:'
f' {dep_name}-{str(dependency_version)} but needs'
f' {dep_version_spec}')
Copy link
Contributor

@dmsimard dmsimard Apr 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This produces: community.okd version_conflict: kubernetes.core-2.3.0 but needs >=2.1.0,<2.3.0 which isn't bad but I could see it get lost in the noise during a build. Maybe we could prepend it with WARNING: or something like that ? Similar thoughts in other places where we print them.

Edit: I mean like here: https://github.com/ansible-community/antsibull/pull/416/files#diff-2f5af13eca4b3f060e34a7c676b4de74a09f3453b5e081086380b9204681b15bR455

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, I implemented that in 88dd4b2.

The other place where they are printed is the validate-deps subcommand, where (similar to other linting subcommands in antsibull-changelog and antsibull-docs) stdout output is only generated if something is broken. There's no need to prepend things, this will only make it harder to process the output in other tools.

continue

return errors


def check_collection_dependencies(collection_root: str) -> List[str]:
'''Analyze dependencies between collections in a collection root.'''
ansible_collection_dir = pathlib.Path(collection_root)
errors = []

collections: Dict[str, CollectionRecord] = {}
for namespace_dir in (n for n in ansible_collection_dir.iterdir() if n.is_dir()):
for collection_dir in (c for c in namespace_dir.iterdir() if c.is_dir()):
try:
collections.update(parse_manifest(collection_dir))
except FileNotFoundError:
errors.append(f'{collection_dir} is not a valid collection')

errors.extend(analyze_deps(collections))
return errors


def validate_dependencies_command() -> int:
'''CLI functionality for analyzing dependencies.'''
app_ctx = app_context.app_ctx.get()

collection_root: str = app_ctx.extra['collection_root']

errors = check_collection_dependencies(collection_root)

for error in errors:
print(error)

return 3 if errors else 0