|  | @@ -1,26 +1,142 @@
 | 
	
		
			
				|  |  | -# -*- coding: utf-8 -*-
 | 
	
		
			
				|  |  | -'''
 | 
	
		
			
				|  |  | -searx is free software: you can redistribute it and/or modify
 | 
	
		
			
				|  |  | -it under the terms of the GNU Affero General Public License as published by
 | 
	
		
			
				|  |  | -the Free Software Foundation, either version 3 of the License, or
 | 
	
		
			
				|  |  | -(at your option) any later version.
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -searx is distributed in the hope that it will be useful,
 | 
	
		
			
				|  |  | -but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
	
		
			
				|  |  | -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
	
		
			
				|  |  | -GNU Affero General Public License for more details.
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -You should have received a copy of the GNU Affero General Public License
 | 
	
		
			
				|  |  | -along with searx. If not, see < http://www.gnu.org/licenses/ >.
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -(C) 2013- by Adam Tauber, <asciimoo@gmail.com>
 | 
	
		
			
				|  |  | -'''
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -# version of searx
 | 
	
		
			
				|  |  | -VERSION_MAJOR = 1
 | 
	
		
			
				|  |  | -VERSION_MINOR = 0
 | 
	
		
			
				|  |  | -VERSION_BUILD = 0
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -VERSION_STRING = "{0}.{1}.{2}".format(VERSION_MAJOR,
 | 
	
		
			
				|  |  | -                                      VERSION_MINOR,
 | 
	
		
			
				|  |  | -                                      VERSION_BUILD)
 | 
	
		
			
				|  |  | +# SPDX-License-Identifier: AGPL-3.0-or-later
 | 
	
		
			
				|  |  | +# lint: pylint
 | 
	
		
			
				|  |  | +# pylint: disable=missing-function-docstring,missing-module-docstring,missing-class-docstring
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import re
 | 
	
		
			
				|  |  | +import os
 | 
	
		
			
				|  |  | +import shlex
 | 
	
		
			
				|  |  | +import subprocess
 | 
	
		
			
				|  |  | +import logging
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +# fallback values
 | 
	
		
			
				|  |  | +# if there is searx.version_frozen module, and it is not possible to get the git tag
 | 
	
		
			
				|  |  | +VERSION_STRING = "1.0.0"
 | 
	
		
			
				|  |  | +VERSION_TAG = "1.0.0"
 | 
	
		
			
				|  |  | +GIT_URL = "unknow"
 | 
	
		
			
				|  |  | +GIT_BRANCH = "unknow"
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +logger = logging.getLogger("searx")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +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():
 | 
	
		
			
				|  |  | +    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():
 | 
	
		
			
				|  |  | +    try:
 | 
	
		
			
				|  |  | +        tag = subprocess_run("git describe HEAD")
 | 
	
		
			
				|  |  | +        # a. HEAD is on tag name, example: tag = "v1.0.1"
 | 
	
		
			
				|  |  | +        # b. HEAD is not a tag name, example "<tag>-<distance>-g<commit>"
 | 
	
		
			
				|  |  | +        tag_version, tag_distance, tag_commit = (tag.split("-") + ["", ""])[:3]
 | 
	
		
			
				|  |  | +        if re.match(r"v[0-9]+\.[0-9]+\.[0-9]+", tag_version):
 | 
	
		
			
				|  |  | +            # tag_version "v1.0.0" becomes "1.0.0" (without the v)
 | 
	
		
			
				|  |  | +            # other patterns are kept untouched
 | 
	
		
			
				|  |  | +            tag_version = tag_version[1:]
 | 
	
		
			
				|  |  | +        # remove "g" prefix from tag_commit
 | 
	
		
			
				|  |  | +        if tag_commit and tag_commit[0] == "g":
 | 
	
		
			
				|  |  | +            tag_commit = tag_commit[1:]
 | 
	
		
			
				|  |  | +        # set git_version to "1.0.0-590-0686e274" or '1.0.0'
 | 
	
		
			
				|  |  | +        git_version = "-".join(filter(bool, [tag_version, tag_distance, tag_commit]))
 | 
	
		
			
				|  |  | +    except subprocess.CalledProcessError:
 | 
	
		
			
				|  |  | +        # fall back to "YYYY.MM.DD.Hash" if there is no tag at all
 | 
	
		
			
				|  |  | +        git_version = subprocess_run(r"git show -s --format='%as-%h'")
 | 
	
		
			
				|  |  | +        # PEP 440: replace - with .
 | 
	
		
			
				|  |  | +        tag_version = git_version = git_version.replace("-", ".")
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    # add "-dirty" suffix if there are uncommited changes except searx/settings.yml
 | 
	
		
			
				|  |  | +    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
 | 
	
		
			
				|  |  | +            )
 | 
	
		
			
				|  |  | +    return git_version, tag_version
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +try:
 | 
	
		
			
				|  |  | +    from searx.version_frozen import VERSION_STRING, VERSION_TAG, GIT_URL, GIT_BRANCH
 | 
	
		
			
				|  |  | +except ImportError:
 | 
	
		
			
				|  |  | +    try:
 | 
	
		
			
				|  |  | +        try:
 | 
	
		
			
				|  |  | +            VERSION_STRING, VERSION_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)
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +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
 | 
	
		
			
				|  |  | +# this file is generated automatically by searx/version.py
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +VERSION_STRING = "{VERSION_STRING}"
 | 
	
		
			
				|  |  | +VERSION_TAG = "{VERSION_TAG}"
 | 
	
		
			
				|  |  | +GIT_URL = "{GIT_URL}"
 | 
	
		
			
				|  |  | +GIT_BRANCH = "{GIT_BRANCH}"
 | 
	
		
			
				|  |  | +"""
 | 
	
		
			
				|  |  | +        with open(
 | 
	
		
			
				|  |  | +            os.path.join(os.path.dirname(__file__), "version_frozen.py"), "w"
 | 
	
		
			
				|  |  | +        ) as f:
 | 
	
		
			
				|  |  | +            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}"
 | 
	
		
			
				|  |  | +GIT_URL="{GIT_URL}"
 | 
	
		
			
				|  |  | +GIT_BRANCH="{GIT_BRANCH}"
 | 
	
		
			
				|  |  | +"""
 | 
	
		
			
				|  |  | +        print(shell_code)
 |