Merge pull request #270 from yan12125/pypi-list-options

pypi: support list options
This commit is contained in:
依云 2024-05-19 15:18:08 +08:00 committed by GitHub
commit 3cf403fc51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 26 additions and 14 deletions

View file

@ -570,6 +570,8 @@ pypi
use_pre_release use_pre_release
Whether to accept pre release. Default is false. Whether to accept pre release. Default is false.
This source supports :ref:`list options`.
.. note:: .. note::
An additional dependency "packaging" is required. An additional dependency "packaging" is required.
You can use ``pip install 'nvchecker[pypi]'``. You can use ``pip install 'nvchecker[pypi]'``.

View file

@ -6,6 +6,8 @@ from packaging.version import Version
from nvchecker.api import RichResult from nvchecker.api import RichResult
async def get_version(name, conf, *, cache, **kwargs): async def get_version(name, conf, *, cache, **kwargs):
ret = []
package = conf.get('pypi') or name package = conf.get('pypi') or name
use_pre_release = conf.get('use_pre_release', False) 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) data = await cache.get_json(url)
if use_pre_release: for version in data['releases'].keys():
version = sorted( parsed_version = Version(version)
data['releases'].keys(),
key = Version, if not use_pre_release and parsed_version.is_prerelease:
)[-1] continue
else:
version = data['info']['version'] ret.append(RichResult(
return RichResult( version = version,
version = version, url = f'https://pypi.org/project/{package}/{version}/',
url = f'https://pypi.org/project/{package}/{version}/', ))
)
return ret

View file

@ -20,3 +20,9 @@ async def test_pypi_pre_release(get_version):
"source": "pypi", "source": "pypi",
"use_pre_release": 1, "use_pre_release": 1,
}) == "1.0.1a1" }) == "1.0.1a1"
async def test_pypi_list(get_version):
assert await get_version("urllib3", {
"source": "pypi",
"include_regex": "^1\\..*",
}) == "1.26.18"

View file

@ -1,5 +1,5 @@
# MIT licensed # MIT licensed
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2020,2024 lilydjwg <lilydjwg@gmail.com>, et al.
# Copyright (c) 2017 Felix Yan <felixonmars@archlinux.org>, et al. # Copyright (c) 2017 Felix Yan <felixonmars@archlinux.org>, et al.
import pytest import pytest
@ -7,9 +7,10 @@ pytestmark = [pytest.mark.asyncio(scope="session"), pytest.mark.needs_net]
@pytest.mark.flaky @pytest.mark.flaky
async def test_ubuntupkg(get_version): async def test_ubuntupkg(get_version):
assert await get_version("sigrok-firmware-fx2lafw", { v = await get_version("sigrok-firmware-fx2lafw", {
"source": "ubuntupkg", "source": "ubuntupkg",
}) == "0.1.7-1" })
assert v.startswith("0.1.7-")
@pytest.mark.flaky @pytest.mark.flaky
async def test_ubuntupkg_strip_release(get_version): async def test_ubuntupkg_strip_release(get_version):