|
| 1 | +""" |
| 2 | +This module retrieves the versions of Micropython from the Micropython repository on GitHub. |
| 3 | +It is used to generate a matrix of versions to create stubs for using Github Actions. |
| 4 | +
|
| 5 | +It provides a function `micropython_versions` that returns a list of versions starting from a specified version. |
| 6 | +The module also includes a main block that generates a matrix of versions based on command-line arguments and environment variables. |
| 7 | +The matrix is printed as JSON and can be optionally written to a file if running in a GitHub Actions workflow. |
| 8 | +""" |
| 9 | +import argparse |
| 10 | +import json |
| 11 | +import os |
| 12 | + |
| 13 | +from github import Auth, Github |
| 14 | +from packaging.version import parse |
| 15 | + |
| 16 | +# Token with no permissions to avoid throttling |
| 17 | +# https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#getting-a-higher-rate-limit |
| 18 | +PAT_NO_ACCESS = ( |
| 19 | + "github_pat" + "_11AAHPVFQ0qAkDnSUaMKSp" + "_ZkDl5NRRwBsUN6EYg9ahp1Dvj4FDDONnXVgimxC2EtpY7Q7BUKBoQ0Jq72X" |
| 20 | +) |
| 21 | +PAT = os.environ.get("GITHUB_TOKEN") or PAT_NO_ACCESS |
| 22 | + |
| 23 | +def micropython_versions(start="v1.10"): |
| 24 | + g = Github(auth=Auth.Token(PAT)) |
| 25 | + try: |
| 26 | + repo = g.get_repo("micropython/micropython") |
| 27 | + # Suppress `v1.22.0-preview` tags |
| 28 | + tags = sorted([tag.name for tag in repo.get_tags() if "-preview" not in tag.name and parse(tag.name) >= parse(start)], reverse=True) |
| 29 | + except Exception as e: |
| 30 | + print(f"Error: {e}") |
| 31 | + tags = ["stable"] |
| 32 | + return tags |
| 33 | + |
| 34 | + |
| 35 | +def main(): |
| 36 | + matrix = {} |
| 37 | + |
| 38 | + parser = argparse.ArgumentParser() |
| 39 | + parser.add_argument("--stable", "--latest","-s", action=argparse.BooleanOptionalAction,default=True, help="Add latest version") |
| 40 | + parser.add_argument("--preview", "-p", action=argparse.BooleanOptionalAction, default=False, help="Add preview version") |
| 41 | + parser.add_argument("--max", "-m", type=int, default=3, help="Maximum number of versions") |
| 42 | + |
| 43 | + args = parser.parse_args() |
| 44 | + |
| 45 | + # only run latests when running in ACT locally for testing |
| 46 | + if os.environ.get("ACT"): |
| 47 | + matrix["version"] = micropython_versions(start="v1.20")[:1] # only latest |
| 48 | + else: |
| 49 | + matrix["version"] = micropython_versions(start="v1.20")[1:args.max] # last three |
| 50 | + |
| 51 | + |
| 52 | + print(args) |
| 53 | + if args.stable: |
| 54 | + matrix["version"].insert(0, "stable") |
| 55 | + if args.preview: |
| 56 | + matrix["version"].insert(0, "preview") |
| 57 | + matrix["version"]=matrix["version"][:args.max] |
| 58 | + # GITHUB_OUTPUT is set by github actions |
| 59 | + if os.getenv('GITHUB_OUTPUT'): |
| 60 | + with open(os.getenv('GITHUB_OUTPUT'), 'a') as file: # type: ignore |
| 61 | + file.write(f"versions={json.dumps(matrix)}") |
| 62 | + else: |
| 63 | + print(json.dumps(matrix, indent=4)) |
| 64 | + |
| 65 | +# sourcery skip: assign-if-exp, merge-dict-assign |
| 66 | +if __name__ == "__main__": |
| 67 | + main() |
| 68 | + |
0 commit comments