aboutsummaryrefslogtreecommitdiff
path: root/repos/arch_linux.py
blob: c535484fc4054656518a7b41287aaab2ec89e1f2 (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
74
75
from io import TextIOWrapper
from pathlib import Path
import re
import tarfile
from typing import Mapping, TextIO, Tuple

from .base import Repository, Version

__all__ = [
    'core',
    'community',
    'community_testing',
    'extra',
    'gnome_unstable',
    'kde_unstable',
    'multilib',
    'multilib_testing',
    'testing',
]

PACKAGE_REVISION_INFO = re.compile(r'(^\d+:)|(-\d+$)')

def parse_desc(desc_file: TextIO) -> Tuple[str, Version]:
    name = None
    version = None
    next_line_is = None
    for line in desc_file:
        line = line.strip()
        if len(line) == 0:
            continue
        if line.startswith('%') and line.endswith('%'):
            next_line_is = line.strip('%')
            continue
        if next_line_is == 'NAME':
            name = line
        elif next_line_is == 'VERSION':
            version = line
        next_line_is = None

        if name is not None and version is not None:
            clean_version = PACKAGE_REVISION_INFO.sub('', version)
            return name, Version(version, clean_version)

def parse_cached(cached: Path) -> Mapping[str, Version]:
    db = tarfile.open(cached)
    result = dict()
    for archive_member in db.getmembers():
        if archive_member.name.endswith('/desc') and archive_member.isfile():
            desc_file = db.extractfile(archive_member)
            desc_file = TextIOWrapper(desc_file)
            name, version = parse_desc(desc_file)
            result[name] = version
    return result

def build_repo(name: str) -> Repository:
    url = f'https://mirror.rackspace.com/archlinux/{name}/os/x86_64/{name}.db.tar.gz'
    return Repository(
        family=None,
        repo='Arch Linux',
        section=name,
        index_url=url,
        parse=parse_cached,
    )

core = build_repo('core')
community = build_repo('community')
community_testing = build_repo('community-testing')
extra = build_repo('extra')
gnome_unstable = build_repo('gnome-unstable')
kde_unstable = build_repo('kde-unstable')
multilib = build_repo('multilib')
multilib_testing = build_repo('multilib-testing')
testing = build_repo('testing')

# TODO figure out how to grab this info from AUR