summaryrefslogtreecommitdiffstats
path: root/ci/release.py
blob: 624b2132bf2f22add9d116d6220d53f875d12933 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3

from contextlib import contextmanager
from dataclasses import asdict, dataclass
from datetime import datetime
from hashlib import sha256
from itertools import chain, repeat
from os import environ, linesep, scandir
from os.path import normcase
from pathlib import Path
from subprocess import check_call
from sys import stderr
from time import sleep
from tomllib import loads
from typing import Iterator
from urllib.error import HTTPError
from urllib.request import build_opener

from jinja2 import Environment, FileSystemLoader, StrictUndefined
from yaml import safe_load


@dataclass(frozen=True)
class _Project:
    repo: str
    version: str
    tag: str
    desc: str
    long_desc: str


_TOP_LEVEL = Path(__file__).resolve().parent.parent


def _walk(path: Path) -> Iterator[Path]:
    for s in scandir(path):
        p = Path(s)
        if s.is_dir():
            yield from _walk(p)
        else:
            yield p


def _load_values() -> _Project:
    tag = environ["GITHUB_REF"].removeprefix("refs/tags/")
    repo = environ["GITHUB_REPOSITORY"]
    repo_uri = f"https://github.com/{repo}"
    cargo = loads((_TOP_LEVEL / "Cargo.toml").read_text())
    vals = safe_load((_TOP_LEVEL / "ci" / "vars.yml").read_text())
    project = _Project(
        **{
            **vals,
            "repo": repo_uri,
            "version": cargo["package"]["version"],
            "tag": tag,
        }
    )
    return project


def _release(project: _Project) -> None:
    time = datetime.now().strftime("%Y-%m-%d_%H-%M")
    title = f"ci_{project.version}_{time}"
    body = (_TOP_LEVEL / "RELEASE_NOTES.md").read_text()
    message = f"{title}{linesep}{linesep}{body}"

    attachments = (normcase(p) for p in _walk(_TOP_LEVEL / "arts"))

    check_call(
        (
            "gh",
            "release",
            "create",
            "--notes",
            message,
            "--",
            project.tag,
            *attachments,
        )
    )


def _build_j2() -> Environment:
    j2 = Environment(
        enable_async=True,
        trim_blocks=True,
        lstrip_blocks=True,
        undefined=StrictUndefined,
        loader=FileSystemLoader(_TOP_LEVEL / "ci" / "templates"),
    )
    return j2


@contextmanager
def _git_ops() -> Iterator[Path]:
    pkgs = _TOP_LEVEL / "packages"
    token = environ["CI_TOKEN"]
    uri = f"https://ms-jpq:{token}@github.com/ms-jpq/homebrew-sad"
    email = "ci@ci.ci"
    username = "ci-bot"
    time = datetime.now().strftime("%Y-%m-%d %H:%M")
    msg = f"CI - {time}"

    check_call(("git", "clone", "--depth=1", uri, pkgs), cwd=_TOP_LEVEL)
    check_call(("git", "config", "--", "user.email", email), cwd=pkgs)
    check_call(("git", "config", "--", "user.name", username), cwd=pkgs)
    yield pkgs
    check_call(("git", "add", "-A"), cwd=pkgs)
    check_call(("git", "commit", "-m", msg), cwd=pkgs)
    check_call(("git", "push", "--force"), cwd=pkgs)


def _sha(uri: str) -> str:
    opener = build_opener()
    for _ in range(9):
        try:
            with opener.open(uri) as resp:
                body = resp.read()
        except HTTPError as e:
            print(uri, e, sep=linesep, file=stderr)
            sleep(9)
        else:
            sha = sha256(body).hexdigest()
            return sha
    else:
        raise TimeoutError()


def _template(project: _Project) -> None:
    prefix = f"{project.repo}/releases/download/{project.tag}"
    aarch64_uri = f"{prefix}/aarch64-apple-darwin.zip"
    x86_uri = f"{prefix}/x86_64-apple-darwin.zip"
    aarch64_sha = _sha(aarch64_uri)
    x86_sha = _sha(x86_uri)

    vals = {
        **asdict(project),
        "aarch64_uri": aarch64_uri,
        "aarch64_sha": aarch64_sha,
        "x86_sha": x86_sha,
        "x86_uri": x86_uri,
    }
    j2 = _build_j2()
    brew_rend = j2.get_template("homebrew.rb").render(**vals)
    snap_rend = j2.get_template("snapcraft.yml").render(**vals)

    with _git_ops() as cwd:
        (cwd / "sad.rb").write_text(brew_rend)
        (cwd / "snapcraft.yaml").write_text(snap_rend)


def main() -> None:
    project = _load_values()
    _release(project)
    _template(project)


main()