version.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from __future__ import annotations
  2. from os import environ
  3. import requests
  4. from functools import cached_property
  5. from importlib.metadata import version as get_package_version, PackageNotFoundError
  6. from subprocess import check_output, CalledProcessError, PIPE
  7. from .errors import VersionNotFoundError
  8. PACKAGE_NAME = "g4f"
  9. GITHUB_REPOSITORY = "xtekky/gpt4free"
  10. def get_pypi_version(package_name: str) -> str:
  11. """
  12. Retrieves the latest version of a package from PyPI.
  13. Args:
  14. package_name (str): The name of the package for which to retrieve the version.
  15. Returns:
  16. str: The latest version of the specified package from PyPI.
  17. Raises:
  18. VersionNotFoundError: If there is an error in fetching the version from PyPI.
  19. """
  20. try:
  21. response = requests.get(f"https://pypi.org/pypi/{package_name}/json").json()
  22. return response["info"]["version"]
  23. except requests.RequestException as e:
  24. raise VersionNotFoundError(f"Failed to get PyPI version: {e}")
  25. def get_github_version(repo: str) -> str:
  26. """
  27. Retrieves the latest release version from a GitHub repository.
  28. Args:
  29. repo (str): The name of the GitHub repository.
  30. Returns:
  31. str: The latest release version from the specified GitHub repository.
  32. Raises:
  33. VersionNotFoundError: If there is an error in fetching the version from GitHub.
  34. """
  35. try:
  36. response = requests.get(f"https://api.github.com/repos/{repo}/releases/latest").json()
  37. return response["tag_name"]
  38. except requests.RequestException as e:
  39. raise VersionNotFoundError(f"Failed to get GitHub release version: {e}")
  40. class VersionUtils:
  41. """
  42. Utility class for managing and comparing package versions of 'g4f'.
  43. """
  44. @cached_property
  45. def current_version(self) -> str:
  46. """
  47. Retrieves the current version of the 'g4f' package.
  48. Returns:
  49. str: The current version of 'g4f'.
  50. Raises:
  51. VersionNotFoundError: If the version cannot be determined from the package manager,
  52. Docker environment, or git repository.
  53. """
  54. # Read from package manager
  55. try:
  56. return get_package_version(PACKAGE_NAME)
  57. except PackageNotFoundError:
  58. pass
  59. # Read from docker environment
  60. version = environ.get("G4F_VERSION")
  61. if version:
  62. return version
  63. # Read from git repository
  64. try:
  65. command = ["git", "describe", "--tags", "--abbrev=0"]
  66. return check_output(command, text=True, stderr=PIPE).strip()
  67. except CalledProcessError:
  68. pass
  69. raise VersionNotFoundError("Version not found")
  70. @cached_property
  71. def latest_version(self) -> str:
  72. """
  73. Retrieves the latest version of the 'g4f' package.
  74. Returns:
  75. str: The latest version of 'g4f'.
  76. """
  77. # Is installed via package manager?
  78. try:
  79. get_package_version(PACKAGE_NAME)
  80. except PackageNotFoundError:
  81. return get_github_version(GITHUB_REPOSITORY)
  82. return get_pypi_version(PACKAGE_NAME)
  83. def check_version(self) -> None:
  84. """
  85. Checks if the current version of 'g4f' is up to date with the latest version.
  86. Note:
  87. If a newer version is available, it prints a message with the new version and update instructions.
  88. """
  89. try:
  90. if self.current_version != self.latest_version:
  91. print(f'New g4f version: {self.latest_version} (current: {self.current_version}) | pip install -U g4f')
  92. except Exception as e:
  93. print(f'Failed to check g4f version: {e}')
  94. utils = VersionUtils()