version.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. # pylint: disable=,missing-module-docstring,missing-class-docstring
  4. import re
  5. import os
  6. import shlex
  7. import subprocess
  8. import logging
  9. # fallback values
  10. # if there is searx.version_frozen module, and it is not possible to get the git tag
  11. VERSION_STRING = "1.0.0"
  12. VERSION_TAG = "1.0.0"
  13. GIT_URL = "unknow"
  14. GIT_BRANCH = "unknow"
  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. try:
  37. ref = subprocess_run("git rev-parse --abbrev-ref @{upstream}")
  38. except subprocess.CalledProcessError:
  39. ref = subprocess_run("git rev-parse --abbrev-ref master@{upstream}")
  40. origin, git_branch = ref.split("/", 1)
  41. git_url = subprocess_run(["git", "remote", "get-url", origin])
  42. # get https:// url from git@ url
  43. if git_url.startswith("git@"):
  44. git_url = git_url.replace(":", "/", 2).replace("git@", "https://", 1)
  45. if git_url.endswith(".git"):
  46. git_url = git_url.replace(".git", "", 1)
  47. return git_url, git_branch
  48. def get_git_version():
  49. try:
  50. tag = subprocess_run("git describe HEAD")
  51. # a. HEAD is on tag name, example: tag = "v1.0.1"
  52. # b. HEAD is not a tag name, example "<tag>-<distance>-g<commit>"
  53. tag_version, tag_distance, tag_commit = (tag.split("-") + ["", ""])[:3]
  54. if re.match(r"v[0-9]+\.[0-9]+\.[0-9]+", tag_version):
  55. # tag_version "v1.0.0" becomes "1.0.0" (without the v)
  56. # other patterns are kept untouched
  57. tag_version = tag_version[1:]
  58. # remove "g" prefix from tag_commit
  59. if tag_commit and tag_commit[0] == "g":
  60. tag_commit = tag_commit[1:]
  61. # set git_version to "1.0.0-590-0686e274" or '1.0.0'
  62. git_version = "-".join(filter(bool, [tag_version, tag_distance, tag_commit]))
  63. except subprocess.CalledProcessError:
  64. # fall back to "YYYY.MM.DD.Hash" if there is no tag at all
  65. git_version = subprocess_run(r"git show -s --format='%as-%h'")
  66. # PEP 440: replace - with .
  67. tag_version = git_version = git_version.replace("-", ".")
  68. # add "-dirty" suffix if there are uncommited changes except searx/settings.yml
  69. try:
  70. subprocess_run(
  71. "git diff --quiet -- . ':!searx/settings.yml' ':!utils/brand.env'"
  72. )
  73. except subprocess.CalledProcessError as e:
  74. if e.returncode == 1:
  75. git_version += "-dirty"
  76. else:
  77. logger.warning(
  78. '"%s" returns an unexpected return code %i', e.returncode, e.cmd
  79. )
  80. return git_version, tag_version
  81. try:
  82. from searx.version_frozen import VERSION_STRING, VERSION_TAG, GIT_URL, GIT_BRANCH
  83. except ImportError:
  84. try:
  85. try:
  86. VERSION_STRING, VERSION_TAG = get_git_version()
  87. except subprocess.CalledProcessError as ex:
  88. logger.error("Error while getting the version: %s", ex.stderr)
  89. try:
  90. GIT_URL, GIT_BRANCH = get_git_url_and_branch()
  91. except subprocess.CalledProcessError as ex:
  92. logger.error("Error while getting the git URL & branch: %s", ex.stderr)
  93. except FileNotFoundError as ex:
  94. logger.error("%s is not found, fallback to the default version", ex.filename)
  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. # freeze the version (to create an archive outside a git repository)
  100. python_code = f"""# SPDX-License-Identifier: AGPL-3.0-or-later
  101. # this file is generated automatically by searx/version.py
  102. VERSION_STRING = "{VERSION_STRING}"
  103. VERSION_TAG = "{VERSION_TAG}"
  104. GIT_URL = "{GIT_URL}"
  105. GIT_BRANCH = "{GIT_BRANCH}"
  106. """
  107. with open(
  108. os.path.join(os.path.dirname(__file__), "version_frozen.py"),
  109. "w", encoding="utf8") as f:
  110. f.write(python_code)
  111. print(f"{f.name} created")
  112. else:
  113. # output shell code to set the variables
  114. # usage: eval "$(python -m searx.version)"
  115. shell_code = f"""
  116. VERSION_STRING="{VERSION_STRING}"
  117. VERSION_TAG="{VERSION_TAG}"
  118. GIT_URL="{GIT_URL}"
  119. GIT_BRANCH="{GIT_BRANCH}"
  120. """
  121. print(shell_code)