2024-01-26 06:54:13 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-01-01 16:48:57 +00:00
|
|
|
from os import environ
|
|
|
|
import requests
|
|
|
|
from functools import cached_property
|
|
|
|
from importlib.metadata import version as get_package_version, PackageNotFoundError
|
|
|
|
from subprocess import check_output, CalledProcessError, PIPE
|
|
|
|
from .errors import VersionNotFoundError
|
|
|
|
|
2024-02-21 23:16:58 +00:00
|
|
|
PACKAGE_NAME = "g4f"
|
|
|
|
GITHUB_REPOSITORY = "xtekky/gpt4free"
|
|
|
|
|
2024-01-14 06:45:41 +00:00
|
|
|
def get_pypi_version(package_name: str) -> str:
|
|
|
|
"""
|
2024-01-14 14:04:37 +00:00
|
|
|
Retrieves the latest version of a package from PyPI.
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-14 14:04:37 +00:00
|
|
|
Args:
|
|
|
|
package_name (str): The name of the package for which to retrieve the version.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The latest version of the specified package from PyPI.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
VersionNotFoundError: If there is an error in fetching the version from PyPI.
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
2024-01-13 14:37:36 +00:00
|
|
|
try:
|
2024-01-14 06:45:41 +00:00
|
|
|
response = requests.get(f"https://pypi.org/pypi/{package_name}/json").json()
|
2024-01-13 14:37:36 +00:00
|
|
|
return response["info"]["version"]
|
2024-01-14 06:45:41 +00:00
|
|
|
except requests.RequestException as e:
|
|
|
|
raise VersionNotFoundError(f"Failed to get PyPI version: {e}")
|
|
|
|
|
|
|
|
def get_github_version(repo: str) -> str:
|
|
|
|
"""
|
2024-01-14 14:04:37 +00:00
|
|
|
Retrieves the latest release version from a GitHub repository.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
repo (str): The name of the GitHub repository.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The latest release version from the specified GitHub repository.
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-14 14:04:37 +00:00
|
|
|
Raises:
|
|
|
|
VersionNotFoundError: If there is an error in fetching the version from GitHub.
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
response = requests.get(f"https://api.github.com/repos/{repo}/releases/latest").json()
|
2024-01-13 14:37:36 +00:00
|
|
|
return response["tag_name"]
|
2024-01-14 06:45:41 +00:00
|
|
|
except requests.RequestException as e:
|
|
|
|
raise VersionNotFoundError(f"Failed to get GitHub release version: {e}")
|
|
|
|
|
|
|
|
class VersionUtils:
|
|
|
|
"""
|
2024-01-14 14:04:37 +00:00
|
|
|
Utility class for managing and comparing package versions of 'g4f'.
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
2024-01-01 16:48:57 +00:00
|
|
|
@cached_property
|
|
|
|
def current_version(self) -> str:
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
2024-01-14 14:04:37 +00:00
|
|
|
Retrieves the current version of the 'g4f' package.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The current version of 'g4f'.
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-14 14:04:37 +00:00
|
|
|
Raises:
|
|
|
|
VersionNotFoundError: If the version cannot be determined from the package manager,
|
|
|
|
Docker environment, or git repository.
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
2024-01-01 16:48:57 +00:00
|
|
|
# Read from package manager
|
|
|
|
try:
|
2024-02-21 23:16:58 +00:00
|
|
|
return get_package_version(PACKAGE_NAME)
|
2024-01-01 16:48:57 +00:00
|
|
|
except PackageNotFoundError:
|
|
|
|
pass
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-01 16:48:57 +00:00
|
|
|
# Read from docker environment
|
|
|
|
version = environ.get("G4F_VERSION")
|
|
|
|
if version:
|
|
|
|
return version
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-01 16:48:57 +00:00
|
|
|
# Read from git repository
|
|
|
|
try:
|
|
|
|
command = ["git", "describe", "--tags", "--abbrev=0"]
|
|
|
|
return check_output(command, text=True, stderr=PIPE).strip()
|
|
|
|
except CalledProcessError:
|
|
|
|
pass
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-01 16:48:57 +00:00
|
|
|
raise VersionNotFoundError("Version not found")
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-01 16:48:57 +00:00
|
|
|
@cached_property
|
|
|
|
def latest_version(self) -> str:
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
2024-01-14 14:04:37 +00:00
|
|
|
Retrieves the latest version of the 'g4f' package.
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-14 14:04:37 +00:00
|
|
|
Returns:
|
|
|
|
str: The latest version of 'g4f'.
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
2024-02-21 23:16:58 +00:00
|
|
|
# Is installed via package manager?
|
|
|
|
try:
|
|
|
|
get_package_version(PACKAGE_NAME)
|
|
|
|
except PackageNotFoundError:
|
|
|
|
return get_github_version(GITHUB_REPOSITORY)
|
|
|
|
return get_pypi_version(PACKAGE_NAME)
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-13 14:37:36 +00:00
|
|
|
def check_version(self) -> None:
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
2024-01-14 14:04:37 +00:00
|
|
|
Checks if the current version of 'g4f' is up to date with the latest version.
|
|
|
|
|
|
|
|
Note:
|
|
|
|
If a newer version is available, it prints a message with the new version and update instructions.
|
2024-01-14 06:45:41 +00:00
|
|
|
"""
|
2024-01-01 16:48:57 +00:00
|
|
|
try:
|
|
|
|
if self.current_version != self.latest_version:
|
2024-01-13 14:37:36 +00:00
|
|
|
print(f'New g4f version: {self.latest_version} (current: {self.current_version}) | pip install -U g4f')
|
2024-01-01 16:48:57 +00:00
|
|
|
except Exception as e:
|
2024-01-13 14:37:36 +00:00
|
|
|
print(f'Failed to check g4f version: {e}')
|
2024-01-14 06:45:41 +00:00
|
|
|
|
2024-01-01 16:48:57 +00:00
|
|
|
utils = VersionUtils()
|