|
| 1 | +import os |
| 2 | +import re |
| 3 | +import urllib.request |
| 4 | + |
| 5 | +import atoma |
| 6 | + |
| 7 | +try: |
| 8 | + from rich import print |
| 9 | +except ImportError: |
| 10 | + pass |
| 11 | + |
| 12 | +DEFINITIONS_FILE = "winbuild/build_prepare.py" |
| 13 | + |
| 14 | +DEPENDENCIES = [ |
| 15 | + # Name, GitHub slug, version prefix |
| 16 | + ("fribidi", "fribidi/fribidi", "v"), |
| 17 | + ("harfbuzz", "harfbuzz/harfbuzz", ""), |
| 18 | + ("openjpeg", "uclouvain/openjpeg", "v"), |
| 19 | +] |
| 20 | + |
| 21 | + |
| 22 | +def update_version(name: str, slug: str, version_prefix: str) -> str | None: |
| 23 | + url = f"https://github.com/{slug}/tags.atom" |
| 24 | + print(url) |
| 25 | + contents = urllib.request.urlopen(url).read() |
| 26 | + feed = atoma.parse_atom_bytes(contents) |
| 27 | + link = feed.entries[0].links[0].href |
| 28 | + print(link) |
| 29 | + new_tag = link.rsplit("/", 1)[1] |
| 30 | + print(f"{new_tag=}") |
| 31 | + new_version = new_tag.removeprefix(version_prefix) |
| 32 | + print(f"{new_version=}") |
| 33 | + |
| 34 | + with open(DEFINITIONS_FILE) as f: |
| 35 | + content = f.read() |
| 36 | + content_new = re.sub( |
| 37 | + rf"https://github.com/{slug}/archive/{version_prefix}\d+\.\d+\.\d+.zip", |
| 38 | + f"https://github.com/{slug}/archive/{version_prefix}{new_version}.zip", |
| 39 | + content, |
| 40 | + ) |
| 41 | + content_new = re.sub( |
| 42 | + rf"{name}-\d+\.\d+\.\d+", |
| 43 | + rf"{name}-{new_version}", |
| 44 | + content_new, |
| 45 | + ) |
| 46 | + changes_made = content != content_new |
| 47 | + print(f"{changes_made=}") |
| 48 | + print() |
| 49 | + |
| 50 | + if changes_made: |
| 51 | + # Write the file out again |
| 52 | + with open(DEFINITIONS_FILE, "w") as f: |
| 53 | + f.write(content_new) |
| 54 | + return f"{name} to {new_version}" |
| 55 | + |
| 56 | + return None |
| 57 | + |
| 58 | + |
| 59 | +def main() -> None: |
| 60 | + updates = [] |
| 61 | + for name, slug, v_in_tag in DEPENDENCIES: |
| 62 | + update = update_version(name, slug, v_in_tag) |
| 63 | + if update: |
| 64 | + updates.append(update) |
| 65 | + |
| 66 | + if updates: |
| 67 | + commit_message = "Update " + ", ".join(updates) |
| 68 | + print(commit_message) |
| 69 | + |
| 70 | + github_env_file = os.getenv("GITHUB_ENV") |
| 71 | + if github_env_file: |
| 72 | + with open(github_env_file, "a") as f: |
| 73 | + f.write(f"COMMIT_MESSAGE={commit_message}") |
| 74 | + else: |
| 75 | + print("No updates") |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
0 commit comments