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

Add ansible-community version CLI tool #429

Merged
merged 9 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions changelogs/fragments/429-ansible-community-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "Include ``ansible-community`` CLI program with ``--version`` parameter from Ansible 6.0.0rc1 on (https://github.com/ansible-community/antsibull/pull/429)."
13 changes: 13 additions & 0 deletions roles/build-release/tasks/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@
success_msg: "ansible {{ _ansible_version_pypi.stdout }} matches {{ _deps_file }} as well as 'ansible_collections.ansible_release'"
fail_msg: "ansible {{ _ansible_version_pypi.stdout }} does not match {{ _deps_file }} or 'ansible_collections.ansible_release'"

- when: antsibull_ansible_version is _antsibull_packaging_version('6.0.0rc1', '>=')
block:
- name: Retrieve the builtin reported version of ansible from the ansible-community CLI tool
command: >-
{{ antsibull_ansible_venv }}/bin/ansible-community --version
changed_when: false
register: _ansible_version_builtin_2

- name: Verify that the version output matches the one we expect
assert:
that:
- _ansible_version_builtin_2.stdout == ("Ansible community version " ~ antsibull_ansible_version)

- name: Retrieve installed collections
environment:
# In case we happen to be testing with devel, don't print a warning about it
Expand Down
18 changes: 17 additions & 1 deletion src/antsibull/build_ansible_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ def write_release_py(ansible_version: PypiVer, ansible_collections_dir: str) ->
f.write(release_contents)


def write_ansible_community_py(ansible_version: PypiVer, ansible_collections_dir: str) -> None:
release_filename = os.path.join(ansible_collections_dir, 'ansible_community.py')

release_tmpl = Template(get_antsibull_data('ansible-community.py.j2').decode('utf-8'))
release_contents = release_tmpl.render(version=ansible_version)

with open(release_filename, 'w', encoding='utf-8') as f:
f.write(release_contents + '\n')


def write_setup(ansible_version: PypiVer,
ansible_core_version: PypiVer,
collection_exclude_paths: t.List[str],
Expand All @@ -143,7 +153,9 @@ def write_setup(ansible_version: PypiVer,
ansible_core_package_name=get_ansible_core_package_name(ansible_core_version),
ansible_core_version=ansible_core_version,
collection_exclude_paths=collection_exclude_paths,
collection_deps=collection_deps)
collection_deps=collection_deps,
PypiVer=PypiVer,
)

with open(setup_filename, 'w', encoding='utf-8') as f:
f.write(setup_contents)
Expand Down Expand Up @@ -425,6 +437,10 @@ def rebuild_single_command() -> int:
# Write the ansible release info to the collections dir
write_release_py(app_ctx.extra['ansible_version'], ansible_collections_dir)

# Write the ansible-community CLI program (starting with Ansible 6.0.0rc1)
if app_ctx.extra['ansible_version'] >= PypiVer('6.0.0rc1'):
write_ansible_community_py(app_ctx.extra['ansible_version'], ansible_collections_dir)

# Install collections
# TODO: PY3.8:
# collections_to_install = [p for f in os.listdir(download_dir)
Expand Down
25 changes: 25 additions & 0 deletions src/antsibull/data/ansible-community.py.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/python
# coding: utf-8
# Author: Mario Lenz <m@riolenz.de>
# License: GPLv3+
# Copyright: Ansible Project, 2022
"""The ansible-community CLI program."""

import argparse


def main():
'''Main entrypoint for the ansible-community CLI program.'''
parser = argparse.ArgumentParser()
parser.add_argument(
'--version',
action='version',
version='Ansible community version {{ version }}',
help="show the version of the Ansible community package",
)
parser.parse_args()
parser.print_help()

Choose a reason for hiding this comment

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

Does this always print help? I mean, if someone runs ansible-communty --version, why should we print the help message here? There's not much help, anyway, since there's only the --version at the moment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When --version is used, the program exists before parser.print_help() is executed (search for version= on https://docs.python.org/3/library/argparse.html: This expects a version= keyword argument in the [add_argument()](https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.add_argument) call, and prints version information and exits when invoked:).

Choose a reason for hiding this comment

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

Thanks for explaining @felixfontein! LGTM :-)



if __name__ == '__main__':
main()
7 changes: 7 additions & 0 deletions src/antsibull/data/ansible-setup_py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,13 @@ setup(
'Topic :: Utilities',
],
data_files=[],
{%- if version >= PypiVer('6.0.0rc1') %}
entry_points={
'console_scripts': [
'ansible-community=ansible_collections.ansible_community:main'
]
},
{%- endif %}
# Installing as zip files would break due to references to __file__
zip_safe=False
)