Skip to content

Commit 5cc66aa

Browse files
committed
RF: Update pkg_info to work with setuptools_scm
1 parent 4c1da79 commit 5cc66aa

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.git_archival.txt export-subst
2+
nibabel/pkg_info.py export-subst

nibabel/pkg_info.py

+30-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from subprocess import run
23

34
from packaging.version import Version
45

@@ -8,6 +9,9 @@
89
__version__ = '0+unknown'
910

1011

12+
COMMIT_HASH = '$Format:%h$'
13+
14+
1115
def _cmp(a, b):
1216
"""Implementation of ``cmp`` for Python 3"""
1317
return (a > b) - (a < b)
@@ -64,15 +68,20 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
6468
return _cmp(Version(version_str), Version(pkg_version_str))
6569

6670

67-
def pkg_commit_hash(pkg_path=None):
71+
def pkg_commit_hash(pkg_path: str = None):
6872
"""Get short form of commit hash
6973
70-
Versioneer placed a ``_version.py`` file in the package directory. This file
71-
gets updated on installation or ``git archive``.
72-
We inspect the contents of ``_version`` to detect whether we are in a
73-
repository, an archive of the repository, or an installed package.
74+
In this file is a variable called COMMIT_HASH. This contains a substitution
75+
pattern that may have been filled by the execution of ``git archive``.
76+
77+
We get the commit hash from (in order of preference):
7478
75-
If detection fails, we return a not-found placeholder tuple
79+
* A substituted value in ``archive_subst_hash``
80+
* A truncated commit hash value that is part of the local portion of the
81+
version
82+
* git's output, if we are in a git repository
83+
84+
If all these fail, we return a not-found placeholder tuple
7685
7786
Parameters
7887
----------
@@ -86,17 +95,20 @@ def pkg_commit_hash(pkg_path=None):
8695
hash_str : str
8796
short form of hash
8897
"""
89-
versions = _version.get_versions()
90-
hash_str = versions['full-revisionid'][:7]
91-
if hasattr(_version, 'version_json'):
92-
hash_from = 'installation'
93-
elif not _version.get_keywords()['full'].startswith('$Format:'):
94-
hash_from = 'archive substitution'
95-
elif versions['version'] == '0+unknown':
96-
hash_from, hash_str = '(none found)', '<not found>'
97-
else:
98-
hash_from = 'repository'
99-
return hash_from, hash_str
98+
if not COMMIT_HASH.startswith('$Format'): # it has been substituted
99+
return 'archive substitution', COMMIT_HASH
100+
ver = Version(__version__)
101+
if ver.local is not None and ver.local.startswith('g'):
102+
return ver.local[1:8], 'installation'
103+
# maybe we are in a repository
104+
proc = run(
105+
('git', 'rev-parse', '--short', 'HEAD'),
106+
capture_output=True,
107+
cwd=pkg_path,
108+
)
109+
if proc.stdout:
110+
return 'repository', proc.stdout.strip()
111+
return '(none found)', '<not found>'
100112

101113

102114
def get_pkg_info(pkg_path):
@@ -112,7 +124,7 @@ def get_pkg_info(pkg_path):
112124
context : dict
113125
with named parameters of interest
114126
"""
115-
src, hsh = pkg_commit_hash()
127+
src, hsh = pkg_commit_hash(pkg_path)
116128
import numpy
117129

118130
return dict(

0 commit comments

Comments
 (0)