From 096afbd25979dfcc2f6da406782d78ec71c843e5 Mon Sep 17 00:00:00 2001
From: Janis Streib <me@janis-streib.de>
Date: Wed, 15 Nov 2023 19:19:30 +0100
Subject: [PATCH] UPD: dynamic version + publish in registry

---
 .gitlab-ci.yml |  7 ++++---
 pyproject.toml |  2 +-
 setup.py       | 24 ++++++++++++++++--------
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 6bed27a..7aa1222 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -20,11 +20,11 @@ before_script:
   - apt-get -y update
   - command -v git || apt-get -y install git
   - command -v python3 || apt-get -y install python3
-  - command -v pip3 || apt-get -y install python3-pip
+  - command -v pip3 || apt-get -y install python3-pip twine
   - python3 -V  # Print out python version for debugging
   - pip3 install build
   - python3 -m venv || apt-get -y install python3-venv
-  - if [[ "$CI_COMMIT_BRANCH" != "master" ]]; then sed -i "s%git+https://git.scc.kit.edu/scc-net/net-suite/api-generator.git@master%git+https://git.scc.kit.edu/scc-net/net-suite/api-generator.git@${CI_COMMIT_BRANCH}%g" pyproject.toml; fi
+  - if [[ "$CI_COMMIT_BRANCH" != "main" ]]; then sed -i "s%git+https://git.scc.kit.edu/scc-net/net-suite/api-generator.git@master%git+https://git.scc.kit.edu/scc-net/net-suite/api-generator.git@${CI_COMMIT_BRANCH}%g" pyproject.toml; fi
   - if [[ "$CI_COMMIT_BRANCH" == "devel" ]]; then export NETDB_ENDPOINT=${NETDB_DEVEL_ENDPOINT}; fi
 
 
@@ -35,10 +35,11 @@ build:
   stage: build
   script:
     - python3 -m build
+    - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python3 -m twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/*
   artifacts:
     paths:
       - dist/*
     expire_in: 2 days
   only:
-    - master
+    - main
     - devel
diff --git a/pyproject.toml b/pyproject.toml
index 4ef2563..9f2f998 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,4 +1,4 @@
 [build-system]
 # Minimum requirements for the build system to execute.
-requires = ["setuptools", "wheel", "net_api_generator @ git+https://git.scc.kit.edu/scc-net/net-suite/api-generator.git@master"]
+requires = ["setuptools", "wheel", "net_api_generator @ git+https://git.scc.kit.edu/scc-net/net-suite/api-generator.git@master", "semver", "requests"]
 build-backend = "setuptools.build_meta"
diff --git a/setup.py b/setup.py
index ee6d277..3c3a2a1 100644
--- a/setup.py
+++ b/setup.py
@@ -1,13 +1,22 @@
 from setuptools import setup
-from distutils.command.build_py import build_py
+from setuptools.command.build_py import build_py
 import subprocess
 import os
+import requests
+import semver
 
+        
+vers = requests.get(f"https://{os.environ.get('NETDB_ENDPOINT')}").json()[0]
+largest_ver = None
+for v in vers:
+    sem_v = semver.Version.parse(v['numeric'])
+    if largest_ver is None or largest_ver < sem_v:
+        largest_ver = sem_v 
 
 class APIGenBuild(build_py):
     def run(self):
-        build_versions = ['3.2', '4.0']
-        default_version = '4.0'
+        build_versions = [f'{v["major"]}.{v["minor"]}' for v in vers]
+        default_version = f'{largest_ver.major}.0'
         environ = os.environ.copy()
         for version in build_versions:
             target_dir = os.path.join(self.build_lib, 'netdb_client', f"api{version.replace('.', '')}")
@@ -18,15 +27,14 @@ class APIGenBuild(build_py):
         environ['NETDB_VERSION'] = default_version
         subprocess.run(['net-api-generator', 'python', f'--output-dir={target_dir}'], check=True, env=environ)
         build_py.run(self)
-        
 
 setup(
     name='netdb_client',
-    version='4.0',
-    author='Dr. Meta',
-    description='This is a meta package to install the automatically generated NET-API 3.0, 3.1, 3.2 and 4.0 definitions.',
+    version=f'{str(largest_ver)}+{os.environ.get("CI_COMMIT_BRANCH", "local")}-{os.environ.get("CI_COMMIT_SHORT_SHA", "HEAD")}',
+    author='NETVS-Team <netvs@scc.kit.edu>',
+    description='This is a meta package to install the automatically generated NET-API definitionsi for the currently supported API versions.',
     url='https://git.scc.kit.edu/scc-net/net-suite/netdb-client-lib',
-    install_requires=['requests'],
+    install_requires=['requests', 'semver'],
     python_requires='>=3.7',
     packages=['netdb_client'],
     cmdclass={'build_py': APIGenBuild}
-- 
GitLab