Skip to content

Commit

Permalink
Added Versioned mixin class to get git commit hash
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerkin committed Nov 1, 2017
1 parent 96c5016 commit 648c2d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
22 changes: 22 additions & 0 deletions sciunit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from quantities.dimensionality import Dimensionality
from quantities.quantity import Quantity
import cypy
import git

PRINT_DEBUG_STATE = False # printd does nothing by default.

Expand Down Expand Up @@ -293,3 +294,24 @@ def decorate(*args, **kwargs):

method_memoize = cypy.memoize


class Versioned(object):
"""
A Mixin class for SciUnit model instances, which provides a version string
based on the Git repository where the model is tracked.
Provided by Andrew Davison in issue #53.
"""

def get_version(self):
module = sys.modules[self.__module__]
# We use module.__file__ instead of module.__path__[0]
# to include modules without a __path__ attribute.
path = os.path.realpath(module.__file__)
repo = git.Repo(path, search_parent_directories=True)
head = repo.head
version = head.commit.hexsha
if repo.is_dirty():
version += "*"
return version
version = property(get_version)

3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
'lxml',
'nbconvert',
'ipykernel',
'nbformat',],
'nbformat',
'gitpython'],
#dependency_links = ['git+https://github.com/python-quantities/python-quantities.git@master#egg=quantities-999'],
entry_points={
'console_scripts': [
Expand Down

0 comments on commit 648c2d3

Please sign in to comment.