Skip to content

Commit 20db217

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

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
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

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
__version__ = '0+unknown'
99

1010

11+
COMMIT_HASH = '$Format:%h$'
12+
13+
1114
def _cmp(a, b):
1215
"""Implementation of ``cmp`` for Python 3"""
1316
return (a > b) - (a < b)
@@ -64,15 +67,20 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
6467
return _cmp(Version(version_str), Version(pkg_version_str))
6568

6669

67-
def pkg_commit_hash(pkg_path=None):
70+
def pkg_commit_hash(pkg_path: str = None):
6871
"""Get short form of commit hash
6972
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.
73+
In this file is a variable called COMMIT_HASH. This contains a substitution
74+
pattern that may have been filled by the execution of ``git archive``.
75+
76+
We get the commit hash from (in order of preference):
7477
75-
If detection fails, we return a not-found placeholder tuple
78+
* A substituted value in ``archive_subst_hash``
79+
* A truncated commit hash value that is part of the local portion of the
80+
version
81+
* git's output, if we are in a git repository
82+
83+
If all these fail, we return a not-found placeholder tuple
7684
7785
Parameters
7886
----------
@@ -86,17 +94,22 @@ def pkg_commit_hash(pkg_path=None):
8694
hash_str : str
8795
short form of hash
8896
"""
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
97+
from subprocess import run
98+
99+
if not COMMIT_HASH.startswith('$Format'): # it has been substituted
100+
return 'archive substitution', COMMIT_HASH
101+
ver = Version(__version__)
102+
if ver.local is not None and ver.local.startswith('g'):
103+
return ver.local[1:8], 'installation'
104+
# maybe we are in a repository
105+
proc = run(
106+
('git', 'rev-parse', '--short', 'HEAD'),
107+
capture_output=True,
108+
cwd=pkg_path,
109+
)
110+
if proc.stdout:
111+
return 'repository', proc.stdout.strip()
112+
return '(none found)', '<not found>'
100113

101114

102115
def get_pkg_info(pkg_path):
@@ -112,7 +125,7 @@ def get_pkg_info(pkg_path):
112125
context : dict
113126
with named parameters of interest
114127
"""
115-
src, hsh = pkg_commit_hash()
128+
src, hsh = pkg_commit_hash(pkg_path)
116129
import numpy
117130

118131
return dict(

0 commit comments

Comments
 (0)