debug.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from os import environ
  2. from requests import get
  3. from importlib.metadata import version as get_package_version, PackageNotFoundError
  4. from subprocess import check_output, CalledProcessError, PIPE
  5. from .errors import VersionNotFoundError
  6. logging = False
  7. version_check = True
  8. def get_version() -> str:
  9. # Read from package manager
  10. try:
  11. return get_package_version("g4f")
  12. except PackageNotFoundError:
  13. pass
  14. # Read from docker environment
  15. current_version = environ.get("G4F_VERSION")
  16. if current_version:
  17. return current_version
  18. # Read from git repository
  19. try:
  20. command = ["git", "describe", "--tags", "--abbrev=0"]
  21. return check_output(command, text=True, stderr=PIPE).strip()
  22. except CalledProcessError:
  23. pass
  24. raise VersionNotFoundError("Version not found")
  25. def get_lastet_version() -> str:
  26. response = get("https://pypi.org/pypi/g4f/json").json()
  27. return response["info"]["version"]
  28. def check_pypi_version() -> None:
  29. try:
  30. version = get_version()
  31. latest_version = get_lastet_version()
  32. if version != latest_version:
  33. print(f'New pypi version: {latest_version} (current: {version}) | pip install -U g4f')
  34. except Exception as e:
  35. print(f'Failed to check g4f pypi version: {e}')