diff --git a/mrdna/version.py b/mrdna/version.py index f543dbaa39846aaca819086e315c0be41b5299fc..404ef8f3225ad7f66b24bdfa7e3405c78e4a2626 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