|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# This file is part of pyAMReX. |
| 4 | +# |
| 5 | +# License: BSD-3-Clause-LBNL |
| 6 | + |
| 7 | +import argparse |
| 8 | +import datetime |
| 9 | +import json |
| 10 | +import os |
| 11 | +import sys |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import requests |
| 15 | + |
| 16 | + |
| 17 | +def update(args): |
| 18 | + # list of repositories to update |
| 19 | + repo_dict = {} |
| 20 | + if args.all or args.amrex: |
| 21 | + repo_dict["amrex"] = {} |
| 22 | + repo_dict["amrex"]["commit"] = ( |
| 23 | + "https://api.github.com/repos/AMReX-Codes/amrex/commits/development" |
| 24 | + ) |
| 25 | + repo_dict["amrex"]["tags"] = ( |
| 26 | + "https://api.github.com/repos/AMReX-Codes/amrex/tags" |
| 27 | + ) |
| 28 | + if args.all or args.pyamrex: |
| 29 | + repo_dict["pyamrex"] = {} |
| 30 | + repo_dict["pyamrex"]["commit"] = ( |
| 31 | + "https://api.github.com/repos/AMReX-Codes/pyamrex/commits/development" |
| 32 | + ) |
| 33 | + repo_dict["pyamrex"]["tags"] = ( |
| 34 | + "https://api.github.com/repos/AMReX-Codes/pyamrex/tags" |
| 35 | + ) |
| 36 | + |
| 37 | + # list of repositories labels for logging convenience |
| 38 | + repo_labels = { |
| 39 | + "amrex": "AMReX", |
| 40 | + "pyamrex": "pyAMReX", |
| 41 | + } |
| 42 | + |
| 43 | + # read from JSON file with dependencies data |
| 44 | + repo_dir = Path(__file__).parent.absolute() |
| 45 | + dependencies_file = os.path.join(repo_dir, "dependencies.json") |
| 46 | + try: |
| 47 | + with open(dependencies_file, "r") as file: |
| 48 | + dependencies_data = json.load(file) |
| 49 | + except Exception as e: |
| 50 | + print(f"An unexpected error occurred: {e}") |
| 51 | + sys.exit() |
| 52 | + |
| 53 | + # loop over repositories and update dependencies data |
| 54 | + for repo_name, repo_subdict in repo_dict.items(): |
| 55 | + print(f"\nUpdating {repo_labels[repo_name]}...") |
| 56 | + # set keys to access dependencies data |
| 57 | + commit_key = f"commit_{repo_name}" |
| 58 | + version_key = f"version_{repo_name}" |
| 59 | + # get new commit information |
| 60 | + commit_response = requests.get(repo_subdict["commit"]) |
| 61 | + commit_dict = commit_response.json() |
| 62 | + # set new commit |
| 63 | + repo_commit_sha = commit_dict["sha"] |
| 64 | + # get new version tag information |
| 65 | + tags_response = requests.get(repo_subdict["tags"]) |
| 66 | + tags_list = tags_response.json() |
| 67 | + # filter out old-format tags for specific repositories |
| 68 | + if repo_name == "amrex": |
| 69 | + tags_list_filtered = [ |
| 70 | + tag_dict |
| 71 | + for tag_dict in tags_list |
| 72 | + if (tag_dict["name"] != "boxlib" and tag_dict["name"] != "v2024") |
| 73 | + ] |
| 74 | + # set new version tag |
| 75 | + if repo_name == "pyamrex": |
| 76 | + # current date version for the pyAMReX release update |
| 77 | + repo_version_tag = datetime.date.today().strftime("%y.%m") |
| 78 | + else: |
| 79 | + # latest available tag (index 0) for all other dependencies |
| 80 | + repo_version_tag = tags_list_filtered[0]["name"] |
| 81 | + # use version tag instead of commit sha for a release update |
| 82 | + new_commit_sha = repo_version_tag if args.release else repo_commit_sha |
| 83 | + # update commit |
| 84 | + if repo_name != "pyamrex": |
| 85 | + print(f"- old commit: {dependencies_data[commit_key]}") |
| 86 | + print(f"- new commit: {new_commit_sha}") |
| 87 | + if dependencies_data[commit_key] == new_commit_sha: |
| 88 | + print("Skipping commit update...") |
| 89 | + else: |
| 90 | + print("Updating commit...") |
| 91 | + dependencies_data[f"commit_{repo_name}"] = new_commit_sha |
| 92 | + # update version |
| 93 | + print(f"- old version: {dependencies_data[version_key]}") |
| 94 | + print(f"- new version: {repo_version_tag}") |
| 95 | + if dependencies_data[version_key] == repo_version_tag: |
| 96 | + print("Skipping version update...") |
| 97 | + else: |
| 98 | + print("Updating version...") |
| 99 | + dependencies_data[f"version_{repo_name}"] = repo_version_tag |
| 100 | + |
| 101 | + # write to JSON file with dependencies data |
| 102 | + with open(dependencies_file, "w") as file: |
| 103 | + json.dump(dependencies_data, file, indent=4) |
| 104 | + |
| 105 | + |
| 106 | +if __name__ == "__main__": |
| 107 | + # define parser |
| 108 | + parser = argparse.ArgumentParser() |
| 109 | + |
| 110 | + # add arguments: AMReX option |
| 111 | + parser.add_argument( |
| 112 | + "--amrex", |
| 113 | + help="Update AMReX only", |
| 114 | + action="store_true", |
| 115 | + dest="amrex", |
| 116 | + ) |
| 117 | + |
| 118 | + # add arguments: pyAMReX option |
| 119 | + parser.add_argument( |
| 120 | + "--pyamrex", |
| 121 | + help="Update pyAMReX only", |
| 122 | + action="store_true", |
| 123 | + dest="pyamrex", |
| 124 | + ) |
| 125 | + |
| 126 | + # add arguments: release option |
| 127 | + parser.add_argument( |
| 128 | + "--release", |
| 129 | + help="New release", |
| 130 | + action="store_true", |
| 131 | + dest="release", |
| 132 | + ) |
| 133 | + |
| 134 | + # parse arguments |
| 135 | + args = parser.parse_args() |
| 136 | + |
| 137 | + # set args.all automatically |
| 138 | + args.all = False if (args.amrex or args.pyamrex) else True |
| 139 | + |
| 140 | + # update |
| 141 | + update(args) |
0 commit comments