Skip to content

Commit

Permalink
Accept ansible-core filename starting with `ansible_core-<version>.ta…
Browse files Browse the repository at this point in the history
…r` 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 <maxwell@gtmx.me>
(cherry picked from commit d787b80)
  • Loading branch information
felixfontein authored and patchback[bot] committed Apr 16, 2024
1 parent cbfba0c commit 94d1bf1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/158-ansible-core-pypi.yml
Original file line number Diff line number Diff line change
@@ -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 <https://peps.python.org/pep-0625/>`__ support in setuptools 69.3.0 (https://github.com/ansible-community/antsibull-core/pull/158)."
33 changes: 23 additions & 10 deletions src/antsibull_core/ansible_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down

0 comments on commit 94d1bf1

Please sign in to comment.