version.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=,missing-module-docstring,missing-class-docstring
  3. import importlib
  4. import logging
  5. import os
  6. import shlex
  7. import subprocess
  8. # fallback values
  9. # if there is searx.version_frozen module, and it is not possible to get the git tag
  10. VERSION_STRING = "1.0.0"
  11. VERSION_TAG = "1.0.0"
  12. DOCKER_TAG = "1.0.0"
  13. GIT_URL = "unknown"
  14. GIT_BRANCH = "unknown"
  15. logger = logging.getLogger("searx")
  16. SUBPROCESS_RUN_ENV = {
  17. "PATH": os.environ["PATH"],
  18. "LC_ALL": "C",
  19. "LANGUAGE": "",
  20. }
  21. def subprocess_run(args, **kwargs):
  22. """Call :py:func:`subprocess.run` and return (striped) stdout. If returncode is
  23. non-zero, raise a :py:func:`subprocess.CalledProcessError`.
  24. """
  25. if not isinstance(args, (list, tuple)):
  26. args = shlex.split(args)
  27. kwargs["env"] = kwargs.get("env", SUBPROCESS_RUN_ENV)
  28. kwargs["encoding"] = kwargs.get("encoding", "utf-8")
  29. kwargs["stdout"] = subprocess.PIPE
  30. kwargs["stderr"] = subprocess.PIPE
  31. # raise CalledProcessError if returncode is non-zero
  32. kwargs["check"] = True
  33. proc = subprocess.run(args, **kwargs) # pylint: disable=subprocess-run-check
  34. return proc.stdout.strip()
  35. def get_git_url_and_branch():
  36. # handle GHA directly
  37. if "GITHUB_REPOSITORY" in os.environ and "GITHUB_REF_NAME" in os.environ:
  38. git_url = f"https://github.com/{os.environ['GITHUB_REPOSITORY']}"
  39. git_branch = os.environ["GITHUB_REF_NAME"]
  40. return git_url, git_branch
  41. try:
  42. ref = subprocess_run("git rev-parse --abbrev-ref @{upstream}")
  43. except subprocess.CalledProcessError:
  44. ref = subprocess_run("git rev-parse --abbrev-ref master@{upstream}")
  45. origin, git_branch = ref.split("/", 1)
  46. git_url = subprocess_run(["git", "remote", "get-url", origin])
  47. # get https:// url from git@ url
  48. if git_url.startswith("git@"):
  49. git_url = git_url.replace(":", "/", 2).replace("git@", "https://", 1)
  50. if git_url.endswith(".git"):
  51. git_url = git_url.replace(".git", "", 1)
  52. return git_url, git_branch
  53. def get_git_version():
  54. git_commit_date_hash = subprocess_run(r"git show -s --date='format:%Y.%m.%d' --format='%cd+%h'")
  55. # Remove leading zero from minor and patch level / replacement of PR-2122
  56. # which depended on the git version: '2023.05.06+..' --> '2023.5.6+..'
  57. git_commit_date_hash = git_commit_date_hash.replace('.0', '.')
  58. tag_version = git_version = git_commit_date_hash
  59. docker_tag = git_commit_date_hash.replace("+", "-")
  60. # add "+dirty" suffix if there are uncommitted changes except searx/settings.yml
  61. try:
  62. subprocess_run("git diff --quiet -- . ':!searx/settings.yml' ':!utils/brand.env'")
  63. except subprocess.CalledProcessError as e:
  64. if e.returncode == 1:
  65. git_version += "+dirty"
  66. else:
  67. logger.warning('"%s" returns an unexpected return code %i', e.returncode, e.cmd)
  68. return git_version, tag_version, docker_tag
  69. def get_information():
  70. version_string = VERSION_STRING
  71. version_tag = VERSION_TAG
  72. docker_tag = DOCKER_TAG
  73. git_url = GIT_URL
  74. git_branch = GIT_BRANCH
  75. try:
  76. version_string, version_tag, docker_tag = get_git_version()
  77. except subprocess.CalledProcessError as ex:
  78. logger.error("Error while getting the version: %s", ex.stderr)
  79. try:
  80. git_url, git_branch = get_git_url_and_branch()
  81. except subprocess.CalledProcessError as ex:
  82. logger.error("Error while getting the git URL & branch: %s", ex.stderr)
  83. return version_string, version_tag, docker_tag, git_url, git_branch
  84. try:
  85. vf = importlib.import_module('searx.version_frozen')
  86. VERSION_STRING, VERSION_TAG, DOCKER_TAG, GIT_URL, GIT_BRANCH = (
  87. vf.VERSION_STRING,
  88. vf.VERSION_TAG,
  89. vf.DOCKER_TAG,
  90. vf.GIT_URL,
  91. vf.GIT_BRANCH,
  92. )
  93. except ImportError:
  94. VERSION_STRING, VERSION_TAG, DOCKER_TAG, GIT_URL, GIT_BRANCH = get_information()
  95. logger.info("version: %s", VERSION_STRING)
  96. if __name__ == "__main__":
  97. import sys
  98. if len(sys.argv) >= 2 and sys.argv[1] == "freeze":
  99. VERSION_STRING, VERSION_TAG, DOCKER_TAG, GIT_URL, GIT_BRANCH = get_information()
  100. # freeze the version (to create an archive outside a git repository)
  101. python_code = f"""# SPDX-License-Identifier: AGPL-3.0-or-later
  102. # pylint: disable=missing-module-docstring
  103. # this file is generated automatically by searx/version.py
  104. VERSION_STRING = "{VERSION_STRING}"
  105. VERSION_TAG = "{VERSION_TAG}"
  106. DOCKER_TAG = "{DOCKER_TAG}"
  107. GIT_URL = "{GIT_URL}"
  108. GIT_BRANCH = "{GIT_BRANCH}"
  109. """
  110. with open(os.path.join(os.path.dirname(__file__), "version_frozen.py"), "w", encoding="utf8") as f:
  111. f.write(python_code)
  112. print(f"{f.name} created")
  113. else:
  114. # output shell code to set the variables
  115. # usage: eval "$(python -m searx.version)"
  116. shell_code = f"""
  117. VERSION_STRING="{VERSION_STRING}"
  118. VERSION_TAG="{VERSION_TAG}"
  119. DOCKER_TAG="{DOCKER_TAG}"
  120. GIT_URL="{GIT_URL}"
  121. GIT_BRANCH="{GIT_BRANCH}"
  122. """
  123. print(shell_code)