Repository: searxng/searxng

Issue:
Illegal version specifier according to pip
<!-- PLEASE FILL THESE FIELDS, IT REALLY HELPS THE MAINTAINERS OF SearXNG -->

**Version of SearXNG, commit number if you are using on master branch and stipulate if you forked SearXNG**
<!-- If you are running on master branch using git execute this command
in order to fetch the latest commit ID:
```
git log -1
``` 
If you are using searxng-docker then look at the bottom of the SearXNG page
and check for the version after "Powered by SearXNG"

Please also stipulate if you are using a forked version of SearXNG and
include a link to the fork source code.
-->

Commit 13b0c251c45c3d14700723b25b601be56178e8df

**How did you install SearXNG?**
<!-- Did you install SearXNG using the official wiki or using searxng-docker
or manually by executing the searx/webapp.py file? -->

Official wiki, install script.

**What happened?**
<!-- A clear and concise description of what the bug is. -->

Step `pip install -e .` fails with

```
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Obtaining file:///usr/local/searxng/searxng-src
  Preparing metadata (setup.py) ... error
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [27 lines of output]
      /usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/setuptools/dist.py:543: UserWarning: The version specified ('2023.01.15-13b0c251') is an invalid version, this may not work as expected with newer versions of setuptools, pip, and PyPI. Please see PEP 440 for more details.
        warnings.warn(
      2023-01-16 13:17:41,865 INFO:root: running egg_info
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/usr/local/searxng/searxng-src/setup.py", line 19, in <module>
          setup(
        File "/usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/setuptools/__init__.py", line 87, in setup
          return distutils.core.setup(**attrs)
        File "/usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/setuptools/_distutils/core.py", line 185, in setup
          return run_commands(dist)
        File "/usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/setuptools/_distutils/core.py", line 201, in run_commands
          dist.run_commands()
        File "/usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/setuptools/_distutils/dist.py", line 969, in run_commands
          self.run_command(cmd)
        File "/usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/setuptools/dist.py", line 1208, in run_command
          super().run_command(command)
        File "/usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/setuptools/_distutils/dist.py", line 987, in run_command
          cmd_obj.ensure_finalized()
        File "/usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/setuptools/_distutils/cmd.py", line 111, in ensure_finalized
          self.finalize_options()
        File "/usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/setuptools/command/egg_info.py", line 219, in finalize_options
          parsed_version = parse_version(self.egg_version)
        File "/usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/pkg_resources/_vendor/packaging/version.py", line 266, in __init__
          raise InvalidVersion(f"Invalid version: '{version}'")
      pkg_resources.extern.packaging.version.InvalidVersion: Invalid version: '2023.01.15-13b0c251'
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
```


**How To Reproduce**
<!-- How can we reproduce this issue? (as minimally and as precisely as possible) -->

Follow instructions in https://docs.searxng.org/admin/installation-scripts.html#installation-scripts, in my case on a Raspberry Pi 4B, with following version info:

```
$ uname -a
Linux raspberrypi 5.15.32-v8+ #1538 SMP PREEMPT Thu Mar 31 19:40:39 BST 2022 aarch64 GNU/Linux
```
```
$ python --version
Python 3.9.2
```
```
$ pip --version
pip 22.3.1 from /usr/local/searxng/searx-pyenv/lib/python3.9/site-packages/pip (python 3.9)
```

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->

Expected installation to succeed.

**Screenshots & Logs**
<!-- If applicable, add screenshots, logs to help explain your problem. -->

N/A

**Additional context**
<!-- Add any other context about the problem here. -->

N/A


Discussion:
SearXNG can't installed by pip .. for detailed installation instructions see https://docs.searxng.org/admin/installation.html
Sorry, could you clarify that `sudo -H ./utils/searxng.sh install all` isn't supposed to work? That's the installation method I chose to try out, and the above error message pops up when the script tries to run `pip install -e `. I can give you the full output if you'd like?
+1. I just ran `sudo -H ./utils/searxng.sh instance update` with the same result & similar error message.

<details>
<summary>Full output here</summary>

```
$ sudo -H ./utils/searxng.sh instance update

Update SearXNG instance
=======================

fetch from https://github.com/searxng/searxng and reset to origin/master
cd /usr/local/searxng/searxng-src
git fetch origin "master"
git reset --hard "origin/master"
  |searxng| From https://github.com/searxng/searxng
  |searxng|  * branch              master     -> FETCH_HEAD
  |searxng| HEAD is now at 13b0c251 Merge pull request #2100 from nexryai/master
pip install -U pip
pip install -U setuptools
  |searxng| Requirement already satisfied: pip in /usr/local/searxng/searx-pyenv/lib/python3.7/site-packages (22.3.1)
pip install -U wheel
pip install -U pyyaml
pip install -U -e .
  |searxng| Requirement already satisfied: setuptools in /usr/local/searxng/searx-pyenv/lib/python3.7/site-packages (66.0.0)
  |searxng| Requirement already satisfied: wheel in /usr/local/searxng/searx-pyenv/lib/python3.7/site-packages (0.38.4)
  |searxng| Requirement already satisfied: pyyaml in /usr/local/searxng/searx-pyenv/lib/python3.7/site-packages (6.0)
  |searxng| Obtaining file:///usr/local/searxng/searxng-src
  |searxng|   Preparing metadata (setup.py): started
  |searxng|   Preparing metadata (setup.py): finished with status 'error'
  |searxng|   error: subprocess-exited-with-error
  |searxng|
  |searxng|   × python setup.py egg_info did not run successfully.
  |searxng|   │ exit code: 1
  |searxng|   ╰─> [27 lines of output]
  |searxng|       /usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/setuptools/dist.py:547: UserWarning: The version specified ('2023.01.15-13b0c251') is an invalid version, this may not work as expected with newer versions of setuptools, pip, and PyPI. Please see PEP 440 for more details.
  |searxng|         "details." % version
  |searxng|       2023-01-16 17:17:56,311 INFO:root: running egg_info
  |searxng|       Traceback (most recent call last):
  |searxng|         File "<string>", line 36, in <module>
  |searxng|         File "<pip-setuptools-caller>", line 34, in <module>
  |searxng|         File "/usr/local/searxng/searxng-src/setup.py", line 74, in <module>
  |searxng|           'translations/*/*/*'
  |searxng|         File "/usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/setuptools/__init__.py", line 87, in setup
  |searxng|           return distutils.core.setup(**attrs)
  |searxng|         File "/usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/setuptools/_distutils/core.py", line 185, in setup
  |searxng|           return run_commands(dist)
  |searxng|         File "/usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/setuptools/_distutils/core.py", line 201, in run_commands
  |searxng|           dist.run_commands()
  |searxng|         File "/usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 969, in run_commands
  |searxng|           self.run_command(cmd)
  |searxng|         File "/usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/setuptools/dist.py", line 1208, in run_command
  |searxng|           super().run_command(command)
  |searxng|         File "/usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/setuptools/_distutils/dist.py", line 987, in run_command
  |searxng|           cmd_obj.ensure_finalized()
  |searxng|         File "/usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/setuptools/_distutils/cmd.py", line 111, in ensure_finalized
  |searxng|           self.finalize_options()
  |searxng|         File "/usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/setuptools/command/egg_info.py", line 219, in finalize_options
  |searxng|           parsed_version = parse_version(self.egg_version)
  |searxng|         File "/usr/local/searxng/searx-pyenv/lib/python3.7/site-packages/pkg_resources/_vendor/packaging/version.py", line 266, in __init__
  |searxng|           raise InvalidVersion(f"Invalid version: '{version}'")
  |searxng|       pkg_resources.extern.packaging.version.InvalidVersion: Invalid version: '2023.01.15-13b0c251'
  |searxng|       [end of output]
  |searxng|
  |searxng|   note: This error originates from a subprocess, and is likely not a problem with pip.
  |searxng| error: metadata-generation-failed
  |searxng|
  |searxng| × Encountered error while generating package metadata.
  |searxng| ╰─> See above for output.
  |searxng|
  |searxng| note: This is an issue with the package mentioned above, not pip.
  |searxng| hint: See above for details.
```
</details>
Hm, very strange .. haven't seen this before and it seems to be limited to '2023.01.15-13b0c251c' .. @dalf Can you think of a reason off the top of your head why pip doesn't like this commit number?

EDIT: Sorry that I haven't time right now to dig any deeper .. will take some days before I have time to look closer.
This is described in PEP440: https://peps.python.org/pep-0440/#local-version-identifiers
A label can be specified after a `+` not a `-`.
So `2023-01-16-a432ab65` is invalid, and `2023-01-16+a432ab65` is valid.

I've seen the issue while trying to use pyproject.toml to allow install of searxng with pip (directly from github): https://github.com/searxng/searxng/compare/master...dalf:searxng:pyprojecttoml
Any workarounds? After upgrading my Ubuntu to the latest LTS I can no longer install.
https://github.com/searxng/searxng/blob/6d72ef3cbec5246fd52498932760ea2f975a3112/searx/version.py#L62

Replace `%cd-%h` by `%cd+%h` should be enough.

Resolving patch:
diff --git a/searx/version.py b/searx/version.py
--- a/searx/version.py
+++ b/searx/version.py
@@ -59,7 +59,7 @@ def get_git_url_and_branch():
 
 
 def get_git_version():
-    git_commit_date_hash = subprocess_run(r"git show -s --date='format:%Y.%m.%d' --format='%cd-%h'")
+    git_commit_date_hash = subprocess_run(r"git show -s --date='format:%Y.%m.%d' --format='%cd+%h'")
     tag_version = git_version = git_commit_date_hash
 
     # add "-dirty" suffix if there are uncommited changes except searx/settings.yml
@@ -67,7 +67,7 @@ def get_git_version():
         subprocess_run("git diff --quiet -- . ':!searx/settings.yml' ':!utils/brand.env'")
     except subprocess.CalledProcessError as e:
         if e.returncode == 1:
-            git_version += "-dirty"
+            git_version += "+dirty"
         else:
             logger.warning('"%s" returns an unexpected return code %i', e.returncode, e.cmd)
     return git_version, tag_version


Test patch: