Skip to content

Commit

Permalink
Moved version utility functions into their own python module
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaSBrown committed Nov 14, 2024
1 parent 05b17b5 commit db4d38a
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 41 deletions.
44 changes: 3 additions & 41 deletions python/datafed_pkg/datafed/MessageLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,14 @@
# secure ZeroMQ link.


import re
import requests
import zmq
from . import Version_pb2
from . import SDMS_Anon_pb2 as anon
from . import SDMS_Auth_pb2 as auth
from . import Connection
from . import VERSION


# Function to check if a string contains any letters
def contains_letters(s):
return bool(re.search("[a-zA-Z]", s))


def remove_after_prefix_with_numbers(s):
# Use regular expression to match the prefix with numbers
match = re.match(r"(\d+.*?)(\D.*)", s)
if match:
return match.group(1) # Return the part before the remaining string
return s # If no match is found, return the original string


# Check with pypi if a newer release is available, only look for stable
# versions
def get_latest_stable_version(package_name):
try:
url = f"https://pypi.org/pypi/{package_name}/json"
response = requests.get(url)
response.raise_for_status()
data = response.json()

# Extract release versions
releases = list(data.get("releases", {}).keys())
# Filter the list to remove entries that contain any letters, we don't
# want to look at entries that could be a pre-release of some sort and
# recommend that the user use for instance a beta version.
releases = [release for release in releases if not contains_letters(release)]
if not releases:
return None

return releases[0]
except Exception as e:
print(f"Unable to connect to pypi: {e}")
return None

from . import VersionUtils

##
# @class API
Expand Down Expand Up @@ -208,15 +170,15 @@ def __init__(

# Make a request to pypi
package_name = "datafed" # Replace with the package name you want to check
latest_version_on_pypi = get_latest_stable_version(package_name)
latest_version_on_pypi = VersionUtils.get_latest_stable_version(package_name)

self.new_client_avail = False
if latest_version_on_pypi:
pypi_major, pypi_minor, pypi_patch = latest_version_on_pypi.split(".")
major, minor, patch_w_prerelease = VERSION.__version__.split(".")

# Remove prerelease part from patch
patch = remove_after_prefix_with_numbers(patch_w_prerelease)
patch = VersionUtils.remove_after_prefix_with_numbers(patch_w_prerelease)

if pypi_major > major:
self.new_client_avail = latest_version_on_pypi
Expand Down
59 changes: 59 additions & 0 deletions python/datafed_pkg/datafed/VersionUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# @package datafed.VersionUtils
# version utilities for ensuring updated version of package are being used
#
# The functions included in here are to do version comparisons and to grab
# the registered versions that are available on pypi.

import re
import requests

# Function to convert version string into a tuple of integers for comparison
def version_key(version):
# Split main version and optional build number (e.g., "1.1.0-4" -> ["1.1.0", "4"])
main_version, *build = version.split('-')
# Convert main version part to tuple of integers for correct comparison
main_version_tuple = tuple(map(int, main_version.split('.')))
# Convert build part to integer if it exists, or set to -1 for non-build versions
build_number = int(build[0]) if build else -1
# Return full tuple for sorting, making sure 0.x.y is distinct from x.y.z
return main_version_tuple + (build_number,)

# Function to check if a string contains any letters
def contains_letters(s):
return bool(re.search("[a-zA-Z]", s))


def remove_after_prefix_with_numbers(s):
# Use regular expression to match the prefix with numbers
match = re.match(r"(\d+.*?)(\D.*)", s)
if match:
return match.group(1) # Return the part before the remaining string
return s # If no match is found, return the original string


# Check with pypi if a newer release is available, only look for stable
# versions
def get_latest_stable_version(package_name):
try:
url = f"https://pypi.org/pypi/{package_name}/json"
response = requests.get(url)
response.raise_for_status()
data = response.json()

# Extract release versions
releases = list(data.get("releases", {}).keys())
# Filter the list to remove entries that contain any letters, we don't
# want to look at entries that could be a pre-release of some sort and
# recommend that the user use for instance a beta version.
releases = [release for release in releases if not contains_letters(release)]
if not releases:
return None

# Sort versions using the custom key function
sorted_releases = sorted(releases, key=version_key)
return sorted_releases[-1]
except Exception as e:
print(f"Unable to connect to pypi: {e}")
return None


0 comments on commit db4d38a

Please sign in to comment.