version.py 4.0 KB

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