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