2020-09-16 09:20:17 +00:00
|
|
|
# MIT licensed
|
|
|
|
# Copyright (c) 2020 Felix Yan <felixonmars@archlinux.org>, et al.
|
|
|
|
|
2020-09-20 15:09:40 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-06-25 07:22:40 +00:00
|
|
|
import re
|
2020-09-20 14:02:18 +00:00
|
|
|
import asyncio
|
2020-09-21 10:49:23 +00:00
|
|
|
from typing import Dict, Tuple
|
2021-06-25 07:22:40 +00:00
|
|
|
import itertools
|
|
|
|
import functools
|
|
|
|
from collections import defaultdict
|
2020-09-20 14:02:18 +00:00
|
|
|
|
|
|
|
from nvchecker.api import (
|
2023-10-18 00:00:46 +00:00
|
|
|
session, GetVersionError, VersionResult,
|
|
|
|
RichResult, Entry, AsyncCache, KeyManager,
|
2020-09-20 14:02:18 +00:00
|
|
|
)
|
2020-09-16 09:20:17 +00:00
|
|
|
|
|
|
|
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", "")
|
|
|
|
|
2021-06-25 07:22:40 +00:00
|
|
|
DpkgVersion = Tuple[int, str, str]
|
|
|
|
|
|
|
|
def parse_version(s: str) -> DpkgVersion:
|
|
|
|
try:
|
|
|
|
epoch_str, rest = s.split(':', 1)
|
|
|
|
except ValueError:
|
|
|
|
epoch = 0
|
|
|
|
rest = s
|
|
|
|
else:
|
|
|
|
epoch = int(epoch_str)
|
|
|
|
|
|
|
|
try:
|
|
|
|
ver, rev = rest.split('-', 1)
|
|
|
|
except ValueError:
|
|
|
|
ver = rest
|
|
|
|
rev = ''
|
|
|
|
|
|
|
|
return epoch, ver, rev
|
|
|
|
|
|
|
|
def _compare_part(a: str, b: str) -> int:
|
|
|
|
sa = re.split(r'(\d+)', a)
|
|
|
|
sb = re.split(r'(\d+)', b)
|
|
|
|
for idx, (pa, pb) in enumerate(itertools.zip_longest(sa, sb)):
|
|
|
|
if pa is None:
|
|
|
|
return -1
|
|
|
|
elif pb is None:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
if idx % 2 == 1:
|
|
|
|
ret = int(pa) - int(pb)
|
|
|
|
if ret != 0:
|
|
|
|
return ret
|
|
|
|
else:
|
|
|
|
if pa < pb:
|
|
|
|
return -1
|
|
|
|
elif pa > pb:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def compare_version_parsed(a: DpkgVersion, b: DpkgVersion) -> int:
|
|
|
|
ret = a[0] - b[0]
|
|
|
|
if ret != 0:
|
|
|
|
return ret
|
|
|
|
ret = _compare_part(a[1], b[1])
|
|
|
|
if ret != 0:
|
|
|
|
return ret
|
|
|
|
return _compare_part(a[2], b[2])
|
|
|
|
|
|
|
|
def compare_version(a: str, b: str) -> int:
|
|
|
|
va = parse_version(a)
|
|
|
|
vb = parse_version(b)
|
|
|
|
return compare_version_parsed(va, vb)
|
|
|
|
|
2020-09-20 14:02:18 +00:00
|
|
|
def _decompress_data(url: str, data: bytes) -> str:
|
2020-09-16 09:20:17 +00:00
|
|
|
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')
|
|
|
|
|
2020-09-20 14:02:18 +00:00
|
|
|
async def get_url(url: str) -> str:
|
|
|
|
res = await session.get(url)
|
|
|
|
data = res.body
|
|
|
|
loop = asyncio.get_running_loop()
|
|
|
|
return await loop.run_in_executor(
|
|
|
|
None, _decompress_data,
|
|
|
|
url, data)
|
|
|
|
|
2023-10-18 00:00:46 +00:00
|
|
|
async def parse_packages(key: Tuple[AsyncCache, str]) -> Tuple[Dict[str, str], Dict[str, str], Dict[str, str]]:
|
2020-09-21 10:49:23 +00:00
|
|
|
cache, url = key
|
|
|
|
apt_packages = await cache.get(url, get_url) # type: ignore
|
|
|
|
|
2021-06-25 07:22:40 +00:00
|
|
|
pkg_map = defaultdict(list)
|
|
|
|
srcpkg_map = defaultdict(list)
|
2023-10-18 00:00:46 +00:00
|
|
|
pkg_to_src_map = defaultdict(list)
|
2020-09-21 10:49:23 +00:00
|
|
|
|
|
|
|
pkg = None
|
|
|
|
srcpkg = None
|
|
|
|
for line in apt_packages.split('\n'):
|
|
|
|
if line.startswith("Package: "):
|
|
|
|
pkg = line[9:]
|
|
|
|
elif line.startswith("Source: "):
|
|
|
|
srcpkg = line[8:]
|
|
|
|
elif line.startswith("Version: "):
|
|
|
|
version = line[9:]
|
|
|
|
if pkg is not None:
|
2021-06-25 07:22:40 +00:00
|
|
|
pkg_map[pkg].append(version)
|
2023-10-18 00:00:46 +00:00
|
|
|
pkg_to_src_map["%s/%s" % (pkg, version)] = srcpkg if srcpkg is not None else pkg
|
2020-09-21 10:49:23 +00:00
|
|
|
if srcpkg is not None:
|
2021-06-25 07:22:40 +00:00
|
|
|
srcpkg_map[srcpkg].append(version)
|
2020-09-21 10:49:23 +00:00
|
|
|
pkg = srcpkg = None
|
|
|
|
|
2021-06-25 07:22:40 +00:00
|
|
|
pkg_map_max = {pkg: max(vs, key=functools.cmp_to_key(compare_version))
|
|
|
|
for pkg, vs in pkg_map.items()}
|
|
|
|
srcpkg_map_max = {pkg: max(vs, key=functools.cmp_to_key(compare_version))
|
|
|
|
for pkg, vs in srcpkg_map.items()}
|
2023-10-18 00:00:46 +00:00
|
|
|
pkg_to_src_map_max = {pkg: pkg_to_src_map["%s/%s" % (pkg, vs)]
|
|
|
|
for pkg, vs in pkg_map_max.items()}
|
2021-06-25 07:22:40 +00:00
|
|
|
|
2023-10-18 00:00:46 +00:00
|
|
|
return pkg_map_max, srcpkg_map_max, pkg_to_src_map_max
|
2020-09-21 10:49:23 +00:00
|
|
|
|
2020-09-20 14:02:18 +00:00
|
|
|
async def get_version(
|
|
|
|
name: str, conf: Entry, *,
|
|
|
|
cache: AsyncCache, keymanager: KeyManager,
|
|
|
|
**kwargs,
|
|
|
|
) -> VersionResult:
|
2020-09-16 13:19:38 +00:00
|
|
|
srcpkg = conf.get('srcpkg')
|
2020-09-16 10:57:50 +00:00
|
|
|
pkg = conf.get('pkg')
|
2020-09-17 04:53:03 +00:00
|
|
|
mirror = conf['mirror']
|
|
|
|
suite = conf['suite']
|
2020-09-16 09:20:17 +00:00
|
|
|
repo = conf.get('repo', 'main')
|
|
|
|
arch = conf.get('arch', 'amd64')
|
|
|
|
strip_release = conf.get('strip_release', False)
|
|
|
|
|
2020-09-16 13:19:38 +00:00
|
|
|
if srcpkg and pkg:
|
2022-04-21 09:16:34 +00:00
|
|
|
raise GetVersionError('Setting both srcpkg and pkg is ambiguous')
|
2020-09-16 13:19:38 +00:00
|
|
|
elif not srcpkg and not pkg:
|
2020-09-16 10:57:50 +00:00
|
|
|
pkg = name
|
|
|
|
|
2020-09-20 14:02:18 +00:00
|
|
|
apt_release = await cache.get(
|
|
|
|
APT_RELEASE_URL % (mirror, suite), get_url) # type: ignore
|
2020-09-16 09:20:17 +00:00
|
|
|
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')
|
|
|
|
|
2023-10-18 00:00:46 +00:00
|
|
|
pkg_map, srcpkg_map, pkg_to_src_map = await cache.get(
|
2020-09-21 10:49:23 +00:00
|
|
|
(cache, APT_PACKAGES_URL % (mirror, suite, packages_path)), parse_packages) # type: ignore
|
2020-09-16 09:20:17 +00:00
|
|
|
|
2020-09-21 10:49:23 +00:00
|
|
|
if pkg and pkg in pkg_map:
|
|
|
|
version = pkg_map[pkg]
|
2023-10-18 00:00:46 +00:00
|
|
|
changelog_name = pkg_to_src_map[pkg]
|
2020-09-21 10:49:23 +00:00
|
|
|
elif srcpkg and srcpkg in srcpkg_map:
|
|
|
|
version = srcpkg_map[srcpkg]
|
2023-10-18 00:00:46 +00:00
|
|
|
changelog_name = srcpkg
|
2020-09-21 10:49:23 +00:00
|
|
|
else:
|
|
|
|
raise GetVersionError('package not found in APT repository')
|
2020-09-16 09:20:17 +00:00
|
|
|
|
2023-10-18 00:00:46 +00:00
|
|
|
# Get Changelogs field from the Release file
|
|
|
|
changelogs_url = None
|
|
|
|
for line in apt_release.split('\n'):
|
|
|
|
if line.startswith('Changelogs: '):
|
|
|
|
changelogs_url = line[12:]
|
|
|
|
break
|
|
|
|
|
|
|
|
# Build the changelog URL (see https://wiki.debian.org/DebianRepository/Format#Changelogs for spec)
|
|
|
|
changelog = None
|
|
|
|
if changelogs_url is not None and changelogs_url != 'no':
|
|
|
|
changelog_section = changelog_name[:4] if changelog_name.startswith('lib') else changelog_name[:1]
|
|
|
|
changelog = changelogs_url.replace('@CHANGEPATH@', f'{repo}/{changelog_section}/{changelog_name}/{changelog_name}_{version}')
|
|
|
|
|
2020-09-21 10:49:23 +00:00
|
|
|
if strip_release:
|
|
|
|
version = version.split("-")[0]
|
2023-10-18 00:00:46 +00:00
|
|
|
|
|
|
|
if changelog is not None:
|
|
|
|
return RichResult(
|
|
|
|
version = version,
|
|
|
|
url = changelog,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return version
|