|
| 1 | +import re |
| 2 | +import subprocess |
| 3 | +import requests |
| 4 | +import sys |
| 5 | +import pathlib |
| 6 | + |
| 7 | +module_bazel_path = sys.argv[1] |
| 8 | +cache_dir = pathlib.Path(__file__).parent.joinpath("cache") |
| 9 | +cache_dir.mkdir(exist_ok=True) |
| 10 | + |
| 11 | +original = open(module_bazel_path).read() |
| 12 | +begin_shas = re.search("# BEGIN SHAS\n", original).end() # pyright: ignore[reportOptionalMemberAccess] |
| 13 | +end_shas = re.search("\n # END SHAS", original).start() # pyright: ignore[reportOptionalMemberAccess] |
| 14 | +print(begin_shas, end_shas) |
| 15 | +sha_pattern = re.compile(r"\"(.+\.tar\.xz)\": \"([0-9a-f]+)\"") |
| 16 | + |
| 17 | +results = "" |
| 18 | + |
| 19 | +for entry in sha_pattern.finditer(original, begin_shas, end_shas): |
| 20 | + short_url, hash = entry.groups() |
| 21 | + cache_path = cache_dir.joinpath(short_url.replace("/", "_")) |
| 22 | + if not cache_path.exists(): |
| 23 | + full_url = f"https://static.rust-lang.org/dist/{short_url}" |
| 24 | + print("getting", full_url, cache_path) |
| 25 | + req = requests.get(full_url) |
| 26 | + with cache_path.open("wb") as f: |
| 27 | + f.write(req.content) |
| 28 | + sha256_cmd = subprocess.check_output(["sha256sum", cache_path.as_posix()], encoding="utf-8") |
| 29 | + sha256 = sha256_cmd.split(" ")[0] |
| 30 | + if results != "": |
| 31 | + results += "\n" |
| 32 | + results += f" \"{short_url}\": \"{sha256}\"," |
| 33 | + |
| 34 | +revised = original[:begin_shas] + results + original[end_shas:] |
| 35 | +with open(module_bazel_path, "w") as f: |
| 36 | + f.write(revised) |
0 commit comments