From ebb2636900d9979fee117f65ce0da2c030d55ab2 Mon Sep 17 00:00:00 2001
From: Gregory Ashton <gregory.ashton@ligo.org>
Date: Mon, 27 Jan 2020 14:08:35 +0100
Subject: [PATCH] Improve the version file information

---
 pyfstat/__init__.py         |  5 ++++
 pyfstat/helper_functions.py | 11 +++++++++
 setup.py                    | 47 ++++++++++++++++++++++++++++++++++++-
 3 files changed, 62 insertions(+), 1 deletion(-)

diff --git a/pyfstat/__init__.py b/pyfstat/__init__.py
index e81fdb0..6af2b7e 100644
--- a/pyfstat/__init__.py
+++ b/pyfstat/__init__.py
@@ -26,3 +26,8 @@ from .grid_based_searches import (
     SliceGridSearch,
     TransientGridSearch,
 )
+
+
+from .helper_functions import get_version_information
+
+__version__ = get_version_information()
diff --git a/pyfstat/helper_functions.py b/pyfstat/helper_functions.py
index bddaa9b..806d485 100644
--- a/pyfstat/helper_functions.py
+++ b/pyfstat/helper_functions.py
@@ -390,3 +390,14 @@ def match_commandlines(cl1, cl2, be_strict_about_full_executable_path=False):
         cl2s[0] = os.path.basename(cl2s[0])
     unmatched = np.setxor1d(cl1s, cl2s)
     return len(unmatched) == 0
+
+
+def get_version_information():
+    version_file = os.path.join(
+        os.path.dirname(os.path.dirname(__file__)), "pyfstat/.version"
+    )
+    try:
+        with open(version_file, "r") as f:
+            return f.readline().rstrip()
+    except EnvironmentError:
+        print("No version information file '.version' found")
diff --git a/setup.py b/setup.py
index 69b60fd..64a0f1c 100644
--- a/setup.py
+++ b/setup.py
@@ -3,6 +3,47 @@
 from setuptools import setup, find_packages
 from os import path
 import sys
+import subprocess
+
+
+def write_version_file(version):
+    """ Writes a file with version information to be used at run time
+
+    Parameters
+    ----------
+    version: str
+        A string containing the current version information
+
+    Returns
+    -------
+    version_file: str
+        A path to the version file
+
+    """
+    try:
+        git_log = subprocess.check_output(
+            ["git", "log", "-1", "--pretty=%h %ai"]
+        ).decode("utf-8")
+        git_diff = (
+            subprocess.check_output(["git", "diff", "."])
+            + subprocess.check_output(["git", "diff", "--cached", "."])
+        ).decode("utf-8")
+        if git_diff == "":
+            git_status = "(CLEAN) " + git_log
+        else:
+            git_status = "(UNCLEAN) " + git_log
+    except Exception as e:
+        print("Unable to obtain git version information, exception: {}".format(e))
+        git_status = ""
+
+    version_file = ".version"
+    if path.isfile(version_file) is False:
+        with open("pyfstat/" + version_file, "w+") as f:
+            f.write("{}: {}".format(version, git_status))
+        print("Done", version_file, version, git_status)
+
+    return version_file
+
 
 # check python version
 min_python_version = (3, 5, 0)  # (major,minor,micro)
@@ -21,9 +62,12 @@ here = path.abspath(path.dirname(__file__))
 with open(path.join(here, "README.md"), encoding="utf-8") as f:
     long_description = f.read()
 
+VERSION = "1.3"
+version_file = write_version_file(VERSION)
+
 setup(
     name="PyFstat",
-    version="1.3",
+    version=VERSION,
     author="Gregory Ashton, David Keitel, Reinhard Prix",
     author_email="gregory.ashton@ligo.org",
     license="MIT",
@@ -36,6 +80,7 @@ setup(
         "pyfstat": [
             "pyCUDAkernels/cudaTransientFstatExpWindow.cu",
             "pyCUDAkernels/cudaTransientFstatRectWindow.cu",
+            version_file,
         ]
     },
     python_requires=">=%s.%s.%s" % min_python_version[:3],
-- 
GitLab