import gzip from pathlib import Path import re from typing import Mapping from .base import Repository, Version __all__ = [ 'focal_main_amd64', 'focal_restricted_amd64', 'focal_universe_amd64', 'focal_multiverse_amd64', 'focal_updates_main_amd64', 'focal_updates_restricted_amd64', 'focal_updates_universe_amd64', 'focal_updates_multiverse_amd64', 'focal_backports_main_amd64', 'focal_backports_restricted_amd64', 'focal_backports_universe_amd64', 'focal_backports_multiverse_amd64', 'focal_security_main_amd64', 'focal_security_restricted_amd64', 'focal_security_universe_amd64', 'focal_security_multiverse_amd64', 'groovy_main_amd64', 'groovy_restricted_amd64', 'groovy_universe_amd64', 'groovy_multiverse_amd64', 'groovy_updates_main_amd64', 'groovy_updates_restricted_amd64', 'groovy_updates_universe_amd64', 'groovy_updates_multiverse_amd64', 'groovy_backports_main_amd64', 'groovy_backports_restricted_amd64', 'groovy_backports_universe_amd64', 'groovy_backports_multiverse_amd64', 'groovy_security_main_amd64', 'groovy_security_restricted_amd64', 'groovy_security_universe_amd64', 'groovy_security_multiverse_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'http://archive.ubuntu.com/ubuntu/dists/{dist}/{section}/binary-{arch}/Packages.gz' return Repository( family='Ubuntu', repo=name, index_url=url, parse=parse_cached, ) focal_main_amd64 = build_repo('20.04 Focal (main/amd64)', 'focal', 'main', 'amd64') focal_restricted_amd64 = build_repo('20.04 Focal (restricted/amd64)', 'focal', 'restricted', 'amd64') focal_universe_amd64 = build_repo('20.04 Focal (universe/amd64)', 'focal', 'universe', 'amd64') focal_multiverse_amd64 = build_repo('20.04 Focal (multiverse/amd64)', 'focal', 'multiverse', 'amd64') focal_updates_main_amd64 = build_repo('20.04 Focal Updates (main/amd64)', 'focal-updates', 'main', 'amd64') focal_updates_restricted_amd64 = build_repo('20.04 Focal Updates (restricted/amd64)', 'focal-updates', 'restricted', 'amd64') focal_updates_universe_amd64 = build_repo('20.04 Focal Updates (universe/amd64)', 'focal-updates', 'universe', 'amd64') focal_updates_multiverse_amd64 = build_repo('20.04 Focal Updates (multiverse/amd64)', 'focal-updates', 'multiverse', 'amd64') focal_backports_main_amd64 = build_repo('20.04 Focal Backports (main/amd64)', 'focal-backports', 'main', 'amd64') focal_backports_restricted_amd64 = build_repo('20.04 Focal Backports (restricted/amd64)', 'focal-backports', 'restricted', 'amd64') focal_backports_universe_amd64 = build_repo('20.04 Focal Backports (universe/amd64)', 'focal-backports', 'universe', 'amd64') focal_backports_multiverse_amd64 = build_repo('20.04 Focal Backports (multiverse/amd64)', 'focal-backports', 'multiverse', 'amd64') focal_security_main_amd64 = build_repo('20.04 Focal Updates (main/amd64)', 'focal-security', 'main', 'amd64') focal_security_restricted_amd64 = build_repo('20.04 Focal Updates (restricted/amd64)', 'focal-security', 'restricted', 'amd64') focal_security_universe_amd64 = build_repo('20.04 Focal Updates (universe/amd64)', 'focal-security', 'universe', 'amd64') focal_security_multiverse_amd64 = build_repo('20.04 Focal Updates (multiverse/amd64)', 'focal-security', 'multiverse', 'amd64') groovy_main_amd64 = build_repo('20.10 Groovy (main/amd64)', 'groovy', 'main', 'amd64') groovy_restricted_amd64 = build_repo('20.10 Groovy (restricted/amd64)', 'groovy', 'restricted', 'amd64') groovy_universe_amd64 = build_repo('20.10 Groovy (universe/amd64)', 'groovy', 'universe', 'amd64') groovy_multiverse_amd64 = build_repo('20.10 Groovy (multiverse/amd64)', 'groovy', 'multiverse', 'amd64') groovy_updates_main_amd64 = build_repo('20.10 Groovy Updates (main/amd64)', 'groovy-updates', 'main', 'amd64') groovy_updates_restricted_amd64 = build_repo('20.10 Groovy Updates (restricted/amd64)', 'groovy-updates', 'restricted', 'amd64') groovy_updates_universe_amd64 = build_repo('20.10 Groovy Updates (universe/amd64)', 'groovy-updates', 'universe', 'amd64') groovy_updates_multiverse_amd64 = build_repo('20.10 Groovy Updates (multiverse/amd64)', 'groovy-updates', 'multiverse', 'amd64') groovy_backports_main_amd64 = build_repo('20.10 Groovy Backports (main/amd64)', 'groovy-backports', 'main', 'amd64') groovy_backports_restricted_amd64 = build_repo('20.10 Groovy Backports (restricted/amd64)', 'groovy-backports', 'restricted', 'amd64') groovy_backports_universe_amd64 = build_repo('20.10 Groovy Backports (universe/amd64)', 'groovy-backports', 'universe', 'amd64') groovy_backports_multiverse_amd64 = build_repo('20.10 Groovy Backports (multiverse/amd64)', 'groovy-backports', 'multiverse', 'amd64') groovy_security_main_amd64 = build_repo('20.10 Groovy Updates (main/amd64)', 'groovy-security', 'main', 'amd64') groovy_security_restricted_amd64 = build_repo('20.10 Groovy Updates (restricted/amd64)', 'groovy-security', 'restricted', 'amd64') groovy_security_universe_amd64 = build_repo('20.10 Groovy Updates (universe/amd64)', 'groovy-security', 'universe', 'amd64') groovy_security_multiverse_amd64 = build_repo('20.10 Groovy Updates (multiverse/amd64)', 'groovy-security', 'multiverse', 'amd64')