Skip to content

Commit 58e6360

Browse files
scottzacharris
authored andcommitted
ENH: Add Git revision hash to numpy dev version string
- Appends the first 6 characters of the Git revision used to build Numpy - Adds an additional attribute to easily obtain the full Git revision
1 parent 8adfc76 commit 58e6360

File tree

2 files changed

+27
-39
lines changed

2 files changed

+27
-39
lines changed

numpy/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
its source directory; please exit the numpy source tree, and relaunch
126126
your python intepreter from there."""
127127
raise ImportError(msg)
128+
from version import git_revision as __git_revision__
128129
from version import version as __version__
129130

130131
from _import_tools import PackageLoader

setup.py

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
ISRELEASED = False
6060
VERSION = '%d.%d.%d' % (MAJOR, MINOR, MICRO)
6161

62-
# Return the svn version as a string, raise a ValueError otherwise
63-
def svn_version():
62+
# Return the git revision as a string
63+
def git_version():
6464
def _minimal_ext_cmd(cmd):
6565
# construct minimal environment
6666
env = {}
@@ -76,25 +76,12 @@ def _minimal_ext_cmd(cmd):
7676
return out
7777

7878
try:
79-
out = _minimal_ext_cmd(['svn', 'info'])
79+
out = _minimal_ext_cmd(['git', 'rev-parse', 'HEAD'])
80+
GIT_REVISION = out.strip()
8081
except OSError:
81-
print(" --- Could not run svn info --- ")
82-
return ""
82+
GIT_REVISION = "Unknwn"
8383

84-
r = re.compile('Revision: ([0-9]+)')
85-
svnver = ""
86-
87-
out = out.decode()
88-
89-
for line in out.split('\n'):
90-
m = r.match(line.strip())
91-
if m:
92-
svnver = m.group(1)
93-
94-
if not svnver:
95-
print("Error while parsing svn version")
96-
97-
return svnver
84+
return GIT_REVISION
9885

9986
# BEFORE importing distutils, remove MANIFEST. distutils doesn't properly
10087
# update it when the contents of directories change.
@@ -106,36 +93,37 @@ def _minimal_ext_cmd(cmd):
10693
# a lot more robust than what was previously being used.
10794
builtins.__NUMPY_SETUP__ = True
10895

109-
FULLVERSION = VERSION
110-
if not ISRELEASED:
111-
FULLVERSION += '.dev'
112-
# If in git or something, bypass the svn rev
113-
if os.path.exists('.svn'):
114-
FULLVERSION += svn_version()
115-
11696
def write_version_py(filename='numpy/version.py'):
11797
cnt = """
11898
# THIS FILE IS GENERATED FROM NUMPY SETUP.PY
11999
short_version='%(version)s'
120100
version='%(version)s'
101+
full_version='%(full_version)s'
102+
git_revision='%(git_revision)s'
121103
release=%(isrelease)s
122104
123105
if not release:
124-
version += '.dev'
125-
import os
126-
svn_version_file = os.path.join(os.path.dirname(__file__),
127-
'core','__svn_version__.py')
128-
if os.path.isfile(svn_version_file):
129-
import imp
130-
svn = imp.load_module('numpy.core.__svn_version__',
131-
open(svn_version_file),
132-
svn_version_file,
133-
('.py','U',1))
134-
version += svn.version
106+
version = full_version
135107
"""
108+
FULL_VERSION = VERSION
109+
if not ISRELEASED:
110+
FULL_VERSION += '.dev'
111+
if os.path.exists('.git'):
112+
GIT_REVISION = git_version()
113+
elif os.path.exists(filename):
114+
# must be a source distribution, use existing version file
115+
from numpy.version import git_revision as GIT_REVISION
116+
else:
117+
GIT_REVISION = "Unknwn"
118+
119+
FULL_VERSION += GIT_REVISION[:6]
120+
136121
a = open(filename, 'w')
137122
try:
138-
a.write(cnt % {'version': VERSION, 'isrelease': str(ISRELEASED)})
123+
a.write(cnt % {'version': VERSION,
124+
'full_version' : FULL_VERSION,
125+
'git_revision' : GIT_REVISION,
126+
'isrelease': str(ISRELEASED)})
139127
finally:
140128
a.close()
141129

@@ -163,7 +151,6 @@ def configuration(parent_package='',top_path=None):
163151
def setup_package():
164152

165153
# Rewrite the version file everytime
166-
if os.path.exists('numpy/version.py'): os.remove('numpy/version.py')
167154
write_version_py()
168155

169156
# Perform 2to3 if needed

0 commit comments

Comments
 (0)