From fcfc0ca6b7598bc7836d495b67a452287fe59c26 Mon Sep 17 00:00:00 2001
From: Bert Palm <bert.palm@ufz.de>
Date: Thu, 27 Jul 2023 16:49:08 +0200
Subject: [PATCH] Rolling release

---
 .gitlab-ci.yml   | 40 ++++++++++++++++++++++++++++++++++++++++
 pyproject.toml   |  2 +-
 saqc/__init__.py |  4 ++++
 saqc/_version.py |  2 +-
 setup.py         | 32 +++++++++++++++++++++++++++++---
 5 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 5e5ab6017..ccaa4c1f5 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,6 +2,20 @@
 #
 # SPDX-License-Identifier: GPL-3.0-or-later
 
+
+# ===========================================================
+# Hints
+# ===========================================================
+
+# $PYPI_PKG_NAME
+#   The variable PYPI_PKG_NAME is used in setup.py to determine
+#   how to name the tarball package. If not set the package is
+#   named 'saqc'.
+
+# $TESTPYPI_TOKEN
+#   The upload token used for testpypi, set it on the gitlab
+#   page and enable masking to prevent revealing
+
 # ===========================================================
 # preparation
 # ===========================================================
@@ -138,6 +152,8 @@ doctest:
 wheel38:
   stage: build
   image: python:3.8
+  variables:
+    PYPI_PKG_NAME: "saqc-dev"
   script:
     - pip install wheel
     - pip wheel .
@@ -146,6 +162,8 @@ wheel38:
 wheel39:
   stage: build
   image: python:3.9
+  variables:
+    PYPI_PKG_NAME: "saqc-dev"
   script:
     - pip install wheel
     - pip wheel .
@@ -154,6 +172,8 @@ wheel39:
 wheel310:
   stage: build
   image: python:3.10
+  variables:
+    PYPI_PKG_NAME: "saqc-dev"
   script:
     - pip install wheel
     - pip wheel .
@@ -162,6 +182,8 @@ wheel310:
 wheel311:
   stage: build
   image: python:3.11
+  variables:
+    PYPI_PKG_NAME: "saqc-dev"
   script:
     - pip install wheel
     - pip wheel .
@@ -179,6 +201,24 @@ docs:
 # Extra Pipeline (run with a successful run of all other jobs on develop)
 # ===========================================================
 
+upload_testpypi:
+  stage: deploy
+  only:
+    - develop
+  except:
+    - schedules
+  variables:
+    PYPI_PKG_NAME: "saqc-dev"
+    TWINE_USERNAME: __token__
+    TWINE_PASSWORD: $TESTPYPI_TOKEN
+  script:
+    - pip install build twine
+    - python -m build
+    - twine check --strict dist/*
+    - twine upload -r testpypi dist/*
+    - echo "install latest develop version with (all option flags are necessary):"
+    - echo "pip install --pre -U -i https://test.pypi.org/simple/ --extra-index-url http://pypi.org/simple saqc-dev`"
+
 # make html docu with sphinx
 pages:
   stage: deploy
diff --git a/pyproject.toml b/pyproject.toml
index 13885dde6..6ad7a8f81 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -12,7 +12,7 @@ markers = "slow: marks tests as slow (deselect with '-m \"not slow\"')"
 
 [tool.versioneer]
 VCS = "git"
-style = "pep440"
+style = "pep440-pre"
 versionfile_source = "saqc/_version.py"
 versionfile_build = "saqc/_version.py"
 tag_prefix = "v"
diff --git a/saqc/__init__.py b/saqc/__init__.py
index f6e1d7df3..d2646a84f 100644
--- a/saqc/__init__.py
+++ b/saqc/__init__.py
@@ -12,3 +12,7 @@ from saqc.core import Flags, DictOfSeries, SaQC
 from saqc.core.translation import DmpScheme, FloatScheme, PositionalScheme, SimpleScheme
 from saqc.parsing.reader import fromConfig
 from saqc.version import __version__
+
+from . import _version
+
+__version__ = _version.get_versions()["version"]
diff --git a/saqc/_version.py b/saqc/_version.py
index de714903a..fc95f3597 100644
--- a/saqc/_version.py
+++ b/saqc/_version.py
@@ -52,7 +52,7 @@ def get_config() -> VersioneerConfig:
     # _version.py
     cfg = VersioneerConfig()
     cfg.VCS = "git"
-    cfg.style = "pep440"
+    cfg.style = "pep440-pre"
     cfg.tag_prefix = "v"
     cfg.parentdir_prefix = "saqc-"
     cfg.versionfile_source = "saqc/_version.py"
diff --git a/setup.py b/setup.py
index f81576440..b56d0d23d 100644
--- a/setup.py
+++ b/setup.py
@@ -1,5 +1,6 @@
 # SPDX-FileCopyrightText: 2021 Helmholtz-Zentrum für Umweltforschung GmbH - UFZ
 # SPDX-License-Identifier: GPL-3.0-or-later
+import os
 
 import versioneer
 from setuptools import find_packages, setup
@@ -10,10 +11,35 @@ from setuptools import find_packages, setup
 with open("README.md", "r") as fh:
     long_description = fh.read()
 
+
+name = os.environ.get("PYPI_PKG_NAME", "saqc")
+if not name:
+    raise ValueError("Environment variable PYPI_PKG_NAME must not be an empty string.")
+
+
+v = versioneer.get_versions()
+print(f"saqc version: {v}")
+
+if v["error"]:
+    raise RuntimeError(v["error"])
+
+if v["dirty"]:
+    raise ValueError(
+        f"The repository you build is dirty. Please commit changes first {v}."
+    )
+
+if "dev" in v["version"] and name == "saqc":
+    raise ValueError(
+        f"An saqc release must have version in the format X.Y.Z, "
+        f"which requires a git tag on the same commit. Please set "
+        f"a tag, then build again. {v}"
+    )
+
+
 setup(
-    name="saqc",
-    version=versioneer.get_version(),
-    cmdclass=versioneer.get_cmdclass(),
+    name=name,
+    version=versioneer.get_version(),  # keep this line as it is
+    cmdclass=versioneer.get_cmdclass(),  # keep this line as it is
     author="Bert Palm, David Schaefer, Florian Gransee, Peter Luenenschloss",
     author_email="david.schaefer@ufz.de",
     description="A timeseries data quality control and processing tool/framework",
-- 
GitLab