11#!/usr/bin/env python
22
33"""
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.
4+ This script updates the version of tool binaries defined in a Bazel rules_multitool lockfile. If the tool has binaries
5+ hosted in a public GitHub repo's Release assets, it will update the lockfile's URL and hash to the latest versions,
6+ otherwise it will skip it.
77
88See: https://github.com/theoremlp/rules_multitool
99
10- Requires:
11- - github module (pip install PyGithub)
12-
13- ```
10+ -----------------------------------------------------------------------------------------------------------
1411usage: update_multitool_binaries.py [-h] [--file LOCKFILE_PATH]
1512
1613options:
1714 -h, --help show this help message and exit
1815 --file LOCKFILE_PATH path to multitool lockfile (defaults to 'multitool.lock.json' in current directory)
19- ```
16+ -----------------------------------------------------------------------------------------------------------
2017"""
2118
2219import argparse
2623import re
2724import urllib .request
2825
29- try :
30- from github import Github
31- except ModuleNotFoundError :
32- exit ("requires github module (run: pip install PyGithub)" )
33-
3426
3527def run (lockfile_path ):
3628 with open (lockfile_path ) as f :
@@ -40,26 +32,25 @@ def run(lockfile_path):
4032 version = re .search (f"download/(.*?)/{ tool } " , data [tool ]["binaries" ][0 ]["url" ])[1 ]
4133 match = re .search ("github.com/(.*?)/releases" , data [tool ]["binaries" ][0 ]["url" ])
4234 if match :
43- user_repo = match [1 ]
35+ releases_url = f"https://api.github.com/repos/ { match [1 ]} /releases/latest"
4436 else :
4537 continue
4638 try :
47- new_version = Github ().get_repo (user_repo ).get_releases ()[0 ].title
39+ with urllib .request .urlopen (releases_url ) as response :
40+ json_resp = json .loads (response .read ())
41+ new_version = json_resp ["tag_name" ]
42+ assets = json_resp ["assets" ]
4843 except Exception :
4944 continue
5045 if new_version != version :
5146 print (f"found new version of '{ tool } ': { new_version } " )
47+ urls = [asset .get ("browser_download_url" ) for asset in assets ]
48+ hashes = [asset .get ("digest" ).split (":" )[1 ] for asset in assets ]
5249 for binary in data [tool ]["binaries" ]:
5350 new_url = binary ["url" ].replace (version , new_version )
54- try :
55- with urllib .request .urlopen (new_url ) as response :
56- sha256_hash = hashlib .sha256 ()
57- sha256_hash .update (response .read ())
58- new_hash = sha256_hash .hexdigest ()
59- binary ["url" ] = new_url
60- binary ["sha256" ] = new_hash
61- except Exception :
62- continue
51+ new_hash = hashes [urls .index (new_url )]
52+ binary ["url" ] = new_url
53+ binary ["sha256" ] = new_hash
6354
6455 with open (lockfile_path , "w" ) as f :
6556 json .dump (data , f , indent = 2 )
0 commit comments