From 94d1bf12cdb38c7184f8dcb3a74a35b61dbbe668 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 16 Apr 2024 21:03:30 +0200 Subject: [PATCH] Accept ansible-core filename starting with `ansible_core-.tar` as well; fix error message formatting (#158) * Fix error message formatting. * Accept filename starting with ansible_core as well. * AnsibleCorePyPiClient.retrieve: ensure the correct filename is returned Code in antsibull relies on the top-level directory and the archive having the same basename. * Update changelog. --------- Co-authored-by: Maxwell G (cherry picked from commit d787b8016f8fa8a66b655de36df6177cf2537bd2) --- .../fragments/158-ansible-core-pypi.yml | 3 ++ src/antsibull_core/ansible_core.py | 33 +++++++++++++------ 2 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 changelogs/fragments/158-ansible-core-pypi.yml diff --git a/changelogs/fragments/158-ansible-core-pypi.yml b/changelogs/fragments/158-ansible-core-pypi.yml new file mode 100644 index 0000000..e06a4f8 --- /dev/null +++ b/changelogs/fragments/158-ansible-core-pypi.yml @@ -0,0 +1,3 @@ +bugfixes: + - "Adjusting ansible-core PyPI code to also accept a filename starting with ``ansible_core``, which seems to be in use since ansible-core 2.16.6 + due to `PEP-625 `__ support in setuptools 69.3.0 (https://github.com/ansible-community/antsibull-core/pull/158)." diff --git a/src/antsibull_core/ansible_core.py b/src/antsibull_core/ansible_core.py index a49846d..affbcd7 100644 --- a/src/antsibull_core/ansible_core.py +++ b/src/antsibull_core/ansible_core.py @@ -134,7 +134,10 @@ async def get_latest_version(self) -> PypiVer: versions = await self.get_versions() return versions[0] - async def retrieve(self, ansible_core_version: str, download_dir: StrPath) -> str: + # TODO(anyone): Make function less complex + async def retrieve( # noqa C901 + self, ansible_core_version: str, download_dir: StrPath + ) -> str: """ Get the release from pypi. @@ -144,31 +147,41 @@ async def retrieve(self, ansible_core_version: str, download_dir: StrPath) -> st """ package_name = get_ansible_core_package_name(ansible_core_version) - tar_filename = f"{package_name}-{ansible_core_version}.tar.gz" + # https://github.com/pypa/setuptools/pull/4286 + # Newer setuptools versions perform dist filename normalization + tar_filenames = ( + f"{package_name.replace('-', '_')}-{ansible_core_version}.tar.gz", + f"{package_name}-{ansible_core_version}.tar.gz", + ) + tar_filename = tar_filenames[0] tar_path = os.path.join(download_dir, tar_filename) lib_ctx = app_context.lib_ctx.get() if lib_ctx.ansible_core_cache and lib_ctx.trust_ansible_core_cache: - cached_path = os.path.join(lib_ctx.ansible_core_cache, tar_filename) - if os.path.isfile(cached_path): - await copy_file(cached_path, tar_path, check_content=False) - return tar_path + for tar_filename in tar_filenames: + cached_path = os.path.join( + t.cast(str, lib_ctx.ansible_core_cache), tar_filename + ) + if os.path.isfile(cached_path): + tar_path = os.path.join(download_dir, tar_filename) + await copy_file(cached_path, tar_path, check_content=False) + return tar_path release_info = await self.get_release_info(package_name) pypi_url = "" digests = {} for release in release_info[ansible_core_version]: - if release["filename"].startswith( - f"{package_name}-{ansible_core_version}.tar." - ): + if release["filename"] in tar_filenames: + tar_filename = release["filename"] + tar_path = os.path.join(download_dir, tar_filename) pypi_url = release["url"] digests = release["digests"] break else: # for-else: http://bit.ly/1ElPkyg raise UnknownVersion( f"{package_name} {ansible_core_version} does not" - " exist on {self.pypi_server_url}" + f" exist on {self.pypi_server_url}" ) if lib_ctx.ansible_core_cache and "sha256" in digests: