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

Work around setuptools 63 deprecations #502

Merged
merged 9 commits into from
Apr 7, 2023
2 changes: 2 additions & 0 deletions changelogs/fragments/502-setup.py.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- "Make compatible with deprecations issued by newer setuptools releases (https://github.com/ansible-community/antsibull/issues/433, https://github.com/ansible-community/antsibull/pull/502)."
35 changes: 31 additions & 4 deletions src/antsibull/build_ansible_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import os.path
import shutil
import tempfile
from collections import defaultdict
from collections.abc import Collection, Mapping
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -211,6 +212,9 @@ def write_setup(ansible_version: PypiVer,
ansible_core_version: PypiVer,
collection_exclude_paths: list[str],
collection_deps: str,
collection_names: list[str],
collection_namespaces: Mapping[str, list[str]],
collection_directories: Mapping[str, list[str]],
package_dir: str,
python_requires: str) -> None:
setup_filename = os.path.join(package_dir, 'setup.py')
Expand All @@ -222,6 +226,9 @@ def write_setup(ansible_version: PypiVer,
ansible_core_version=ansible_core_version,
collection_exclude_paths=collection_exclude_paths,
collection_deps=collection_deps,
collection_names=collection_names,
collection_namespaces=collection_namespaces,
collection_directories=collection_directories,
python_requires=python_requires,
PypiVer=PypiVer,
)
Expand All @@ -240,6 +247,9 @@ def write_python_build_files(ansible_version: PypiVer,
ansible_core_version: PypiVer,
collection_exclude_paths: list[str],
collection_deps: str,
collection_names: list[str],
collection_namespaces: Mapping[str, list[str]],
collection_directories: Mapping[str, list[str]],
package_dir: str,
release_notes: ReleaseNotes | None = None,
debian: bool = False,
Expand All @@ -250,7 +260,8 @@ def write_python_build_files(ansible_version: PypiVer,
write_manifest(package_dir, release_notes, debian, tags_file)
write_setup(
ansible_version, ansible_core_version, collection_exclude_paths, collection_deps,
package_dir, python_requires)
collection_names, collection_namespaces, collection_directories, package_dir,
python_requires)


def write_debian_directory(ansible_version: PypiVer,
Expand Down Expand Up @@ -576,15 +587,31 @@ def rebuild_single_command() -> int:

# TODO: do something with collection_ignored_files

# Collect collection namespaces and collection root subdirectories
collection_namespaces: dict[str, list[str]] = defaultdict(list)
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
collection_directories: dict[str, list[str]] = {}
for collection in dependency_data.deps:
namespace, name = collection.split('.', 1)
collection_namespaces[namespace].append(name)
collection_path = os.path.join(ansible_collections_dir, namespace, name)
collection_directories[collection] = [
file for file in os.listdir(collection_path)
if file not in ('tests', 'docs')
and not file.startswith('.')
and os.path.isdir(os.path.join(collection_path, file))
]
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggestion: you could try moving this to a function that returns a tuple of collection_names, collection_namespaces, collection_directories to reduce the mccabe complexity.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I moved a slightly different block out: f63694b.

Copy link
Contributor

Choose a reason for hiding this comment

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

I like it!


# Write build scripts and files
tags_path: str | None = None
if app_ctx.extra['tags_file']:
tags_path = os.path.join(app_ctx.extra['data_dir'],
app_ctx.extra['tags_file'])
write_build_script(app_ctx.extra['ansible_version'], ansible_core_version, package_dir)
write_python_build_files(app_ctx.extra['ansible_version'], ansible_core_version,
collection_exclude_paths, '', package_dir, release_notes,
app_ctx.extra['debian'], python_requires, tags_path)
collection_exclude_paths, '', sorted(dependency_data.deps),
felixfontein marked this conversation as resolved.
Show resolved Hide resolved
collection_namespaces, collection_directories, package_dir,
release_notes, app_ctx.extra['debian'], python_requires,
tags_path)
if app_ctx.extra['debian']:
write_debian_directory(app_ctx.extra['ansible_version'], ansible_core_version,
package_dir)
Expand Down Expand Up @@ -719,7 +746,7 @@ def build_multiple_command() -> int:
collection_deps_str = '\n' + ',\n'.join(collection_deps)
write_build_script(app_ctx.extra['ansible_version'], ansible_core_version_obj, package_dir)
write_python_build_files(app_ctx.extra['ansible_version'], ansible_core_version_obj,
[], collection_deps_str, package_dir,
[], collection_deps_str, [], {}, {}, package_dir,
python_requires=python_requires)

make_dist(package_dir, app_ctx.extra['sdist_dir'])
Expand Down
41 changes: 37 additions & 4 deletions src/antsibull/data/ansible-setup_py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@

import os
import sys
{%- if version.major >= 8 %}
from setuptools import setup, find_namespace_packages
{%- else %}
from setuptools import setup
{%- endif %}

{%- if version.major < 6 %}

Expand Down Expand Up @@ -174,17 +178,46 @@ setup(
},
license='GPLv3+',
python_requires='{{ python_requires }}',
{%- if version.major >= 8 %}
packages=find_namespace_packages(
'.',
include=[
'ansible_collections',
'ansible_collections.*',
],
exclude=[
{%- for collection in collection_names %}
'ansible_collections.{{ collection }}.tests.*',
'ansible_collections.{{ collection }}.tests',
'ansible_collections.{{ collection }}.docs.*',
'ansible_collections.{{ collection }}.docs',
{%- endfor %}
],
),
package_dir={'': '.'},
package_data={
{%- for collection in collection_names %}
'ansible_collections.{{ collection }}': [
'*',
{%- for dir in collection_directories[collection] %}
'{{ dir }}/**/*', '{{ dir }}/**/.*',
{%- endfor %}
],
{%- endfor %}
},
{%- else %}
packages=['ansible_collections'],
{% if version.major >= 6 and collection_exclude_paths %}
{%- if version.major >= 6 and collection_exclude_paths %}
exclude_package_data={
'ansible_collections': [
{%- for path in collection_exclude_paths %}
{%- for path in collection_exclude_paths %}
'{{ path }}',
{%- endfor %}
{%- endfor %}
],
},
{%- endif %}
{%- endif %}
include_package_data=True,
{%- endif %}
install_requires=[
'{{ ansible_core_package_name }} ~= {{ ansible_core_version }}',{{ collection_deps }}
],
Expand Down