1
1
import sys
2
+ from subprocess import run
2
3
3
4
from packaging .version import Version
4
5
8
9
__version__ = '0+unknown'
9
10
10
11
12
+ COMMIT_HASH = '$Format:%h$'
13
+
14
+
11
15
def _cmp (a , b ):
12
16
"""Implementation of ``cmp`` for Python 3"""
13
17
return (a > b ) - (a < b )
@@ -64,15 +68,20 @@ def cmp_pkg_version(version_str, pkg_version_str=__version__):
64
68
return _cmp (Version (version_str ), Version (pkg_version_str ))
65
69
66
70
67
- def pkg_commit_hash (pkg_path = None ):
71
+ def pkg_commit_hash (pkg_path : str = None ):
68
72
"""Get short form of commit hash
69
73
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):
74
78
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
76
85
77
86
Parameters
78
87
----------
@@ -86,17 +95,20 @@ def pkg_commit_hash(pkg_path=None):
86
95
hash_str : str
87
96
short form of hash
88
97
"""
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>'
100
112
101
113
102
114
def get_pkg_info (pkg_path ):
@@ -112,7 +124,7 @@ def get_pkg_info(pkg_path):
112
124
context : dict
113
125
with named parameters of interest
114
126
"""
115
- src , hsh = pkg_commit_hash ()
127
+ src , hsh = pkg_commit_hash (pkg_path )
116
128
import numpy
117
129
118
130
return dict (
0 commit comments