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