|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Check the latest published version before publishing.""" |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import tomllib |
| 9 | +from pathlib import Path |
| 10 | +from urllib.request import urlopen |
| 11 | + |
| 12 | + |
| 13 | +ROOT_DIR = Path(__file__).resolve().parent |
| 14 | +VENV_DIR = ROOT_DIR / ".venv" |
| 15 | + |
| 16 | + |
| 17 | +def get_project_metadata(key: str) -> str: |
| 18 | + """Read a value from the Poetry project section in pyproject.toml.""" |
| 19 | + try: |
| 20 | + with (ROOT_DIR / "pyproject.toml").open("rb") as pyproject_file: |
| 21 | + metadata = tomllib.load(pyproject_file) |
| 22 | + value = metadata["tool"]["poetry"][key] |
| 23 | + except (KeyError, TypeError) as error: |
| 24 | + raise ValueError(f"Could not find the project {key} in [tool.poetry]") from error |
| 25 | + return value |
| 26 | + |
| 27 | + |
| 28 | +def get_project_name() -> str: |
| 29 | + """Read the Poetry project name from pyproject.toml.""" |
| 30 | + return get_project_metadata("name") |
| 31 | + |
| 32 | + |
| 33 | +def get_project_version() -> str: |
| 34 | + """Read the Poetry project version from pyproject.toml.""" |
| 35 | + return get_project_metadata("version") |
| 36 | + |
| 37 | + |
| 38 | +def get_latest_published_version(project_name: str) -> str: |
| 39 | + """Query PyPI for the latest published project version.""" |
| 40 | + url = f"https://pypi.org/pypi/{project_name}/json" |
| 41 | + with urlopen(url, timeout=10) as response: |
| 42 | + payload = json.load(response) |
| 43 | + |
| 44 | + try: |
| 45 | + version = payload["info"]["version"] |
| 46 | + except (KeyError, TypeError) as error: |
| 47 | + raise ValueError("PyPI returned a response without a project version") from error |
| 48 | + |
| 49 | + return version |
| 50 | + |
| 51 | + |
| 52 | +def prepare_virtual_environment() -> None: |
| 53 | + """Create .venv if needed, upgrade publishing tools, and build the package.""" |
| 54 | + venv_python = VENV_DIR / ("Scripts/python.exe" if os.name == "nt" else "bin/python") |
| 55 | + if not venv_python.is_file(): |
| 56 | + print("Creating .venv...") |
| 57 | + subprocess.run([sys.executable, "-m", "venv", str(VENV_DIR)], check=True) |
| 58 | + else: |
| 59 | + print("Using existing .venv...") |
| 60 | + |
| 61 | + for package in ("pip", "build", "twine"): |
| 62 | + print(f"Upgrading {package} in .venv...") |
| 63 | + subprocess.run( |
| 64 | + [str(venv_python), "-m", "pip", "install", "--upgrade", package], |
| 65 | + check=True, |
| 66 | + ) |
| 67 | + |
| 68 | + print("Building package in .venv...") |
| 69 | + subprocess.run([str(venv_python), "-m", "build"], cwd=ROOT_DIR, check=True) |
| 70 | + |
| 71 | + |
| 72 | +def print_publish_instructions() -> None: |
| 73 | + """Show how to activate .venv if needed and upload the built distributions.""" |
| 74 | + if Path(sys.prefix).resolve() != VENV_DIR.resolve(): |
| 75 | + print() |
| 76 | + print("Activate the project .venv before publishing:") |
| 77 | + if os.name == "nt": |
| 78 | + print(r" PowerShell: .\.venv\Scripts\Activate.ps1") |
| 79 | + print(r" Command Prompt: .venv\Scripts\activate.bat") |
| 80 | + else: |
| 81 | + print(" source .venv/bin/activate") |
| 82 | + |
| 83 | + print("Publish with:") |
| 84 | + print(" python -m twine upload dist/*") |
| 85 | + |
| 86 | + |
| 87 | +def main() -> int: |
| 88 | + """Check the published version and prepare the publishing environment.""" |
| 89 | + try: |
| 90 | + project_name = get_project_name() |
| 91 | + project_version = get_project_version() |
| 92 | + version = get_latest_published_version(project_name) |
| 93 | + except (OSError, ValueError) as error: |
| 94 | + print(f"Unable to check the latest published version: {error}", file=sys.stderr) |
| 95 | + return 1 |
| 96 | + |
| 97 | + if project_version == version: |
| 98 | + print(f"The current version {project_version} has already been published.", file=sys.stderr) |
| 99 | + return 1 |
| 100 | + |
| 101 | + print(f"Latest published version of {project_name}: {version}") |
| 102 | + print(f"Preparing to publish version {project_version}...") |
| 103 | + try: |
| 104 | + prepare_virtual_environment() |
| 105 | + except (OSError, subprocess.CalledProcessError) as error: |
| 106 | + print(f"Unable to prepare the publishing environment: {error}", file=sys.stderr) |
| 107 | + return 1 |
| 108 | + |
| 109 | + print_publish_instructions() |
| 110 | + return 0 |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + raise SystemExit(main()) |
0 commit comments