aboutsummaryrefslogtreecommitdiff
path: root/repos/alpine_linux.py
blob: 7c646cc7ffa8f425623ddcea3698a171f9fc2b1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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, url_path: str) -> Repository:
    url = f'http://dl-cdn.alpinelinux.org/alpine/{url_path}/APKINDEX.tar.gz'
    return Repository(
        family='Alpine Linux',
        repo=name,
        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')