Merge pull request #146 from felixonmars/apt

Add an APT source
This commit is contained in:
依云 2020-09-17 13:17:33 +08:00 committed by GitHub
commit ef808b7517
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 132 additions and 0 deletions

View file

@ -610,6 +610,37 @@ host
This source returns tags and supports :ref:`list options`.
Check APT repository
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
source = "apt"
This enables you to track the update of an arbitary APT repository, without needing of apt and an updated local APT database.
pkg
Name of the APT binary package.
srcpkg
Name of the APT source package.
mirror
URL of the repository.
suite
Name of the APT repository release (jessie, wheezy, etc)
repo
Name of the APT repository (main, contrib, etc, defaults to main)
arch
Architecture of the repository (i386, amd64, etc, defaults to amd64)
strip_release
Strip the release part.
Note that either pkg or srcpkg needs to be specified (but not both) or the item name will be used as pkg.
Manually updating
~~~~~~~~~~~~~~~~~
::

60
nvchecker_source/apt.py Normal file
View file

@ -0,0 +1,60 @@
# MIT licensed
# Copyright (c) 2020 Felix Yan <felixonmars@archlinux.org>, et al.
from nvchecker.api import session, GetVersionError
APT_RELEASE_URL = "%s/dists/%s/Release"
APT_PACKAGES_PATH = "%s/binary-%s/Packages%s"
APT_PACKAGES_URL = "%s/dists/%s/%s"
APT_PACKAGES_SUFFIX_PREFER = (".xz", ".gz", "")
async def get_url(url):
res = await session.get(url)
data = res.body
if url.endswith(".xz"):
import lzma
data = lzma.decompress(data)
elif url.endswith(".gz"):
import gzip
data = gzip.decompress(data)
return data.decode('utf-8')
async def get_version(name, conf, *, cache, **kwargs):
srcpkg = conf.get('srcpkg')
pkg = conf.get('pkg')
mirror = conf['mirror']
suite = conf['suite']
repo = conf.get('repo', 'main')
arch = conf.get('arch', 'amd64')
strip_release = conf.get('strip_release', False)
if srcpkg and pkg:
raise GetVersionError('Setting both srcpkg and pkg is ambigious')
elif not srcpkg and not pkg:
pkg = name
apt_release = await cache.get(APT_RELEASE_URL % (mirror, suite), get_url)
for suffix in APT_PACKAGES_SUFFIX_PREFER:
packages_path = APT_PACKAGES_PATH % (repo, arch, suffix)
if " " + packages_path in apt_release:
break
else:
raise GetVersionError('Packages file not found in APT repository')
apt_packages = await cache.get(APT_PACKAGES_URL % (mirror, suite, packages_path), get_url)
pkg_found = False
for line in apt_packages.split("\n"):
if pkg and line == "Package: " + pkg:
pkg_found = True
if srcpkg and line == "Source: " + srcpkg:
pkg_found = True
if pkg_found and line.startswith("Version: "):
version = line[9:]
if strip_release:
version = version.split("-")[0]
return version
raise GetVersionError('package not found in APT repository')

41
tests/test_apt.py Normal file
View file

@ -0,0 +1,41 @@
# MIT licensed
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al.
# Copyright (c) 2017 Felix Yan <felixonmars@archlinux.org>, et al.
from flaky import flaky
import pytest
pytestmark = [pytest.mark.asyncio, pytest.mark.needs_net]
@flaky(max_runs=10)
async def test_apt(get_version):
assert await get_version("sigrok-firmware-fx2lafw", {
"source": "apt",
"mirror": "http://deb.debian.org/debian/",
"suite": "sid",
}) == "0.1.7-1"
@flaky(max_runs=10)
async def test_apt_srcpkg(get_version):
assert await get_version("test", {
"source": "apt",
"srcpkg": "golang-github-dataence-porter2",
"mirror": "http://deb.debian.org/debian/",
"suite": "sid",
}) == "0.0~git20150829.56e4718-2"
@flaky(max_runs=10)
async def test_apt_strip_release(get_version):
assert await get_version("sigrok-firmware-fx2lafw", {
"source": "apt",
"mirror": "http://deb.debian.org/debian/",
"suite": "sid",
"strip_release": 1,
}) == "0.1.7"
@flaky(max_runs=10)
async def test_apt_deepin(get_version):
assert await get_version("sigrok-firmware-fx2lafw", {
"source": "apt",
"mirror": "https://community-packages.deepin.com/deepin",
"suite": "apricot",
}) == "0.1.6-1"