From 8397cbbce0ae989fffb16bd921968fa2ddc74e88 Mon Sep 17 00:00:00 2001 From: Chih-Hsuan Yen <645432-yan12125@users.noreply.gitlab.com> Date: Sun, 19 May 2024 12:10:40 +0800 Subject: [PATCH] pypi: support list options --- docs/usage.rst | 2 ++ nvchecker_source/pypi.py | 25 ++++++++++++++----------- tests/test_pypi.py | 6 ++++++ 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/docs/usage.rst b/docs/usage.rst index e64a478..5a85433 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -570,6 +570,8 @@ pypi use_pre_release Whether to accept pre release. Default is false. +This source supports :ref:`list options`. + .. note:: An additional dependency "packaging" is required. You can use ``pip install 'nvchecker[pypi]'``. diff --git a/nvchecker_source/pypi.py b/nvchecker_source/pypi.py index 993ec99..e565475 100644 --- a/nvchecker_source/pypi.py +++ b/nvchecker_source/pypi.py @@ -6,6 +6,8 @@ from packaging.version import Version from nvchecker.api import RichResult async def get_version(name, conf, *, cache, **kwargs): + ret = [] + package = conf.get('pypi') or name use_pre_release = conf.get('use_pre_release', False) @@ -13,14 +15,15 @@ async def get_version(name, conf, *, cache, **kwargs): data = await cache.get_json(url) - if use_pre_release: - version = sorted( - data['releases'].keys(), - key = Version, - )[-1] - else: - version = data['info']['version'] - return RichResult( - version = version, - url = f'https://pypi.org/project/{package}/{version}/', - ) + for version in data['releases'].keys(): + parsed_version = Version(version) + + if not use_pre_release and parsed_version.is_prerelease: + continue + + ret.append(RichResult( + version = version, + url = f'https://pypi.org/project/{package}/{version}/', + )) + + return ret diff --git a/tests/test_pypi.py b/tests/test_pypi.py index 7faa447..18f8b0b 100644 --- a/tests/test_pypi.py +++ b/tests/test_pypi.py @@ -20,3 +20,9 @@ async def test_pypi_pre_release(get_version): "source": "pypi", "use_pre_release": 1, }) == "1.0.1a1" + +async def test_pypi_list(get_version): + assert await get_version("urllib3", { + "source": "pypi", + "include_regex": "^1\\..*", + }) == "1.26.18"