From 45bbc7675d3074beb8dfae6a2d7ef790e054fad7 Mon Sep 17 00:00:00 2001
From: Chris Maffeo <cmaffeo2@illinois.edu>
Date: Tue, 3 Mar 2020 17:44:55 -0600
Subject: [PATCH] Fixed version script so it changes directories to
 version.__file__

---
 mrdna/version.py | 49 +++++++++++++++++++++++++++---------------------
 1 file changed, 28 insertions(+), 21 deletions(-)

diff --git a/mrdna/version.py b/mrdna/version.py
index f543dba..404ef8f 100644
--- a/mrdna/version.py
+++ b/mrdna/version.py
@@ -38,6 +38,7 @@ __all__ = ("get_version")
 from pathlib import Path
 import subprocess
 from subprocess import Popen, PIPE
+import os
 
 maintainer = "Chris Maffeo <cmaffeo2@illinois.edu>"
 
@@ -49,7 +50,7 @@ def check_git_repository():
         remotes = subprocess.check_output(['git', 'remote', '-v'], stderr=subprocess.STDOUT)
         return b'mrdna.git' in remotes
     except:
-        return False   
+        return False
 
 def call_git_describe(abbrev):
     try:
@@ -100,38 +101,44 @@ def get_version(abbrev=7):
     
     release_version = read_release_version()
 
-    if not check_git_repository():
-        return release_version
-        # raise Exception(__function__ +" called from outside a version controlled source repository")
+    old_wd = os.getcwd()
+    try:
+        os.chdir(Path(__file__).parent)
 
+        if not check_git_repository():
+            return release_version
+            # raise Exception(__function__ +" called from outside a version controlled source repository")
 
-    # First try to get the current version using “git describe”.
 
-    split_version = call_git_describe(abbrev).split("-")
-    version = split_version[0]
-    if len(split_version) > 1 and int(split_version[-2]) > 0:
-        version += ".dev{}".format(int(split_version[-2]))
+        # First try to get the current version using “git describe”.
 
+        split_version = call_git_describe(abbrev).split("-")
+        version = split_version[0]
+        if len(split_version) > 1 and int(split_version[-2]) > 0:
+            version += ".dev{}".format(int(split_version[-2]))
 
-    # If that doesn't work, fall back on the value that's in
-    # RELEASE-VERSION.
 
-    if version is None:
-        version = release_version
+        # If that doesn't work, fall back on the value that's in
+        # RELEASE-VERSION.
 
-    # If we still don't have anything, that's an error.
+        if version is None:
+            version = release_version
 
-    if version is None:
-        raise ValueError("Cannot find the version number!")
+        # If we still don't have anything, that's an error.
 
-    # If the current version is different from what's in the
-    # RELEASE-VERSION file, update the file to be current.
+        if version is None:
+            raise ValueError("Cannot find the version number!")
 
-    if version != release_version:
-        write_release_version(version)
+        # If the current version is different from what's in the
+        # RELEASE-VERSION file, update the file to be current.
 
-    # Finally, return the current version.
+        if version != release_version:
+            write_release_version(version)
 
+    finally:
+        os.chdir(old_wd)
+
+    # Finally, return the current version.
     return version
 
 
-- 
GitLab