-
Notifications
You must be signed in to change notification settings - Fork 31
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
felixfontein
merged 5 commits into
ansible-community:main
from
felixfontein:dependencies
Apr 27, 2022
Merged
Check dependencies #416
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e602756
Add a Dep closure script.
abadger d1631c4
Convert script to library code.
felixfontein 3159598
Add validate-deps subcommand.
felixfontein dcddc8f
Check dependencies during rebuild.
felixfontein 88dd4b2
Prepend messages with 'WARNING:'.
felixfontein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}') | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 withWARNING:
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
There was a problem hiding this comment.
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.