|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import requests |
| 4 | +import argparse |
| 5 | +import os |
| 6 | +import re |
| 7 | +import json |
| 8 | +from typing import NamedTuple, Optional, TextIO |
| 9 | + |
| 10 | +# GitHub repository details |
| 11 | +REPO = "TravisWheelerLab/mdr" |
| 12 | +API_URL = f"https://api.github.com/repos/{REPO}/releases" |
| 13 | + |
| 14 | + |
| 15 | +class ReleaseInfo(NamedTuple): |
| 16 | + """ Release info """ |
| 17 | + os: str |
| 18 | + arch: str |
| 19 | + |
| 20 | + |
| 21 | +class Args(NamedTuple): |
| 22 | + """ Command-line args """ |
| 23 | + version: str |
| 24 | + json: Optional[TextIO] |
| 25 | + |
| 26 | + |
| 27 | +# -------------------------------------------------- |
| 28 | +def get_args() -> Args: |
| 29 | + parser = argparse.ArgumentParser( |
| 30 | + description="Update a specific release in GitHub." |
| 31 | + ) |
| 32 | + |
| 33 | + parser.add_argument( |
| 34 | + "version", help="The tag name of the release to update" |
| 35 | + ) |
| 36 | + |
| 37 | + parser.add_argument( |
| 38 | + "-j", |
| 39 | + "--json", |
| 40 | + type=argparse.FileType("rt"), |
| 41 | + help="Local JSON file" |
| 42 | + ) |
| 43 | + |
| 44 | + args = parser.parse_args() |
| 45 | + |
| 46 | + return Args(version=args.version, json=args.json) |
| 47 | + |
| 48 | + |
| 49 | +# -------------------------------------------------- |
| 50 | +def main() -> None: |
| 51 | + args = get_args() |
| 52 | + releases = get_releases_data(args.json) |
| 53 | + |
| 54 | + if release := find_release_by_version(releases, args.version): |
| 55 | + print("Release '{}' has {} assets".format(args.version, len(release["assets"]))) |
| 56 | + markdown_table = generate_markdown_table(release) |
| 57 | + update_release_body(release["id"], markdown_table) |
| 58 | + print(f"Release '{args.version}' updated successfully.") |
| 59 | + else: |
| 60 | + print(f"Release version '{args.version}' not found.") |
| 61 | + |
| 62 | + |
| 63 | +# -------------------------------------------------- |
| 64 | +def get_releases_data(file: Optional[TextIO]): |
| 65 | + if file: |
| 66 | + return json.loads(file.read()) |
| 67 | + |
| 68 | + headers = { |
| 69 | + "Authorization": f'token {os.getenv("GITHUB_TOKEN")}', |
| 70 | + "Accept": "application/vnd.github.v3+json", |
| 71 | + } |
| 72 | + response = requests.get(API_URL, headers=headers) |
| 73 | + response.raise_for_status() |
| 74 | + return response.json() |
| 75 | + |
| 76 | + |
| 77 | +# -------------------------------------------------- |
| 78 | +def find_release_by_version(releases, version): |
| 79 | + for release in releases: |
| 80 | + if release["tag_name"] == version: |
| 81 | + return release |
| 82 | + return None |
| 83 | + |
| 84 | + |
| 85 | +# -------------------------------------------------- |
| 86 | +def extract_os_arch_from_filename(filename) -> Optional[ReleaseInfo]: |
| 87 | + pattern = re.compile(r"^(.+)(?:\.tar\.gz|\.zip)$") |
| 88 | + |
| 89 | + if match := pattern.search(filename): |
| 90 | + stem = match.group(1) |
| 91 | + _, arch, os = stem.split("-", 2) |
| 92 | + |
| 93 | + if os == "macos-latest" or os == "apple-darwin": |
| 94 | + os = "MacOS" |
| 95 | + elif os == "ubuntu-latest": |
| 96 | + os = "Ubuntu" |
| 97 | + elif re.search("linux", os): |
| 98 | + os = "Linux" |
| 99 | + elif re.search("windows", os): |
| 100 | + os = "Windows" |
| 101 | + |
| 102 | + if arch == "x64" or arch == "x86_64": |
| 103 | + arch = "Intel/AMD 64-bit" |
| 104 | + elif arch == "386": |
| 105 | + arch = "Intel/AMD 32-bit" |
| 106 | + elif arch == "arm64": |
| 107 | + if os == "MacOS": |
| 108 | + arch = "M1/M2/M3 (ARM 64-bit)" |
| 109 | + else: |
| 110 | + arch = "ARM 64-bit" |
| 111 | + elif arch == "arm": |
| 112 | + arch = "ARM 32-bit" |
| 113 | + |
| 114 | + return ReleaseInfo(os, arch) |
| 115 | + |
| 116 | + |
| 117 | +# -------------------------------------------------- |
| 118 | +def generate_markdown_table(release) -> str: |
| 119 | + table = "### Release Assets\n" |
| 120 | + table += "| OS | Architecture | Link |\n" |
| 121 | + table += "|---------|----------|-------------|\n" |
| 122 | + |
| 123 | + for asset in release["assets"]: |
| 124 | + if info := extract_os_arch_from_filename(asset["name"]): |
| 125 | + print(">>> Asset {}".format(asset["name"])) |
| 126 | + download_url = asset["browser_download_url"] |
| 127 | + table += f"| {info.os} | {info.arch} | [Download]({download_url}) |\n" |
| 128 | + |
| 129 | + # Add note about Mac binary signing restriction |
| 130 | + table += ( |
| 131 | + "\n***(For Macs) To address the Mac binary signing restriction, use the following command:\n" |
| 132 | + "\n```\n" |
| 133 | + "sudo xattr -dr com.apple.quarantine <path to file>/mdr\n" |
| 134 | + "```\n" |
| 135 | + ) |
| 136 | + |
| 137 | + return table |
| 138 | + |
| 139 | + |
| 140 | +# -------------------------------------------------- |
| 141 | +def update_release_body(release_id, new_body): |
| 142 | + headers = { |
| 143 | + "Authorization": f'token {os.getenv("GITHUB_TOKEN")}', |
| 144 | + "Accept": "application/vnd.github.v3+json", |
| 145 | + } |
| 146 | + update_url = f"https://api.github.com/repos/{REPO}/releases/{release_id}" |
| 147 | + data = {"body": new_body} |
| 148 | + |
| 149 | + response = requests.patch(update_url, headers=headers, json=data) |
| 150 | + response.raise_for_status() |
| 151 | + |
| 152 | + |
| 153 | +# -------------------------------------------------- |
| 154 | +if __name__ == "__main__": |
| 155 | + main() |
0 commit comments