Skip to content

Commit

Permalink
Rewriting version structure and adding version to footer
Browse files Browse the repository at this point in the history
  • Loading branch information
mehr32 committed May 19, 2024
1 parent 050cc70 commit 877ce21
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 82 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pytomlpp==1.0.13; python_version < '3.11'
translate==3.6.1
requests==2.31.0
beautifulsoup4==4.12.2
GitPython
2 changes: 1 addition & 1 deletion searx/templates/simple/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
</main>
<footer>
<p>
{{ _('a privacy-respecting, open metasearch engine') }}
{{ _('a privacy-respecting, open metasearch engine') }} - {{ searx_version }}
</p><p>
<a href="{{ get_setting('brand.git_url')}}">{{ _('Source code') }}</a>
| <a href="{{ get_setting('brand.issue_url') }}">{{ _('Issue tracker') }}</a>
Expand Down
127 changes: 46 additions & 81 deletions searx/version.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,58 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=,missing-module-docstring,missing-class-docstring
# pylint: disable=missing-module-docstring,missing-class-docstring

import os
import shlex
import subprocess
import logging
import importlib
from git import Repo, GitCommandError, InvalidGitRepositoryError
from packaging.version import Version, InvalidVersion

# fallback values
# if there is searx.version_frozen module, and it is not possible to get the git tag
# Fallback values
VERSION_STRING = "1.0.0"
VERSION_TAG = "1.0.0"
GIT_URL = "unknow"
GIT_BRANCH = "unknow"
GIT_URL = "unknown"
GIT_BRANCH = "unknown"

logger = logging.getLogger("searx")
logger = logging.getLogger("MOA")

SUBPROCESS_RUN_ENV = {
"PATH": os.environ["PATH"],
"LC_ALL": "C",
"LANGUAGE": "",
}


def subprocess_run(args, **kwargs):
"""Call :py:func:`subprocess.run` and return (striped) stdout. If returncode is
non-zero, raise a :py:func:`subprocess.CalledProcessError`.
"""
if not isinstance(args, (list, tuple)):
args = shlex.split(args)

kwargs["env"] = kwargs.get("env", SUBPROCESS_RUN_ENV)
kwargs["encoding"] = kwargs.get("encoding", "utf-8")
kwargs["stdout"] = subprocess.PIPE
kwargs["stderr"] = subprocess.PIPE
# raise CalledProcessError if returncode is non-zero
kwargs["check"] = True
proc = subprocess.run(args, **kwargs) # pylint: disable=subprocess-run-check
return proc.stdout.strip()


def get_git_url_and_branch():
def get_latest_tag(repo_path='.'):
try:
ref = subprocess_run("git rev-parse --abbrev-ref @{upstream}")
except subprocess.CalledProcessError:
ref = subprocess_run("git rev-parse --abbrev-ref master@{upstream}")
origin, git_branch = ref.split("/", 1)
git_url = subprocess_run(["git", "remote", "get-url", origin])

# get https:// url from git@ url
if git_url.startswith("git@"):
git_url = git_url.replace(":", "/", 2).replace("git@", "https://", 1)
if git_url.endswith(".git"):
git_url = git_url.replace(".git", "", 1)

return git_url, git_branch


def get_git_version():
git_commit_date_hash = subprocess_run(r"git show -s --date='format:%Y.%m.%d' --format='%cd+%h'")
# Remove leading zero from minor and patch level / replacement of PR-2122
# which depended on the git version: '2023.05.06+..' --> '2023.5.6+..'
git_commit_date_hash = git_commit_date_hash.replace('.0', '.')
tag_version = git_version = git_commit_date_hash

# add "+dirty" suffix if there are uncommitted changes except searx/settings.yml
repo = Repo(repo_path)
latest_tag = repo.git.describe('--tags', '--abbrev=0')
return latest_tag
except (GitCommandError, InvalidGitRepositoryError) as e:
logger.error("Error while getting the latest tag: %s", str(e))
return "1.0.0"

def get_git_url_and_branch(repo_path='.'):
try:
subprocess_run("git diff --quiet -- . ':!searx/settings.yml' ':!utils/brand.env'")
except subprocess.CalledProcessError as e:
if e.returncode == 1:
git_version += "+dirty"
else:
logger.warning('"%s" returns an unexpected return code %i', e.returncode, e.cmd)
docker_tag = git_version.replace("+", "-")
return git_version, tag_version, docker_tag

repo = Repo(repo_path)
git_url = next(repo.remote().urls)
git_branch = repo.active_branch.name

# Convert ssh URL to https URL
if git_url.startswith("git@"):
git_url = git_url.replace(":", "/").replace("git@", "https://")
if git_url.endswith(".git"):
git_url = git_url[:-4]

return git_url, git_branch
except (GitCommandError, InvalidGitRepositoryError) as e:
logger.error("Error while getting the git URL & branch: %s", str(e))
return GIT_URL, GIT_BRANCH

def get_git_version(repo_path='.'):
try:
repo = Repo(repo_path)
git_commit = repo.head.commit
git_commit_date = git_commit.committed_datetime.strftime('%Y.%m.%d')
git_commit_hash = git_commit.hexsha[:7]
latest_tag = get_latest_tag(repo_path)
git_version = f"{git_commit_date}-{git_commit_hash}"
return f"{git_version}-{latest_tag}", latest_tag, git_version
except (GitCommandError, InvalidGitRepositoryError) as e:
logger.error("Error while getting the version: %s", str(e))
return VERSION_STRING, VERSION_TAG, VERSION_STRING

try:
vf = importlib.import_module('searx.version_frozen')
Expand All @@ -86,29 +64,18 @@ def get_git_version():
vf.GIT_BRANCH,
)
except ImportError:
try:
try:
VERSION_STRING, VERSION_TAG, DOCKER_TAG = get_git_version()
except subprocess.CalledProcessError as ex:
logger.error("Error while getting the version: %s", ex.stderr)
try:
GIT_URL, GIT_BRANCH = get_git_url_and_branch()
except subprocess.CalledProcessError as ex:
logger.error("Error while getting the git URL & branch: %s", ex.stderr)
except FileNotFoundError as ex:
logger.error("%s is not found, fallback to the default version", ex.filename)

VERSION_STRING, VERSION_TAG, DOCKER_TAG = get_git_version()
GIT_URL, GIT_BRANCH = get_git_url_and_branch()

logger.info("version: %s", VERSION_STRING)

if __name__ == "__main__":
import sys

if len(sys.argv) >= 2 and sys.argv[1] == "freeze":
# freeze the version (to create an archive outside a git repository)
python_code = f"""# SPDX-License-Identifier: AGPL-3.0-or-later
# pylint: disable=missing-module-docstring
# this file is generated automatically by searx/version.py
# This file is generated automatically by searx/version.py
VERSION_STRING = "{VERSION_STRING}"
VERSION_TAG = "{VERSION_TAG}"
Expand All @@ -120,8 +87,6 @@ def get_git_version():
f.write(python_code)
print(f"{f.name} created")
else:
# output shell code to set the variables
# usage: eval "$(python -m searx.version)"
shell_code = f"""
VERSION_STRING="{VERSION_STRING}"
VERSION_TAG="{VERSION_TAG}"
Expand Down

0 comments on commit 877ce21

Please sign in to comment.