|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# SPDX-FileCopyrightText: 2026 Melissa LeBlanc-Williams for Adafruit Industries |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: MIT |
| 5 | + |
| 6 | +"""Update the minimum Adafruit-PlatformDetect dependency in setup.py.""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import json |
| 12 | +import re |
| 13 | +import sys |
| 14 | +import urllib.request |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | + |
| 18 | +PYPI_URL = "https://pypi.org/pypi/Adafruit-PlatformDetect/json" |
| 19 | +SETUP_PY = Path("setup.py") |
| 20 | +REQUIREMENT_RE = re.compile(r'("Adafruit-PlatformDetect>=)([^"]+)(")') |
| 21 | + |
| 22 | + |
| 23 | +def release_tuple(version: str) -> tuple[int, ...]: |
| 24 | + """Return the numeric release segment for simple PyPI version comparisons.""" |
| 25 | + match = re.match(r"^\d+(?:\.\d+)*", version) |
| 26 | + if not match: |
| 27 | + raise ValueError(f"Unsupported version format: {version}") |
| 28 | + return tuple(int(part) for part in match.group(0).split(".")) |
| 29 | + |
| 30 | + |
| 31 | +def latest_pypi_version() -> str: |
| 32 | + with urllib.request.urlopen(PYPI_URL, timeout=30) as response: |
| 33 | + payload = json.load(response) |
| 34 | + return payload["info"]["version"] |
| 35 | + |
| 36 | + |
| 37 | +def update_requirement(version: str) -> bool: |
| 38 | + setup_text = SETUP_PY.read_text(encoding="utf-8") |
| 39 | + match = REQUIREMENT_RE.search(setup_text) |
| 40 | + if not match: |
| 41 | + raise RuntimeError( |
| 42 | + "Could not find Adafruit-PlatformDetect requirement in setup.py" |
| 43 | + ) |
| 44 | + |
| 45 | + current_version = match.group(2) |
| 46 | + if release_tuple(version) <= release_tuple(current_version): |
| 47 | + print( |
| 48 | + "Adafruit-PlatformDetect minimum is already current " |
| 49 | + f"({current_version}; latest is {version})" |
| 50 | + ) |
| 51 | + return False |
| 52 | + |
| 53 | + updated_text = REQUIREMENT_RE.sub(rf"\g<1>{version}\3", setup_text, count=1) |
| 54 | + SETUP_PY.write_text(updated_text, encoding="utf-8") |
| 55 | + print( |
| 56 | + f"Updated Adafruit-PlatformDetect minimum from {current_version} to {version}" |
| 57 | + ) |
| 58 | + return True |
| 59 | + |
| 60 | + |
| 61 | +def main() -> int: |
| 62 | + parser = argparse.ArgumentParser() |
| 63 | + parser.add_argument( |
| 64 | + "version", |
| 65 | + nargs="?", |
| 66 | + help="Version to set. Defaults to the latest Adafruit-PlatformDetect version on PyPI.", |
| 67 | + ) |
| 68 | + args = parser.parse_args() |
| 69 | + |
| 70 | + version = args.version or latest_pypi_version() |
| 71 | + update_requirement(version) |
| 72 | + return 0 |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + sys.exit(main()) |
0 commit comments