-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathfix.py
More file actions
executable file
·55 lines (40 loc) · 1.4 KB
/
fix.py
File metadata and controls
executable file
·55 lines (40 loc) · 1.4 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
#!/usr/bin/env python3
# Copyright (c) 2024 GitHub, Inc.
"""
Fix metadata in overridden registry, updating `metadata.json` to list correct versions and `source.json`
to list correct patches with sha256 hashes.
"""
import pathlib
import json
import base64
import hashlib
import re
this_dir = pathlib.Path(__file__).resolve().parent
def sha256(file):
with open(file, 'rb') as input:
hash = hashlib.sha256(input.read()).digest()
hash = base64.b64encode(hash).decode()
return f"sha256-{hash}"
def patch_file(file, f):
try:
data = file.read_text()
except FileNotFoundError:
data = None
file.write_text(f(data))
def patch_json(file, **kwargs):
def update(data):
data = json.loads(data) if data else {}
data.update(kwargs)
return json.dumps(data, indent=4) + "\n"
patch_file(file, update)
for entry in this_dir.joinpath("modules").iterdir():
if not entry.is_dir():
continue
versions = [e for e in entry.iterdir() if e.is_dir()]
patch_json(entry / "metadata.json", versions=[v.name for v in versions])
for version in versions:
patch_json(version / "source.json", patches={
p.name: sha256(p) for p in version.joinpath("patches").iterdir()
})
patch_file(version / "MODULE.bazel",
lambda s: re.sub(r'''version\s*=\s*['"].*['"]''', f'version = "{version.name}"', s, 1))