add CRAN support

This commit is contained in:
Pekka Ristola 2022-02-22 18:38:49 +02:00
parent 7739f5c8c0
commit e03023bd6c
No known key found for this signature in database
GPG Key ID: 2C20BE716E05213E
3 changed files with 47 additions and 0 deletions

View File

@ -587,6 +587,17 @@ Check `MetaCPAN <https://metacpan.org/>`_ for updates.
cpan
The name used on CPAN, e.g. ``YAML``.
Check CRAN
~~~~~~~~~~
::
source = "cran"
Check `CRAN <https://cran.r-project.org/web/packages/>`_ for updates.
cran
The name used on CRAN, e.g. ``xml2``.
Check Packagist
~~~~~~~~~~~~~~~
::

26
nvchecker_source/cran.py Normal file
View File

@ -0,0 +1,26 @@
# MIT licensed
# Copyright (c) 2022 Pekka Ristola <pekkarr [at] protonmail [dot] com>, et al.
from nvchecker.api import session, GetVersionError
CRAN_URL = 'https://cran.r-project.org/package=%s/DESCRIPTION'
VERSION_FIELD = 'Version: '
async def request(pkg):
url = CRAN_URL % pkg
res = await session.get(url)
return res.body.decode('utf-8', errors='ignore')
async def get_version(name, conf, *, cache, **kwargs):
package = conf.get('cran', name)
desc = await cache.get(package, request)
for line in desc.splitlines():
if line.startswith(VERSION_FIELD):
version = line[len(VERSION_FIELD):]
break
else:
raise GetVersionError('Invalid DESCRIPTION file')
return version

10
tests/test_cran.py Normal file
View File

@ -0,0 +1,10 @@
# MIT licensed
# Copyright (c) 2022 Pekka Ristola <pekkarr [at] protonmail [dot] com>, et al.
import pytest
pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
async def test_cran(get_version):
assert await get_version("xml2", {
"source": "cran",
}) == "1.3.3"