Skip to content

Commit ab18040

Browse files
authored
Merge pull request #1118 from makermelissa/update-platformdetect-dependency-action
Add PlatformDetect dependency update workflow
2 parents 582a37b + 66c9ed3 commit ab18040

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# SPDX-FileCopyrightText: 2026 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
name: Update PlatformDetect Dependency
5+
6+
on:
7+
workflow_dispatch:
8+
schedule:
9+
- cron: "0 10 * * 1"
10+
11+
permissions:
12+
contents: write
13+
pull-requests: write
14+
15+
jobs:
16+
update-platformdetect:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- name: Checkout Current Repo
20+
uses: actions/checkout@v3
21+
22+
- name: Set up Python
23+
uses: actions/setup-python@v4
24+
with:
25+
python-version: "3.x"
26+
27+
- name: Update PlatformDetect dependency
28+
id: update-platformdetect
29+
run: |
30+
python scripts/update_platformdetect_min.py
31+
if git diff --quiet -- setup.py; then
32+
echo "changed=false" >> "$GITHUB_OUTPUT"
33+
else
34+
echo "changed=true" >> "$GITHUB_OUTPUT"
35+
fi
36+
37+
- name: Create pull request
38+
if: steps.update-platformdetect.outputs.changed == 'true'
39+
uses: peter-evans/create-pull-request@v6
40+
with:
41+
branch: update-platformdetect-dependency
42+
commit-message: Update PlatformDetect dependency
43+
title: Update PlatformDetect dependency
44+
body: Updates Blinka's minimum Adafruit-PlatformDetect dependency to the latest PyPI release.
45+
delete-branch: true
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)