2023-12-11 02:56:06 +03:00
|
|
|
from os import environ
|
|
|
|
from requests import get
|
2023-12-11 04:50:33 +03:00
|
|
|
from importlib.metadata import version as get_package_version, PackageNotFoundError
|
2023-12-11 02:56:06 +03:00
|
|
|
from subprocess import check_output, CalledProcessError, PIPE
|
|
|
|
from .errors import VersionNotFoundError
|
|
|
|
|
2023-12-07 09:18:05 +03:00
|
|
|
logging = False
|
2023-12-11 02:56:06 +03:00
|
|
|
version_check = True
|
|
|
|
|
|
|
|
def get_version() -> str:
|
|
|
|
# Read from package manager
|
|
|
|
try:
|
2023-12-11 04:50:33 +03:00
|
|
|
return get_package_version("g4f")
|
2023-12-11 02:56:06 +03:00
|
|
|
except PackageNotFoundError:
|
|
|
|
pass
|
|
|
|
# Read from docker environment
|
|
|
|
current_version = environ.get("G4F_VERSION")
|
|
|
|
if current_version:
|
|
|
|
return current_version
|
|
|
|
# Read from git repository
|
|
|
|
try:
|
|
|
|
command = ["git", "describe", "--tags", "--abbrev=0"]
|
|
|
|
return check_output(command, text=True, stderr=PIPE).strip()
|
|
|
|
except CalledProcessError:
|
|
|
|
pass
|
|
|
|
raise VersionNotFoundError("Version not found")
|
|
|
|
|
2023-12-19 08:08:12 +03:00
|
|
|
def get_latest_version() -> str:
|
2023-12-11 02:56:06 +03:00
|
|
|
response = get("https://pypi.org/pypi/g4f/json").json()
|
|
|
|
return response["info"]["version"]
|
|
|
|
|
|
|
|
def check_pypi_version() -> None:
|
|
|
|
try:
|
|
|
|
version = get_version()
|
2023-12-19 08:08:12 +03:00
|
|
|
latest_version = get_latest_version()
|
2023-12-11 04:50:33 +03:00
|
|
|
if version != latest_version:
|
|
|
|
print(f'New pypi version: {latest_version} (current: {version}) | pip install -U g4f')
|
2023-12-11 02:56:06 +03:00
|
|
|
except Exception as e:
|
2023-12-11 04:50:33 +03:00
|
|
|
print(f'Failed to check g4f pypi version: {e}')
|