From 30ad1cc0869d13d7cfc67a1c124d38ae6fe154ce Mon Sep 17 00:00:00 2001
From: Tamas Gal <tgal@km3net.de>
Date: Thu, 27 Feb 2020 10:50:18 +0100
Subject: [PATCH] Modernise versioning

---
 .gitignore           |  4 ++--
 MANIFEST.in          |  1 -
 doc/conf.py          |  8 +++-----
 km3io/__init__.py    |  8 ++++++--
 km3io/__version__.py | 24 ------------------------
 pyproject.toml       |  5 +++++
 setup.py             |  5 +----
 7 files changed, 17 insertions(+), 38 deletions(-)
 delete mode 100644 km3io/__version__.py
 create mode 100644 pyproject.toml

diff --git a/.gitignore b/.gitignore
index 4cd2784..7fe6ec5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,8 +6,8 @@ __pycache__/
 # C extensions
 *.so
 
-# Version info for PyPI
-km3io/version.txt
+# Version info
+km3io/version.py
 
 # Distribution / packaging
 .Python
diff --git a/MANIFEST.in b/MANIFEST.in
index 40c6e45..8da04a0 100644
--- a/MANIFEST.in
+++ b/MANIFEST.in
@@ -1,4 +1,3 @@
 include README.rst
 include LICENSE
 include requirements.txt
-include km3io/version.txt
diff --git a/doc/conf.py b/doc/conf.py
index 944303b..cf1e47b 100644
--- a/doc/conf.py
+++ b/doc/conf.py
@@ -18,13 +18,11 @@ import os
 from datetime import date
 import sphinx_rtd_theme
 from pkg_resources import get_distribution
-import km3io
 
 # -- Project information -----------------------------------------------------
 
-release = get_distribution('km3io').version
-version = '.'.join(release.split('.')[:2])
-project = 'km3io {}'.format(km3io.__version__)
+version = get_distribution('km3io').version
+project = 'km3io {}'.format(version)
 copyright = '{0}, Zineb Aly and Tamas Gal'.format(date.today().year)
 author = 'Zineb Aly, Tamas Gal, Johannes Schumann'
 
@@ -74,4 +72,4 @@ html_theme_path = [sphinx_rtd_theme.get_html_theme_path()]
 # relative to this directory. They are copied after the builtin static files,
 # so a file named "default.css" will overwrite the builtin "default.css".
 # html_static_path = ['_static']
-html_title = "km3io {}".format(km3io.__version__)
+html_title = "km3io {}".format(version)
diff --git a/km3io/__init__.py b/km3io/__init__.py
index 6c7748f..edd00a1 100644
--- a/km3io/__init__.py
+++ b/km3io/__init__.py
@@ -1,5 +1,9 @@
-from .__version__ import version
-__version__ = version
+from pkg_resources import get_distribution, DistributionNotFound
+
+try:
+    version = get_distribution(__name__).version
+except DistributionNotFound:
+    version = "unknown version"
 
 from .offline import OfflineReader
 from .daq import DAQReader
diff --git a/km3io/__version__.py b/km3io/__version__.py
deleted file mode 100644
index f9ea4bd..0000000
--- a/km3io/__version__.py
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env python
-# Filename: __version__.py
-# pylint: disable=C0103
-"""
-Pep 386 compliant version info.
-
-    (major, minor, micro, alpha/beta/rc/final, #)
-    (1, 1, 2, 'alpha', 0) => "1.1.2.dev"
-    (1, 2, 0, 'beta', 2) => "1.2b2"
-
-"""
-from __future__ import absolute_import, print_function, division
-
-from os.path import dirname, realpath, join
-
-from setuptools_scm import get_version
-
-version = 'unknown version'
-
-try:
-    version = get_version(root='..', relative_to=__file__)
-except LookupError:
-    with open(join(realpath(dirname(__file__)), "version.txt"), 'r') as fobj:
-        version = fobj.read()
diff --git a/pyproject.toml b/pyproject.toml
new file mode 100644
index 0000000..8c1be0f
--- /dev/null
+++ b/pyproject.toml
@@ -0,0 +1,5 @@
+[build-system]
+requires = ["setuptools>=42", "wheel", "setuptools_scm[toml]>=3.4"]
+
+[tool.setuptools_scm]
+write_to = "km3io/version.py"
diff --git a/setup.py b/setup.py
index 7f9d245..9348c37 100644
--- a/setup.py
+++ b/setup.py
@@ -26,10 +26,7 @@ setup(
     include_package_data=True,
     platforms='any',
     setup_requires=['setuptools_scm'],
-    use_scm_version={
-        'write_to': 'km3io/version.txt',
-        'tag_regex': r'^(?P<prefix>v)?(?P<version>[^\+]+)(?P<suffix>.*)?$',
-    },
+    use_scm_version=True,
     install_requires=requirements,
     python_requires='>=3.5',
     entry_points={
-- 
GitLab