-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild-db.py
More file actions
executable file
·442 lines (382 loc) · 12.2 KB
/
build-db.py
File metadata and controls
executable file
·442 lines (382 loc) · 12.2 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#!/usr/bin/env python3
import datetime
import hashlib
import json
import os
import base64
import subprocess
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
from concurrent.futures import ThreadPoolExecutor, as_completed
FORMULA_URL = "https://formulae.brew.sh/api/formula.json"
ANALYTICS_URL = "https://formulae.brew.sh/api/analytics/install/30d.json"
CACHE_DIR = "cache"
ECOSYSTEM = "brew.sh"
DB_PATH = "db.json"
SCHEMA_VERSION = 2
META_KEY = "__pkgdb_meta__"
PAYLOAD_KEY = "__pkgdb_payload__"
USER_AGENT = "brewx/0.1"
CHECK_INTERVAL_SECONDS = 24 * 60 * 60
DEFAULT_TIMEOUT = 60
MANIFEST_ACCEPT = "application/vnd.oci.image.index.v1+json"
TOKEN_SERVICE = "https://ghcr.io/token"
_GHCR_TOKENS = {}
def _ensure_cwd():
root = os.path.abspath(os.path.dirname(__file__))
cwd = os.path.abspath(os.getcwd())
if cwd != root:
print(
"build-db.py must be run from the repository root.",
file=sys.stderr,
)
sys.exit(2)
def _cache_path(url):
digest = hashlib.sha256(url.encode("utf-8")).hexdigest()
return os.path.join(CACHE_DIR, ECOSYSTEM, f"{digest}.json")
def _read_cached_json(url):
path = _cache_path(url)
if not os.path.exists(path):
raise FileNotFoundError(path)
with open(path, "rb") as handle:
data = json.load(handle)
if isinstance(data, dict) and META_KEY in data and PAYLOAD_KEY in data:
meta = data.get(META_KEY) or {}
return data.get(PAYLOAD_KEY), meta
return data, {}
def _write_cache(path, payload, etag, checked_at):
os.makedirs(os.path.dirname(path), exist_ok=True)
wrapper = {
META_KEY: {"etag": etag, "checked_at": checked_at},
PAYLOAD_KEY: payload,
}
with open(path, "w", encoding="utf-8") as handle:
json.dump(wrapper, handle)
def _fetch_json(url):
path = _cache_path(url)
payload = None
meta = {}
if os.path.exists(path):
payload, meta = _read_cached_json(url)
checked_at = meta.get("checked_at")
now = int(time.time())
if (
isinstance(checked_at, int)
and now - checked_at < CHECK_INTERVAL_SECONDS
):
return payload
headers = {"Accept": "application/json", "User-Agent": USER_AGENT}
parsed = urllib.parse.urlparse(url)
if parsed.hostname == "ghcr.io":
headers["Accept"] = MANIFEST_ACCEPT
repo = _ghcr_repo_from_url(parsed.path)
if repo:
token = _ghcr_bearer_token(repo)
if token:
headers["Authorization"] = f"Bearer {token}"
etag = meta.get("etag")
if etag:
headers["If-None-Match"] = etag
request = urllib.request.Request(url, headers=headers)
try:
with urllib.request.urlopen(
request,
timeout=DEFAULT_TIMEOUT,
) as response:
data = response.read()
etag = response.headers.get("etag")
payload = json.loads(data)
_write_cache(path, payload, etag, now)
return payload
except urllib.error.HTTPError as err:
if err.code == 404:
return None
if err.code == 304 and payload is not None:
_write_cache(path, payload, etag, now)
return payload
if payload is not None:
print(f"Using cached data for {url}: {err}", file=sys.stderr)
return payload
raise
except urllib.error.URLError as err:
if payload is not None:
print(f"Using cached data for {url}: {err}", file=sys.stderr)
return payload
raise
def _github_token():
token = os.environ.get("GITHUB_TOKEN") or os.environ.get("GH_TOKEN")
if token:
return token.strip()
try:
result = subprocess.run(
["gh", "auth", "token"],
check=True,
capture_output=True,
text=True,
)
except (FileNotFoundError, subprocess.CalledProcessError):
return None
token = result.stdout.strip()
if token:
return token
return None
def _github_username():
for key in ("GHCR_USERNAME", "GITHUB_ACTOR", "USER"):
value = os.environ.get(key)
if value:
return value.strip()
try:
result = subprocess.run(
["gh", "api", "user", "-q", ".login"],
check=True,
capture_output=True,
text=True,
)
except (FileNotFoundError, subprocess.CalledProcessError):
return None
username = result.stdout.strip()
if username:
return username
return None
def _ghcr_repo_from_url(path):
parts = [part for part in path.split("/") if part]
if len(parts) < 4 or parts[0] != "v2":
return None
return "/".join(parts[1:-2])
def _ghcr_bearer_token(repo):
now = int(time.time())
cached = _GHCR_TOKENS.get(repo)
if cached and cached["expires_at"] > now:
return cached["token"]
token = _github_token()
if not token:
return None
username = _github_username() or "x-access-token"
scope = f"repository:{repo}:pull"
query = urllib.parse.urlencode(
{"service": "ghcr.io", "scope": scope}
)
url = f"{TOKEN_SERVICE}?{query}"
basic = base64.b64encode(
f"{username}:{token}".encode("utf-8")
).decode("utf-8")
headers = {
"Authorization": f"Basic {basic}",
"User-Agent": USER_AGENT,
}
request = urllib.request.Request(url, headers=headers)
try:
with urllib.request.urlopen(
request,
timeout=DEFAULT_TIMEOUT,
) as response:
data = json.loads(response.read())
except urllib.error.HTTPError as err:
print(f"Failed to get GHCR token for {repo}: {err}", file=sys.stderr)
return None
bearer = data.get("token")
expires_in = data.get("expires_in", 300)
if bearer:
_GHCR_TOKENS[repo] = {
"token": bearer,
"expires_at": now + int(expires_in) - 10,
}
return bearer
return None
def _stable_version(stable):
if isinstance(stable, str):
return stable
if isinstance(stable, dict):
for key in ("version", "tag"):
value = stable.get(key)
if value:
return value
return None
def _manifest_url(formula):
name = formula.get("name")
versions = formula.get("versions", {})
stable = versions.get("stable")
version = _stable_version(stable)
if not name or not version:
return None
url = (
"https://ghcr.io/v2/homebrew/core/"
f"{name.replace('+', 'x')}/manifests/{version}"
)
revision = formula.get("revision")
stable_revision = None
if isinstance(stable, dict):
stable_revision = stable.get("revision")
revision_value = revision if revision is not None else stable_revision
if revision_value not in (None, 0):
url = f"{url}_{revision_value}"
rebuild = (
formula.get("bottle", {})
.get("stable", {})
.get("rebuild")
)
if rebuild:
url = f"{url}-{rebuild}"
return url
def _parse_count(value):
if value is None:
return None
if isinstance(value, int):
return value
if isinstance(value, str):
value = value.replace(",", "")
if value.isdigit():
return int(value)
return None
def _fetch_popularity():
payload = _fetch_json(ANALYTICS_URL)
items = payload.get("items") if isinstance(payload, dict) else None
popularity = {}
if not isinstance(items, list):
return popularity
for item in items:
if not isinstance(item, dict):
continue
formula = item.get("formula")
count = _parse_count(item.get("count"))
if formula and count is not None:
popularity[formula] = count
return popularity
def _parse_exec_paths(paths):
executables = set()
for entry in paths:
if not entry:
continue
entry = entry.strip()
if not entry:
continue
name = entry.rsplit("/", 1)[-1]
if name:
executables.add(name)
return executables
def _collect_entries(formulae, popularity_by_formula, manifests):
entries = {}
missing_manifests = 0
for formula in formulae:
if not isinstance(formula, dict):
continue
name = formula.get("name")
if not name or "@" in name:
continue
popularity = popularity_by_formula.get(name, 0)
url = _manifest_url(formula)
if not url:
continue
payload = manifests.get(url)
if not payload:
missing_manifests += 1
continue
manifest_list = payload.get("manifests", [])
executables = set()
for manifest in manifest_list:
annotations = None
if isinstance(manifest, dict):
annotations = manifest.get("annotations")
if not annotations:
continue
provides = annotations.get("sh.brew.path_exec_files")
if not provides:
continue
paths = [
item.strip()
for item in provides.split(",")
if item.strip()
]
executables.update(_parse_exec_paths(paths))
if executables:
break
for executable in executables:
entries.setdefault(executable, []).append(
{"formula": name, "popularity": popularity}
)
return entries, missing_manifests
def _sorted_entries(entries):
ordered = {}
for executable in sorted(entries.keys()):
items = entries[executable]
items.sort(
key=lambda item: (
-(item.get("popularity") or 0),
item.get("formula", ""),
)
)
top = items[0]["formula"] if items else None
if top:
ordered[executable] = top
return ordered
def main():
_ensure_cwd()
os.makedirs(os.path.join(CACHE_DIR, ECOSYSTEM), exist_ok=True)
formulae = _fetch_json(FORMULA_URL)
if not isinstance(formulae, list):
print("Formula list was not a list.", file=sys.stderr)
sys.exit(2)
popularity_by_formula = {}
try:
popularity_by_formula = _fetch_popularity()
except Exception as err:
print(f"Failed to fetch analytics data: {err}", file=sys.stderr)
manifest_urls = []
for formula in formulae:
if not isinstance(formula, dict):
continue
name = formula.get("name")
if not name or "@" in name:
continue
url = _manifest_url(formula)
if url:
manifest_urls.append(url)
manifests = {}
completed = 0
max_workers = min(32, (os.cpu_count() or 4) * 4)
with ThreadPoolExecutor(max_workers=max_workers) as executor:
future_map = {
executor.submit(_fetch_json, url): url for url in manifest_urls
}
for future in as_completed(future_map):
url = future_map[future]
try:
payload = future.result()
except Exception as err:
print(f"Failed to fetch {url}: {err}", file=sys.stderr)
continue
if payload:
manifests[url] = payload
completed += 1
if completed % 20 == 0:
print(
f"Fetched {completed}/{len(manifest_urls)} manifests...",
file=sys.stderr,
)
entries, missing_manifests = _collect_entries(
formulae,
popularity_by_formula,
manifests,
)
ordered_entries = _sorted_entries(entries)
db = {
"schema": SCHEMA_VERSION,
"generated_at": datetime.datetime.now(
datetime.timezone.utc
).isoformat(),
"entries": ordered_entries,
}
with open(DB_PATH, "w", encoding="utf-8") as handle:
json.dump(db, handle, indent=2, sort_keys=True)
handle.write("\n")
print(f"Wrote {DB_PATH} with {len(ordered_entries)} executables")
if missing_manifests:
print(
f"Skipped {missing_manifests} formulas missing cached manifests",
file=sys.stderr,
)
if __name__ == "__main__":
main()