Skip to content

Commit

Permalink
Added version check
Browse files Browse the repository at this point in the history
  • Loading branch information
RhetTbull committed Feb 14, 2024
1 parent 14754eb commit b2844ef
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
37 changes: 37 additions & 0 deletions scripts/get_latest_pypi_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Given a PyPI package name, print the latest version number of the package.
This uses the standard library instead of requests to avoid adding a dependency to the project.
"""

from __future__ import annotations

import json
import ssl
import sys
import urllib.request

VERSION_INFO_URL = "https://pypi.org/pypi/{}/json"


def get_latest_version(package_name: str) -> str | None:
"""Get latest version of package_name from PyPI"""
try:
url = VERSION_INFO_URL.format(package_name)
ssl_context = ssl._create_unverified_context()
response = urllib.request.urlopen(url, context=ssl_context)
data = json.load(response)
return data["info"]["version"]
except Exception as e:
raise ValueError(f"Error retrieving version for {package_name}: {e}")


if __name__ == "__main__":
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} PACKAGE_NAME")
sys.exit(1)
package_name = sys.argv[1]
try:
print(get_latest_version(package_name))
except ValueError as e:
print(e)
sys.exit(1)
12 changes: 12 additions & 0 deletions scripts/pyapp-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ SERVER=$1
PROJECT_NAME=$2
PROJECT_VERSION=$3

# verify PROJECT_VERSION is valid
# PyApp will happily build with an invalid version number
# get directory of this script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
PYPI_VERSION=$(python $DIR/get_latest_pypi_version.py $PROJECT_NAME)
if [ "$PYPI_VERSION" != "$PROJECT_VERSION" ]; then
echo "Invalid version number: $PROJECT_VERSION"
echo "Latest version on PyPI: $PYPI_VERSION"
echo "Did you forget to run 'flit publish'?"
exit 1
fi

# define the remote server and user
# assumes ssh keys are setup for the user
# Set these directly or read from environment variables
Expand Down

0 comments on commit b2844ef

Please sign in to comment.