version.py 4.0 KB

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