aboutsummaryrefslogtreecommitdiff
path: root/repos/homebrew.py
blob: 42e2e7c538b5215a6f6ee9396e0538e1cb5cecd9 (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
import json
from pathlib import Path
from typing import Mapping

from .base import Repository, Version

__all__ = [
    'core',
    'linux',
    'cask',
]

def parse_cached(cached: Path) -> Mapping[str, Version]:
    with cached.open('r') as json_file:
        formulae = json.load(json_file)
    result = dict()
    for formula in formulae:
        if 'full_name' in formula:
            name = formula['full_name']
        elif 'full_token' in formula:
            name = formula['full_token']
        else:
            print('no name', formula)
            raise ValueError()
        if 'version' in formula:
            version = formula['version']
        elif 'versions' in formula:
            versions = formula['versions']
            if 'stable' in versions:
                version = versions['stable']
            else:
                print('no stable versions', versions)
                raise ValueError()
        else:
            print('no versions??', formula)
            raise ValueError()
        result[name] = Version(version, version)
    return result

def build_repo(name: str, url: str) -> Repository:
    url = f'https://formulae.brew.sh/api/{url}.json'
    return Repository(
        family='Homebrew',
        repo=name,
        section=None,
        index_url=url,
        parse=parse_cached,
    )

core = build_repo('Core', 'formula')
linux = build_repo('Linux', 'formula-linux')
cask = build_repo('Cask', 'cask')