|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +""" |
| 4 | +This script updates the version of tool binaries defined in a Bazel rules_multitool lockfile. |
| 5 | +If the tool has binaries hosted in a public GitHub repo's Release assets, it will update the |
| 6 | +lockfile's URL and hash to the latest versions, otherwise it will skip it. |
| 7 | +
|
| 8 | +See: https://github.com/theoremlp/rules_multitool |
| 9 | +
|
| 10 | +Requires: |
| 11 | + - github module (pip install PyGithub) |
| 12 | +
|
| 13 | +``` |
| 14 | +usage: update_multitool_binaries.py [-h] [--file LOCKFILE_PATH] |
| 15 | +
|
| 16 | +options: |
| 17 | + -h, --help show this help message and exit |
| 18 | + --file LOCKFILE_PATH path to multitool lockfile (defaults to 'multitool.lock.json' in current directory) |
| 19 | +``` |
| 20 | +""" |
| 21 | + |
| 22 | +import argparse |
| 23 | +import hashlib |
| 24 | +import json |
| 25 | +import os |
| 26 | +import re |
| 27 | +import urllib.request |
| 28 | + |
| 29 | +try: |
| 30 | + from github import Github |
| 31 | +except ModuleNotFoundError: |
| 32 | + exit("requires github module (run: pip install PyGithub)") |
| 33 | + |
| 34 | + |
| 35 | +def run(lockfile_path): |
| 36 | + with open(lockfile_path) as f: |
| 37 | + data = json.load(f) |
| 38 | + |
| 39 | + for tool in [tool for tool in data if tool != "$schema"]: |
| 40 | + version = re.search(f"download/(.*?)/{tool}", data[tool]["binaries"][0]["url"])[ |
| 41 | + 1 |
| 42 | + ] |
| 43 | + match = re.search( |
| 44 | + f"github.com/(.*?)/releases", data[tool]["binaries"][0]["url"] |
| 45 | + ) |
| 46 | + if match: |
| 47 | + user_repo = match[1] |
| 48 | + else: |
| 49 | + continue |
| 50 | + try: |
| 51 | + new_version = Github().get_repo(user_repo).get_releases()[0].title |
| 52 | + except Exception: |
| 53 | + continue |
| 54 | + if new_version != version: |
| 55 | + print(f"found new version of '{tool}': {new_version}") |
| 56 | + for binary in data[tool]["binaries"]: |
| 57 | + new_url = binary["url"].replace(version, new_version) |
| 58 | + try: |
| 59 | + with urllib.request.urlopen(new_url) as response: |
| 60 | + sha256_hash = hashlib.sha256() |
| 61 | + sha256_hash.update(response.read()) |
| 62 | + new_hash = sha256_hash.hexdigest() |
| 63 | + binary["url"] = new_url |
| 64 | + binary["sha256"] = new_hash |
| 65 | + except Exception: |
| 66 | + continue |
| 67 | + |
| 68 | + with open(lockfile_path, "w") as f: |
| 69 | + json.dump(data, f, indent=2) |
| 70 | + |
| 71 | + print(f"\ngenerated new '{lockfile_path}' with updated urls and hashes") |
| 72 | + |
| 73 | + |
| 74 | +def main(): |
| 75 | + parser = argparse.ArgumentParser() |
| 76 | + parser.add_argument( |
| 77 | + "--file", |
| 78 | + dest="lockfile_path", |
| 79 | + default=os.path.join(os.getcwd(), "multitool.lock.json"), |
| 80 | + help="path to multitool lockfile (defaults to 'multitool.lock.json' in current directory)", |
| 81 | + ) |
| 82 | + args = parser.parse_args() |
| 83 | + run(args.lockfile_path) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments