diff --git a/pyfstat/__init__.py b/pyfstat/__init__.py index e81fdb0ed13875a1d73e9e1bf38413692d128e3c..6af2b7e990bbe90d4adf122937cc0f17b260541f 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 bddaa9ba0431f3ff7cc88684359cdedd2b39a09a..806d485db4a4d238d0fcb3c92cfb4b75d1c2e4f9 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 69b60fd63fd6d99f3e994782bcd3aa7fe0c9f7ef..64a0f1c53d560cedc9920710b2aff8513348a0a8 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],