-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion_check.py
44 lines (33 loc) · 1.58 KB
/
version_check.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import httpx
from packaging import version
from packaging.version import Version
# Import the current version of your package
from git_mirror.__about__ import __version__
from git_mirror.ui import console_with_theme
console = console_with_theme()
def call_pypi_with_version_check(package_name: str, current_version: str) -> tuple[bool, Version]:
"""
Synchronously checks if the latest version of the package on PyPI is greater than the current version.
Args:
package_name (str): The name of the package to check.
current_version (str): The current version of the package.
Returns:
bool: True if the latest version on PyPI is greater than the current version, False otherwise.
"""
pypi_url = f"https://pypi.org/pypi/{package_name}/json"
response = httpx.get(pypi_url, timeout=10)
response.raise_for_status() # Raises an exception for 4XX/5XX responses
data = response.json()
latest_version = data["info"]["version"]
return version.parse(latest_version) > version.parse(current_version), version.parse(latest_version)
# Example usage
def display_version_check_message() -> None:
try:
package_name = "git_mirror"
available, new_version = call_pypi_with_version_check(package_name, __version__)
if available:
console.print(f"A newer version of {package_name} is available on PyPI. Upgrade to {new_version}.")
except httpx.HTTPError as e:
console.print(f"An error occurred while checking the latest version: {e}", style="danger")
if __name__ == "__main__":
display_version_check_message()