|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import subprocess |
| 3 | +import pathlib |
| 4 | +import re |
| 5 | +import sys |
| 6 | +import urllib.request |
| 7 | +import json |
| 8 | + |
| 9 | +INIT_FILE = pathlib.Path("socketsecurity/__init__.py") |
| 10 | +PYPROJECT_FILE = pathlib.Path("pyproject.toml") |
| 11 | + |
| 12 | +VERSION_PATTERN = re.compile(r"__version__\s*=\s*['\"]([^'\"]+)['\"]") |
| 13 | +PYPROJECT_PATTERN = re.compile(r'^version\s*=\s*".*"$', re.MULTILINE) |
| 14 | +PYPI_API = "https://test.pypi.org/pypi/socketsecurity/json" |
| 15 | + |
| 16 | +def read_version_from_init(path: pathlib.Path) -> str: |
| 17 | + content = path.read_text() |
| 18 | + match = VERSION_PATTERN.search(content) |
| 19 | + if not match: |
| 20 | + print(f"❌ Could not find __version__ in {path}") |
| 21 | + sys.exit(1) |
| 22 | + return match.group(1) |
| 23 | + |
| 24 | +def read_version_from_git(path: str) -> str: |
| 25 | + try: |
| 26 | + output = subprocess.check_output(["git", "show", f"HEAD:{path}"], text=True) |
| 27 | + match = VERSION_PATTERN.search(output) |
| 28 | + if not match: |
| 29 | + return None |
| 30 | + return match.group(1) |
| 31 | + except subprocess.CalledProcessError: |
| 32 | + return None |
| 33 | + |
| 34 | +def bump_patch_version(version: str) -> str: |
| 35 | + if ".dev" in version: |
| 36 | + version = version.split(".dev")[0] |
| 37 | + parts = version.split(".") |
| 38 | + parts[-1] = str(int(parts[-1]) + 1) |
| 39 | + return ".".join(parts) |
| 40 | + |
| 41 | +def fetch_existing_versions() -> set: |
| 42 | + try: |
| 43 | + with urllib.request.urlopen(PYPI_API) as response: |
| 44 | + data = json.load(response) |
| 45 | + return set(data.get("releases", {}).keys()) |
| 46 | + except Exception as e: |
| 47 | + print(f"⚠️ Warning: Failed to fetch existing versions from Test PyPI: {e}") |
| 48 | + return set() |
| 49 | + |
| 50 | +def find_next_available_dev_version(base_version: str) -> str: |
| 51 | + existing_versions = fetch_existing_versions() |
| 52 | + for i in range(1, 100): |
| 53 | + candidate = f"{base_version}.dev{i}" |
| 54 | + if candidate not in existing_versions: |
| 55 | + return candidate |
| 56 | + print("❌ Could not find available .devN slot after 100 attempts.") |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | +def inject_version(version: str): |
| 60 | + print(f"🔁 Updating version to: {version}") |
| 61 | + |
| 62 | + # Update __init__.py |
| 63 | + init_content = INIT_FILE.read_text() |
| 64 | + new_init_content = VERSION_PATTERN.sub(f"__version__ = '{version}'", init_content) |
| 65 | + INIT_FILE.write_text(new_init_content) |
| 66 | + |
| 67 | + # Update pyproject.toml |
| 68 | + pyproject = PYPROJECT_FILE.read_text() |
| 69 | + if PYPROJECT_PATTERN.search(pyproject): |
| 70 | + new_pyproject = PYPROJECT_PATTERN.sub(f'version = "{version}"', pyproject) |
| 71 | + else: |
| 72 | + new_pyproject = re.sub(r"(\[project\])", rf"\1\nversion = \"{version}\"", pyproject) |
| 73 | + PYPROJECT_FILE.write_text(new_pyproject) |
| 74 | + |
| 75 | +def main(): |
| 76 | + dev_mode = "--dev" in sys.argv |
| 77 | + current_version = read_version_from_init(INIT_FILE) |
| 78 | + previous_version = read_version_from_git("socketsecurity/__init__.py") |
| 79 | + |
| 80 | + print(f"Current: {current_version}, Previous: {previous_version}") |
| 81 | + |
| 82 | + if current_version == previous_version: |
| 83 | + if dev_mode: |
| 84 | + base_version = current_version.split(".dev")[0] if ".dev" in current_version else current_version |
| 85 | + new_version = find_next_available_dev_version(base_version) |
| 86 | + inject_version(new_version) |
| 87 | + print("⚠️ Version was unchanged — auto-bumped. Please git add + commit again.") |
| 88 | + sys.exit(0) |
| 89 | + else: |
| 90 | + new_version = bump_patch_version(current_version) |
| 91 | + inject_version(new_version) |
| 92 | + print("⚠️ Version was unchanged — auto-bumped. Please git add + commit again.") |
| 93 | + sys.exit(1) |
| 94 | + else: |
| 95 | + print("✅ Version already bumped — proceeding.") |
| 96 | + sys.exit(0) |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments