Skip to content

Commit

Permalink
update commit_hash management
Browse files Browse the repository at this point in the history
  • Loading branch information
skoudoro committed Apr 15, 2023
1 parent c60e99f commit 42e5dd2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
14 changes: 4 additions & 10 deletions fury/__init__.py
@@ -1,15 +1,7 @@
"""Init file for visualization package."""
import warnings

try:
from ._version import version as __version__
from ._version import version_tuple

__revision_id__ = version_tuple[-1][1:9]
except ImportError:
__version__ = 'unknown version'
version_tuple = (0, 0, 0, 'unknown version', 'unknown version')
__revision_id__ = 'unknown revision'
from fury.pkg_info import __version__, pkg_commit_hash


def get_info(verbose=False):
Expand Down Expand Up @@ -37,10 +29,12 @@ def get_info(verbose=False):
mpl, have_mpl, _ = optional_package('matplotlib')
dipy, have_dipy, _ = optional_package('dipy')

install_type, commit_hash = pkg_commit_hash(dirname(__file__))

info = dict(
fury_version=__version__,
pkg_path=dirname(__file__),
commit_hash=__revision_id__,
commit_hash=commit_hash,
sys_version=sys.version,
sys_executable=sys.executable,
sys_platform=sys.platform,
Expand Down
55 changes: 55 additions & 0 deletions fury/pkg_info.py
@@ -0,0 +1,55 @@
from __future__ import annotations

from subprocess import run

from packaging.version import Version

try:
from ._version import __version__
except ImportError:
__version__ = '0+unknown'

COMMIT_HASH = '$Format:%h$'


def pkg_commit_hash(pkg_path: str | None = None) -> tuple[str, str]:
"""Get short form of commit hash
In this file is a variable called COMMIT_HASH. This contains a substitution
pattern that may have been filled by the execution of ``git archive``.
We get the commit hash from (in order of preference):
* A substituted value in ``archive_subst_hash``
* A truncated commit hash value that is part of the local portion of the
version
* git's output, if we are in a git repository
If all these fail, we return a not-found placeholder tuple
Parameters
----------
pkg_path : str
directory containing package
Returns
-------
hash_from : str
Where we got the hash from - description
hash_str : str
short form of hash
"""
if not COMMIT_HASH.startswith('$Format'): # it has been substituted
return 'archive substitution', COMMIT_HASH
ver = Version(__version__)
if ver.local is not None and ver.local.startswith('g'):
return 'installation', ver.local[1:8]
# maybe we are in a repository
proc = run(
('git', 'rev-parse', '--short', 'HEAD'),
capture_output=True,
cwd=pkg_path,
)
if proc.stdout:
return 'repository', proc.stdout.decode().strip()
return '(none found)', '<not found>'

0 comments on commit 42e5dd2

Please sign in to comment.