Skip to content

Commit 93d74d9

Browse files
committed
RF: Update pkg_info to work with setuptools_scm
1 parent 3c1ce1b commit 93d74d9

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
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: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import sys
23

34
from packaging.version import Version
@@ -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)
@@ -67,12 +71,20 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
6771
def pkg_commit_hash(pkg_path=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+
There should be a file called 'COMMIT_INFO.txt' in `pkg_path`. This is a
75+
file in INI file format, with at least one section: ``commit hash``, and
76+
two variables ``archive_subst_hash`` and ``install_hash``. The first has a
77+
substitution pattern in it which may have been filled by the execution of
78+
``git archive`` if this is an archive generated that way. The second is
79+
filled in by the installation, if the installation is from a git archive.
80+
81+
We get the commit hash from (in order of preference):
82+
83+
* A substituted value in ``archive_subst_hash``
84+
* A written commit hash value in ``install_hash`
85+
* git's output, if we are in a git repository
7486
75-
If detection fails, we return a not-found placeholder tuple
87+
If all these fail, we return a not-found placeholder tuple
7688
7789
Parameters
7890
----------
@@ -86,17 +98,20 @@ def pkg_commit_hash(pkg_path=None):
8698
hash_str : str
8799
short form of hash
88100
"""
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
101+
if not COMMIT_HASH.startswith('$Format'): # it has been substituted
102+
return 'archive substitution', COMMIT_HASH
103+
ver = Version(__version__)
104+
if ver.local is not None and ver.local.startswith('g'):
105+
return ver.local[1:8], 'installation'
106+
# maybe we are in a repository
107+
proc = subprocess.Popen('git rev-parse --short HEAD',
108+
stdout=subprocess.PIPE,
109+
stderr=subprocess.PIPE,
110+
cwd=pkg_path, shell=True)
111+
repo_commit, _ = proc.communicate()
112+
if repo_commit:
113+
return 'repository', repo_commit.strip()
114+
return '(none found)', '<not found>'
100115

101116

102117
def get_pkg_info(pkg_path):
@@ -112,7 +127,7 @@ def get_pkg_info(pkg_path):
112127
context : dict
113128
with named parameters of interest
114129
"""
115-
src, hsh = pkg_commit_hash()
130+
src, hsh = pkg_commit_hash(pkg_path)
116131
import numpy
117132

118133
return dict(

0 commit comments

Comments
 (0)