From 1851beeef9d2b3d05efe48e3dfbebfbde32c2e25 Mon Sep 17 00:00:00 2001
From: Nikolay Stanchev <ns17@it-innovation.soton.ac.uk>
Date: Fri, 31 Aug 2018 10:49:58 +0100
Subject: [PATCH] Redactors test module setup.py

---
 src/test/MANIFEST.in |  4 ++--
 src/test/VERSION     |  1 +
 src/test/setup.py    | 39 ++++++++++++++++++++++++---------------
 3 files changed, 27 insertions(+), 17 deletions(-)
 create mode 100644 src/test/VERSION

diff --git a/src/test/MANIFEST.in b/src/test/MANIFEST.in
index ed111d8..e7b7744 100644
--- a/src/test/MANIFEST.in
+++ b/src/test/MANIFEST.in
@@ -1,2 +1,2 @@
-include MANIFEST.in
-recursive-include clmctest _version.py *.yml *.yaml *.sh *.json *.conf
\ No newline at end of file
+include VERSION
+recursive-include clmctest *.yml *.yaml *.sh *.json *.conf
\ No newline at end of file
diff --git a/src/test/VERSION b/src/test/VERSION
new file mode 100644
index 0000000..4a2bfa8
--- /dev/null
+++ b/src/test/VERSION
@@ -0,0 +1 @@
+__version__ = "1.2.0"
\ No newline at end of file
diff --git a/src/test/setup.py b/src/test/setup.py
index 8a52b18..7f03b5e 100644
--- a/src/test/setup.py
+++ b/src/test/setup.py
@@ -27,32 +27,41 @@ import os.path
 from setuptools import setup, find_packages
 
 
-def read(fname):
-    return open(os.path.join(os.path.dirname(__file__), fname)).read()
+def get_version(*relative_path):
+    """
+    Reads and parses a version file.
 
+    :param relative_path: iterable representing the relative path to the version file
+    :return:
+    """
+
+    fname = os.path.join(os.path.dirname(__file__), *relative_path)
 
-def get_version(fname):
     if os.path.isfile(fname):
-      git_revision = read(fname)
+        with open(fname) as f:  # Use context managers when opening files, otherwise file handlers might not be properly closed
+            version = {}
+            # execute the version file and put its content in the version dictionary
+            exec(f.read(), version)
+            # extract the __version__ variable from the dictionary, if not found use default value "SNAPSHOT"
+            git_revision = version.get("__version__", "SNAPSHOT")
     else:
-      git_revision = "SNAPSHOT"
+        git_revision = "SNAPSHOT"
 
     return git_revision
 
 
 setup(
-    name = "clmctest",
-    version = get_version("_version.py"),
-    author = "Michael Boniface",
-    author_email = "mjb@it-innovation.soton.ac.uk",
-    description = "FLAME CLMC Test Module",
-    license = "https://gitlab.it-innovation.soton.ac.uk/FLAME/flame-clmc/blob/integration/LICENSE",
-    keywords = "FLAME CLMC",
+    name="clmctest",
+    version=get_version("VERSION"),
+    author="Michael Boniface",
+    author_email="mjb@it-innovation.soton.ac.uk",
+    description="FLAME CLMC Test Module",
+    license="https://gitlab.it-innovation.soton.ac.uk/FLAME/flame-clmc/blob/integration/LICENSE",
+    keywords="FLAME CLMC",
     url='https://gitlab.it-innovation.soton.ac.uk/FLAME/flame-clmc',
-    packages=find_packages(exclude=["services"]),
+    packages=find_packages(),
     include_package_data=True,
-    package_data={'': ['_version.py', '*.yml', '*.yaml', '*.sh', '*.json', '*.conf']},
-    long_description="FLAME CLMC",
+    long_description="FLAME CLMC Test",
     classifiers=[
         "Development Status :: Alpha",
         "Topic :: FLAME Tests",
-- 
GitLab