Skip to content

Commit

Permalink
Merge pull request #270 from yan12125/pypi-list-options
Browse files Browse the repository at this point in the history
pypi: support list options
  • Loading branch information
lilydjwg committed May 19, 2024
2 parents 2a63dde + 6af59aa commit 3cf403f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
2 changes: 2 additions & 0 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]'``.
Expand Down
25 changes: 14 additions & 11 deletions nvchecker_source/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
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)

url = 'https://pypi.org/pypi/{}/json'.format(package)

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
6 changes: 6 additions & 0 deletions tests/test_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
7 changes: 4 additions & 3 deletions tests/test_ubuntupkg.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# 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.

import pytest
pytestmark = [pytest.mark.asyncio(scope="session"), pytest.mark.needs_net]

@pytest.mark.flaky
async def test_ubuntupkg(get_version):
assert await get_version("sigrok-firmware-fx2lafw", {
v = await get_version("sigrok-firmware-fx2lafw", {
"source": "ubuntupkg",
}) == "0.1.7-1"
})
assert v.startswith("0.1.7-")

@pytest.mark.flaky
async def test_ubuntupkg_strip_release(get_version):
Expand Down

0 comments on commit 3cf403f

Please sign in to comment.