from io import TextIOWrapper from pathlib import Path import re import tarfile from typing import Mapping, TextIO from .base import Repository, Version __all__ = [ 'stable_main_x86_64', 'stable_community_x86_64', 'edge_main_x86_64', 'edge_community_x86_64', 'edge_testing_x86_64', ] PACKAGE_REVISION_INFO = re.compile(r'-r\d+$') def parse_apkindex(apkindex: TextIO) -> Mapping[str, Version]: result = dict() current_package = None current_version = None ignore_lines = ['C', 'A', 'S', 'I', 'T', 'U', 'L', 'o', 'm', 't', 'c', 'D', 'p', 'i', 'k'] for line in apkindex: line = line.strip() if len(line) == 0: if current_package is not None and current_version is not None: result[current_package] = current_version current_package = None current_version = None continue try: line_type, line_data = line.split(':', 1) except ValueError: print('what uhhhh the fuck', line, line.split(':', 1)) continue if line_type == 'C': # TODO figure out what this means pass elif line_type == 'P': current_package = line_data elif line_type == 'V': version = line_data clean_version = PACKAGE_REVISION_INFO.sub('', version) current_version = Version(version, clean_version) elif line_type in ignore_lines: pass else: raise ValueError('unknown line type: ' + line_type + ' in line ' + repr(line)) return result def parse_cached(cached: Path) -> Mapping[str, Version]: apkindex = tarfile.open(cached) for archive_member in apkindex.getmembers(): if archive_member.name == 'APKINDEX': apkindex_file = apkindex.extractfile(archive_member) apkindex_file = TextIOWrapper(apkindex_file) return parse_apkindex(apkindex_file) def build_repo(name: str, section: str, url_path: str) -> Repository: url = f'http://dl-cdn.alpinelinux.org/alpine/{url_path}/APKINDEX.tar.gz' return Repository( family='Alpine Linux', repo=name, section=section, index_url=url, parse=parse_cached, ) stable_main_x86_64 = build_repo('Stable', 'main/x86_64', 'latest-stable/main/x86_64') stable_community_x86_64 = build_repo('Stable', 'community/x86_64', 'latest-stable/community/x86_64') edge_main_x86_64 = build_repo('Edge', 'main/x86_64', 'edge/main/x86_64') edge_community_x86_64 = build_repo('Edge', 'community/x86_64', 'edge/community/x86_64') edge_testing_x86_64 = build_repo('Edge', 'testing/x86_64', 'edge/testing/x86_64')