version.py 4.7 KB

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