import gzip from pathlib import Path import re from typing import Mapping from .base import Repository, Version __all__ = [ 'buster_main_all', 'buster_main_amd64', 'buster_contrib_all', 'buster_contrib_amd64', 'buster_non_free_all', 'buster_non_free_amd64', 'testing_main_all', 'testing_main_amd64', 'testing_contrib_all', 'testing_contrib_amd64', 'testing_non_free_all', 'testing_non_free_amd64', ] PACKAGE_REVISION_INFO = re.compile(r'(\+\w+)*-\d+$') def parse_cached(cached: Path) -> Mapping[str, Version]: result = dict() with gzip.open(cached, mode='rt') as file: this_package = None this_version = None for line in file: line = line.strip() pieces = line.split(': ', 1) if len(pieces) != 2: continue line_type, line_data = pieces if line_type == 'Package': this_package = line_data elif line_type == 'Version': this_version = line_data if this_package is not None and this_version is not None: clean_version = PACKAGE_REVISION_INFO.sub('', this_version) result[this_package] = Version(this_version, clean_version) this_version = None this_package = None return result def build_repo(name: str, dist: str, section: str, arch: str = 'all') -> Repository: url = f'https://deb.debian.org/debian/dists/{dist}/{section}/binary-{arch}/Packages.gz' if arch != 'all': section = f'{section}/{arch}' return Repository( family='Debian', repo=name, section=section, index_url=url, parse=parse_cached, ) buster_main_all = build_repo('10 - Buster', 'buster', 'main') buster_main_amd64 = build_repo('10 - Buster', 'buster', 'main', 'amd64') buster_contrib_all = build_repo('10 - Buster', 'buster', 'contrib') buster_contrib_amd64 = build_repo('10 - Buster', 'buster', 'contrib', 'amd64') buster_non_free_all = build_repo('10 - Buster', 'buster', 'non-free') buster_non_free_amd64 = build_repo('10 - Buster', 'buster', 'non-free', 'amd64') testing_main_all = build_repo('Testing', 'testing', 'main') testing_main_amd64 = build_repo('Testing', 'testing', 'main', 'amd64') testing_contrib_all = build_repo('Testing', 'testing', 'contrib') testing_contrib_amd64 = build_repo('Testing', 'testing', 'contrib', 'amd64') testing_non_free_all = build_repo('Testing', 'testing', 'non-free') testing_non_free_amd64 = build_repo('Testing', 'testing', 'non-free', 'amd64')