From 791ae864b7135917e53cc0aa6766ca9a9ba3e91b Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Wed, 7 Sep 2022 14:20:14 +0200
Subject: [PATCH] [wheel] Get ogs version from env or git.

[ci] Upload wheel to GitLab registry.
---
 Applications/Python/OpenGeoSys/__init__.py |  1 +
 pyproject.toml                             |  1 +
 scripts/ci/jobs/build-wheels.yml           |  6 ++++
 setup.py                                   | 32 +++++++++++++++++++++-
 4 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/Applications/Python/OpenGeoSys/__init__.py b/Applications/Python/OpenGeoSys/__init__.py
index 7631e8c3369..628678b353d 100644
--- a/Applications/Python/OpenGeoSys/__init__.py
+++ b/Applications/Python/OpenGeoSys/__init__.py
@@ -11,5 +11,6 @@ def _program(name, args):
     return subprocess.call([os.path.join(OGS_BIN_DIR, name)] + args)
 
 
+# Binary entrypoints
 def ogs():
     raise SystemExit(_program("ogs", sys.argv[1:]))
diff --git a/pyproject.toml b/pyproject.toml
index 754315d212c..9c3d7b3b2c7 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -22,3 +22,4 @@ build-verbosity = "1"
 [tool.cibuildwheel.linux]
 manylinux-x86_64-image = "manylinux2014"
 manylinux-aarch64-image = "manylinux2014"
+environment-pass = ["OGS_VERSION"]
diff --git a/scripts/ci/jobs/build-wheels.yml b/scripts/ci/jobs/build-wheels.yml
index 0c29c74387e..6bcab571755 100644
--- a/scripts/ci/jobs/build-wheels.yml
+++ b/scripts/ci/jobs/build-wheels.yml
@@ -4,6 +4,12 @@ build wheels linux:
   tags: [envinf3-shell]
   script:
     - pipx run cibuildwheel
+    - >
+      pipx run twine upload
+      --repository-url https://gitlab.opengeosys.org/api/v4/projects/${CI_PROJECT_ID}/packages/pypi
+      --username gitlab-ci-token
+      --password ${CI_JOB_TOKEN}
+      wheelhouse/*
   artifacts:
     paths:
       - wheelhouse/
diff --git a/setup.py b/setup.py
index eaa57d94967..f95e450d35f 100644
--- a/setup.py
+++ b/setup.py
@@ -1,9 +1,39 @@
 from skbuild import setup
 from setuptools import find_packages
 
+import os
+import re
+import subprocess
+
+
+def get_version():
+    git_version = ""
+    if "OGS_VERSION" in os.environ:
+        git_version = os.environ["OGS_VERSION"]
+    else:
+        git_version = subprocess.run(
+            ["git describe --tags"],
+            capture_output=True,
+            text=True,
+            shell=True,
+        ).stdout.strip()
+
+    if re.match("\d+\.\d+\.\d+-\d+-g\w+", git_version):
+        # Make it PEP 440 compliant
+        # e.g. 6.4.2-1140-g85bbc8b4e1 -> 6.4.2.dev1140
+        m = re.match(".+?(?=-g[\w]*$)", git_version)  # strip out commit hash
+        if m:
+            return m.group(0).replace("-", ".dev")  # insert dev
+        else:
+            print("WARNING: Could not get ogs version!")
+            exit(1)
+    else:
+        return git_version
+
+
 setup(
     name="OpenGeoSys",
-    version="6.4.2",
+    version=get_version(),
     description="OpenGeoSys",
     author="OpenGeoSys Community",
     license="BSD-3-Clause",
-- 
GitLab