-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgenerate_cache.py
More file actions
executable file
·363 lines (295 loc) · 11.2 KB
/
generate_cache.py
File metadata and controls
executable file
·363 lines (295 loc) · 11.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
#!/usr/bin/env spack-python
# This script will generate package metadata files for
# each package in the latest version of spack
#
# Usage:
# python generate_cache.py
from dataclasses import dataclass
from collections import defaultdict
from functools import lru_cache
from pathlib import Path
from typing import Iterable, Literal
import json
import os
import requests
import re
import yaml
import spack.database
import spack.spec
from spack.database import _DB_DIRNAME
here = Path(os.getcwd())
db_root = here / "spack-db"
INDEX_URL = "https://binaries.spack.io/cache_spack_io_index.json"
MUTABLE_REFS = ["develop"]
INDEX_BUCKET_MATCHER = re.compile(r"^s3://([^\/]+)/(.+)$")
BUCKET_URLS = {
"spack-binaries": "https://binaries.spack.io",
}
# Template for cache data
template = """---
title: "%s"
layout: cache
categories: [package, %s]
meta: %s
spec_details: %s
---"""
tag_page_template = """---
layout: table
permalink: /tag/%s/
tag: %s
---"""
def binary_size(spec: spack.spec.Spec) -> int | Literal["-"]:
# TODO: blocked on this value being baked into the build cache index.json
return "-"
@dataclass(frozen=True)
class Stack:
label: str
url: str
@lru_cache
def get_build_cache_index() -> dict[str, list[Stack]]:
r = requests.get(INDEX_URL)
r.raise_for_status()
return {k: [Stack(**x) for x in v] for k, v in r.json().items()}
def get_hash_stacks(entry: str, stacks: Iterable[Stack]) -> dict[str, set[str]]:
hash_stacks = defaultdict(set)
for stack in stacks:
r = requests.get(
stack.url.replace("s3://spack-binaries/", "https://binaries.spack.io/")
)
if r.status_code == 404:
print(f"No build cache for {entry} {stack} ({stack.url})")
continue
else:
r.raise_for_status()
for hash in r.json()["database"]["installs"].keys():
hash_stacks[hash].add(stack.label)
return hash_stacks
def write_cache_entries(name, specs, hash_stacks):
"""
Given a named list of specs, write markdown and json to cache output directory.
"""
# For each spec, write to the _cache folder
for package_name, speclist in specs.items():
# Keep a set of summary metrics for a package
metrics = {
"versions": set(),
"compilers": set(),
"oss": set(),
"platforms": set(),
"targets": set(),
"stacks": set(),
"num_specs": 0,
"num_specs_by_stack": defaultdict(int),
}
package_dir = here / "_cache" / name / package_name
if not os.path.exists(package_dir):
os.makedirs(package_dir)
spec_details = []
for i, spec in enumerate(speclist):
compiler_str = compiler_str_from_spec(spec)
metrics["oss"].add(spec.architecture.os)
metrics["platforms"].add(spec.architecture.platform)
metrics["targets"].add(spec.architecture.target.name)
metrics["versions"].add(str(spec.version))
metrics["compilers"].add(compiler_str)
metrics["stacks"] |= hash_stacks[spec._hash]
metrics["num_specs"] += 1
for stack in hash_stacks[spec._hash]:
metrics["num_specs_by_stack"][stack] += 1
assert len(spec.versions) == 1, spec.versions
spec_details.append(
{
"hash": spec._hash,
"compiler": compiler_str,
"versions": [str(v) for v in spec.versions],
"os": spec.architecture.os,
"platform": spec.architecture.platform,
"target": spec.architecture.target.name,
"variants": [str(v) for v in spec.variants.values()],
"stacks": sorted(list(hash_stacks[spec._hash])),
"size": binary_size(spec),
}
)
metrics["oss"] = sorted(list(metrics["oss"]))
metrics["platforms"] = sorted(list(metrics["platforms"]))
metrics["targets"] = sorted(list(metrics["targets"]))
metrics["versions"] = sorted(list(metrics["versions"]))
metrics["compilers"] = sorted(list(metrics["compilers"]))
metrics["stacks"] = sorted(list(metrics["stacks"]))
spec_details = sorted(spec_details, key=lambda obj: obj["hash"])
render = template % (
package_name,
name,
json.dumps(metrics, sort_keys=True),
json.dumps(spec_details, sort_keys=True),
)
md_file = os.path.join(package_dir, "specs.md")
with open(md_file, "w") as fd:
fd.write(render)
def compiler_str_from_spec(spec: spack.spec.Spec) -> str:
try:
_ = spec.compiler
compiler_str = spec.format("{compiler.name}@{compiler.version}")
except AttributeError:
compiler_str = "none"
return compiler_str
def specs_by_package(name: str, url: str) -> dict[str, list[spack.spec.Spec]]:
"""
Given a named entry and a URL, load a spack database
"""
response = requests.get(url)
response.raise_for_status()
index = response.json()
# Write index.json to file
entry_db = os.path.join(db_root, name, _DB_DIRNAME)
if not os.path.exists(entry_db):
os.makedirs(entry_db)
with open(os.path.join(entry_db, "index.json"), "w") as outfile:
outfile.write(json.dumps(index, indent=4))
# yeah this is awkward <--- from @tgamblin :D
db = spack.database.Database(os.path.join(db_root, name))
# Organize specs by package
specs: dict[str, list[spack.spec.Spec]] = defaultdict(list)
# keep lookup of specs
with db.read_transaction():
db_specs = db.query_local(installed=False, in_buildcache=True)
if db_specs:
for spec in db_specs:
assert isinstance(spec.name, str) # appease type checker
specs[spec.name].append(spec)
return specs
def get_specs_metadata(specs: dict[str, list[spack.spec.Spec]]) -> dict:
"""
Given loaded specs, parse metadata and return dict lookup.
"""
# For funsies store top level metrics
updates = {}
parameters = {}
compilers = {}
count = 0
# For each package, generate a data page, including the spec.json
for package_name, speclist in specs.items():
for s in speclist:
count += 1
for node_spec in s.traverse():
spec = node_spec.to_node_dict()
for paramname, setting in spec["parameters"].items():
# Is true or not empty list
if setting:
if paramname not in parameters:
parameters[paramname] = 0
parameters[paramname] += 1
for key, value in spec["arch"].items():
# Target can have another level of nesting
if key == "target" and isinstance(value, dict):
value = "%s %s" % (value["vendor"], value["name"])
compiler = compiler_str_from_spec(node_spec)
if compiler not in compilers:
compilers[compiler] = 0
compilers[compiler] += 1
# For each meta, write to data file
updates["compilers"] = compilers
updates["parameters"] = parameters
updates["count"] = count
return updates
def load_existing_metadata() -> dict[str, dict]:
meta_path = here / "_data" / "meta.yaml"
try:
with open(meta_path) as fd:
return yaml.safe_load(fd.read())
except Exception:
print("No pre-existing metadata could be read")
return {}
def load_existing_tags() -> list[dict]:
tag_path = here / "_data" / "tags.yaml"
try:
with open(tag_path) as fd:
return yaml.safe_load(fd.read())
except Exception:
print("No pre-existing tags could be read")
return []
def s3_to_cloudfront(url):
m = INDEX_BUCKET_MATCHER.match(url)
if m:
bucket = m.group(1)
prefix = m.group(2)
if bucket in BUCKET_URLS:
return f"{BUCKET_URLS[bucket]}/{prefix}"
return url
def main():
# Metadata file will store all versions
meta: dict[str, dict] = load_existing_metadata()
tags = load_existing_tags()
tags_dir = here / "pages" / "tags"
tags_dir.mkdir(parents=True, exist_ok=True)
for name, stacks in get_build_cache_index().items():
for s in stacks:
if s.label == "root":
url = s3_to_cloudfront(s.url)
break
else:
print(f"Skipping {name} because it doesn't have a root stack")
continue
cache_path = here / "_cache" / name
if os.path.exists(cache_path) and name not in MUTABLE_REFS:
print(f"Skip parsing cache for {name}. We already have it, and it is immutable.")
continue
print(f"Parsing cache for {name}")
# Create spack database and load specs
print("Loading spack db")
specs = specs_by_package(name, url)
print("Getting hash stacks")
hash_stacks = get_hash_stacks(name, stacks)
# Get metadata for specs
print("Getting specs metadata")
meta[name] = get_specs_metadata(specs)
# Write jekyll files
print("Writing jekyll files")
write_cache_entries(name, specs, hash_stacks)
tag_entry = {"name": name, "stacks": sorted([s.label for s in stacks])}
for idx, tag_stacks in enumerate(tags):
if tag_stacks["name"] == name:
tags[idx] = tag_entry
break
else:
tags.append(tag_entry)
with open(f"pages/tags/{name}.md", "w") as f:
f.write(tag_page_template % (name, name))
with open("_data/tags.yaml", "w") as f:
# sort tags such that develop is first, named tags are next,
# and develop-* are last (but in reverse order)
def tag_sorter(item):
if item["name"] == "develop":
return 0, 0
elif match := re.match(r"^develop-(\d{4}-\d{2}-\d{2})$", item["name"]):
return 2, -int(match.group(1).replace("-", ""))
else:
return 1, -int(item["name"].replace("v", "").replace(".", ""))
tags = sorted(tags, key=tag_sorter)
yaml.dump(tags, f)
# Create the "all" group
meta["all"] = {"version": "all", "count": 0}
compilers = {}
parameters = {}
# Count total compilers, params, specs
for k, entry in meta.items():
if k == "all":
continue
meta["all"]["count"] += entry["count"]
for compiler, ccount in entry["compilers"].items():
if compiler not in compilers:
compilers[compiler] = 0
compilers[compiler] += ccount
for param, pcount in entry["parameters"].items():
if param not in parameters:
parameters[param] = 0
parameters[param] += pcount
meta["all"]["compiler_count"] = "{:,}".format(len(compilers))
meta["all"]["parameter_count"] = "{:,}".format(len(parameters))
# Save all metadata
meta_file = here / "_data" / "meta.yaml"
with open(meta_file, "w") as fd:
fd.write(yaml.dump(meta))
print("Done!\n\n")
if __name__ == "__main__":
main()