Last active
February 7, 2026 17:07
-
-
Save simonLeary42/af948bb40f130d6cc4fae112fa2a7b3a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| import sys | |
| import json | |
| from json import JSONDecodeError | |
| from urllib import request | |
| from urllib.error import HTTPError | |
| from http.client import HTTPResponse | |
| """ | |
| helps locate release files from github | |
| usage: | |
| python find-latest-github-release-file.py REPO_NAME_HERE REGEX_HERE | |
| python find-latest-github-release-file.py REPO_NAME_HERE | |
| if you call with no regex argument, it simply prints all out the filenames that a regex would match | |
| """ | |
| def get_url_json_decode(url) -> list[dict]: | |
| try: | |
| response: HTTPResponse = request.urlopen(url) | |
| except HTTPError as e: | |
| e.add_note(f"{url=}") | |
| raise e | |
| response_str = response.read() | |
| if response.status != 200: | |
| raise RuntimeError(f"{url=} {response.status=} {response.reason=} {response_str=}") | |
| try: | |
| return json.loads(response_str) | |
| except JSONDecodeError: | |
| e.add_note(f"{response_str=}") | |
| raise e | |
| if len(sys.argv) == 1: | |
| print("required arguments: repo, regex") | |
| sys.exit(1) | |
| elif len(sys.argv) == 2: | |
| repo = sys.argv[1] | |
| release_data = get_url_json_decode(f"https://api.github.com/repos/{repo}/releases/latest") | |
| print("regex argument not given. Here are the values your regex could match:") | |
| for asset in release_data["assets"]: | |
| print(asset["name"]) | |
| sys.exit(0) | |
| elif len(sys.argv) == 3: | |
| repo, pattern = sys.argv[1:] | |
| release_data = get_url_json_decode(f"https://api.github.com/repos/{repo}/releases/latest") | |
| matches = [ | |
| (x["name"], x["browser_download_url"]) | |
| for x in release_data["assets"] | |
| if re.search(pattern, x["name"]) | |
| ] | |
| if len(matches) == 0: | |
| print(f"regex '{pattern}' had 0 matches! Here are the values your pattern could match:") | |
| for asset in release_data["assets"]: | |
| print(asset["name"]) | |
| sys.exit(1) | |
| elif len(matches) == 1: | |
| print(matches[0][1]) | |
| sys.exit(0) | |
| else: | |
| print(f"regex '{pattern}' had multiple matches!") | |
| for match in matches: | |
| print(match[0]) | |
| sys.exit(1) | |
| else: | |
| print("too many arguments!") | |
| sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment