|
| 1 | +# /// script |
| 2 | +# requires-python = ">=3.12" |
| 3 | +# dependencies = [ |
| 4 | +# "click==8.2.1", |
| 5 | +# "peppyproject==1.0.2", |
| 6 | +# "requests==2.32.5", |
| 7 | +# "packaging==25.0", |
| 8 | +# ] |
| 9 | +# /// |
| 10 | + |
| 11 | +import json |
| 12 | +import os |
| 13 | +from pathlib import Path |
| 14 | +import warnings |
| 15 | + |
| 16 | +import click |
| 17 | +import requests |
| 18 | +from packaging.specifiers import SpecifierSet |
| 19 | +from packaging.version import Version |
| 20 | +from peppyproject import PyProjectConfiguration |
| 21 | + |
| 22 | + |
| 23 | +@click.command() |
| 24 | +@click.option("--source", default=None) |
| 25 | +@click.option("--flags", default=None) |
| 26 | +@click.option("--no-eoas", is_flag=True, default=False) |
| 27 | +def print_supported_pythons( |
| 28 | + source: Path = None, |
| 29 | + flags: list[str] = None, |
| 30 | + no_eoas: bool = False, |
| 31 | +): |
| 32 | + """enumerate toxenvs Python from a package source""" |
| 33 | + |
| 34 | + toxenvs = supported_python_toxenvs(source, flags, no_eoas) |
| 35 | + print(json.dumps(toxenvs, indent=2)) |
| 36 | + with open(os.environ["GITHUB_OUTPUT"], "a") as f: |
| 37 | + f.write(f"envs={json.dumps(toxenvs)}\n") |
| 38 | + |
| 39 | + |
| 40 | +def supported_python_toxenvs( |
| 41 | + source: Path = None, |
| 42 | + flags: list[str] = None, |
| 43 | + no_eoas: bool = False, |
| 44 | +) -> list[str]: |
| 45 | + pythons = current_python_versions(no_eoas=no_eoas) |
| 46 | + |
| 47 | + if source is None or source == "": |
| 48 | + supported_pythons = pythons |
| 49 | + else: |
| 50 | + configuration = PyProjectConfiguration.from_directory(source) |
| 51 | + try: |
| 52 | + python_requirements = SpecifierSet(configuration["project"]["requires-python"]) |
| 53 | + |
| 54 | + supported_pythons = [python for python in pythons if python in python_requirements] |
| 55 | + except (KeyError, TypeError): |
| 56 | + warnings.warn("could not find `requires-python` in metadata; falling back to current Python versions...") |
| 57 | + supported_pythons = pythons |
| 58 | + |
| 59 | + return [ |
| 60 | + f"py{str(python).replace('.', '')}{'-' + '-'.join(flags) if flags is not None and len(flags) > 0 else ''}" |
| 61 | + for python in supported_pythons |
| 62 | + ] |
| 63 | + |
| 64 | + |
| 65 | +def current_python_versions(no_eoas: bool = False) -> list[Version]: |
| 66 | + url = "https://endoflife.date/api/v1/products/python" |
| 67 | + response = requests.get("https://endoflife.date/api/v1/products/python") |
| 68 | + if response.status_code == 200: |
| 69 | + return [ |
| 70 | + Version(python["name"]) |
| 71 | + for python in response.json()["result"]["releases"] |
| 72 | + if not python["isEoas" if no_eoas else "isEol"] |
| 73 | + ] |
| 74 | + else: |
| 75 | + raise ValueError(f"request returned status code {response.status_code} [{url}]") |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + print_supported_pythons() |
0 commit comments