Repository navigation
Expand file tree
/
Copy pathbump-nightly.py
More file actions
166 lines (127 loc) · 6.55 KB
/
Copy pathbump-nightly.py
File metadata and controls
166 lines (127 loc) · 6.55 KB
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
159
160
161
162
163
164
165
166
#!/usr/bin/env python3
"""Pin the OpenMS formulae to an OpenMS commit (normally the tip of the `nightly` branch).
Everything OpenMS would otherwise download at configure time (FetchContent
dependencies, PeptDeep ONNX models) is read from OpenMS's own CMake files at that
commit and rendered as Homebrew `resource` blocks, so the build runs offline and
the pins always match what OpenMS expects.
All formulae get the same source block (the application layers require the exact
libopenms version they are built from); resource blocks are only rendered into
formulae that have the nightly-resources markers (libopenms).
Usage: bump-nightly.py [--sha SHA] [--formula PATH ...]
Prints `changed=`, `version=` and `sha=` lines suitable for $GITHUB_OUTPUT.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import os
import re
import sys
import urllib.request
from datetime import datetime, timezone
from pathlib import Path
REPO = "OpenMS/OpenMS"
BRANCH = "nightly"
FORMULAE = ["Formula/libopenms.rb", "Formula/openms.rb", "Formula/openms-gui.rb"]
# FetchContent names handed to CMake via FETCHCONTENT_SOURCE_DIR_<NAME>.
FETCHCONTENT_DEPS = ["opentims", "pylmcf", "wnet", "wnetalign"]
def http_get(url: str) -> bytes:
headers = {"User-Agent": "homebrew-openms-bump"}
token = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
if token and url.startswith("https://api.github.com/"):
headers["Authorization"] = f"Bearer {token}"
with urllib.request.urlopen(urllib.request.Request(url, headers=headers), timeout=600) as r:
return r.read()
def sha256_of(url: str) -> str:
return hashlib.sha256(http_get(url)).hexdigest()
def raw(sha: str, path: str) -> str:
return http_get(f"https://raw.githubusercontent.com/{REPO}/{sha}/{path}").decode()
def resolve_commit(ref: str) -> tuple[str, datetime]:
data = json.loads(http_get(f"https://api.github.com/repos/{REPO}/commits/{ref}"))
date = datetime.fromisoformat(data["commit"]["committer"]["date"].replace("Z", "+00:00"))
return data["sha"], date.astimezone(timezone.utc)
def cmake_set(text: str, var: str) -> str:
m = re.search(rf'set\(\s*{var}\s+"?([^")\s]*)"?', text)
if not m:
sys.exit(f"could not find {var} in OpenMS CMake files")
return m.group(1)
def cmake_list(text: str, var: str) -> list[str]:
m = re.search(rf"set\(\s*{var}\s+(.*?)\s+CACHE", text, re.S)
if not m:
sys.exit(f"could not find list {var} in OpenMS CMake files")
return m.group(1).split()
def fetchcontent_pins(text: str) -> dict[str, tuple[str, str]]:
pins = {}
for m in re.finditer(r"FetchContent_Declare\(\s*(\w+)(.*?)\n\s*\)", text, re.S):
name, body = m.group(1).lower(), m.group(2)
repo = re.search(r"GIT_REPOSITORY\s+(\S+)", body)
tag = re.search(r"GIT_TAG\s+(\S+)", body)
if repo and tag:
pins[name] = (repo.group(1).removesuffix(".git"), tag.group(1))
return pins
def formula_field(text: str, field: str) -> str | None:
m = re.search(rf'^\s*{field} "([^"]+)"', text, re.M)
return m.group(1) if m else None
def has_markers(text: str, begin: str) -> bool:
return re.search(rf"^[ \t]*# {re.escape(begin)}", text, re.M) is not None
def replace_between(text: str, begin: str, end: str, lines: list[str]) -> str:
pattern = re.compile(
rf"(^([ \t]*)# {re.escape(begin)}[^\n]*\n).*?(^[ \t]*# {re.escape(end)}[^\n]*\n)", re.S | re.M
)
m = pattern.search(text)
if not m:
sys.exit(f"markers '{begin}'/'{end}' not found in formula")
indent = m.group(2)
body = "".join(f"{indent}{l}\n" if l else "\n" for l in lines)
return text[: m.start()] + m.group(1) + body + m.group(3) + text[m.end() :]
def main() -> None:
ap = argparse.ArgumentParser()
ap.add_argument("--sha", default="", help="OpenMS commit to pin (default: tip of nightly)")
ap.add_argument("--formula", action="append", help=f"formula to update (default: {' '.join(FORMULAE)})")
args = ap.parse_args()
sha, date = resolve_commit(args.sha or BRANCH)
formulae = {Path(f): Path(f).read_text() for f in (args.formula or FORMULAE)}
first = next(iter(formulae.values()))
if all(sha in (formula_field(t, "url") or "") for t in formulae.values()):
print("changed=false")
print(f"version={formula_field(first, 'version')}")
print(f"sha={sha}")
return
top = raw(sha, "CMakeLists.txt")
ext = raw(sha, "cmake/cmake_findExternalLibs.cmake")
lib = raw(sha, "src/openms/CMakeLists.txt")
base = ".".join(cmake_set(top, f"OPENMS_PACKAGE_VERSION_{p}") for p in ("MAJOR", "MINOR", "PATCH"))
version = f"{base}-pre.{date:%Y%m%d}"
if any((formula_field(t, "version") or "").startswith(version) for t in formulae.values()):
# More than one build on the same day: disambiguate with the commit time.
version = f"{base}-pre.{date:%Y%m%d.%H%M}"
url = f"https://github.com/{REPO}/archive/{sha}.tar.gz"
source = [f'url "{url}"', f'version "{version}"', f'sha256 "{sha256_of(url)}"']
pins = fetchcontent_pins(ext)
res: list[str] = []
for name in FETCHCONTENT_DEPS:
if name not in pins:
sys.exit(f"FetchContent dependency '{name}' is no longer declared by OpenMS; update FETCHCONTENT_DEPS")
repo, tag = pins[name]
ref = tag if re.fullmatch(r"[0-9a-f]{40}", tag) else f"refs/tags/{tag}"
r_url = f"{repo}/archive/{ref}.tar.gz"
res += [f'resource "{name}" do', f' url "{r_url}"', f' sha256 "{sha256_of(r_url)}"', "end", ""]
model_url = cmake_set(lib, "OPENMS_PEPTDEEP_MODEL_URL").replace("http://", "https://", 1)
models = cmake_list(lib, "OPENMS_PEPTDEEP_MODELS")
hashes = cmake_list(lib, "OPENMS_PEPTDEEP_MODEL_SHA256S")
if len(models) != len(hashes):
sys.exit("PeptDeep model and checksum lists differ in length")
for model, digest in zip(models, hashes):
res += [f'resource "{model}" do', f' url "{model_url}/{model}"', f' sha256 "{digest}"', "end", ""]
for path, text in formulae.items():
text = replace_between(text, "BEGIN nightly-source", "END nightly-source", source)
if has_markers(text, "BEGIN nightly-resources"):
text = replace_between(text, "BEGIN nightly-resources", "END nightly-resources", res[:-1])
# Bottles belong to the previous commit; pr-upload writes a fresh block.
text = re.sub(r"^ bottle do\n.*?^ end\n\n", "", text, count=1, flags=re.S | re.M)
path.write_text(text)
print("changed=true")
print(f"version={version}")
print(f"sha={sha}")
if __name__ == "__main__":
main()