Skip to content

Commit 95cee78

Browse files
author
Ilan Schnell
committed
add git_version() to setup.py
1 parent d3fae53 commit 95cee78

File tree

1 file changed

+30
-43
lines changed

1 file changed

+30
-43
lines changed

setup.py

+30-43
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 = {}
@@ -72,30 +72,17 @@ def _minimal_ext_cmd(cmd):
7272
env['LANGUAGE'] = 'C'
7373
env['LANG'] = 'C'
7474
env['LC_ALL'] = 'C'
75-
out = subprocess.Popen(cmd,
76-
stdout=subprocess.PIPE, env=env).communicate()[0]
75+
out = subprocess.Popen(cmd, stdout = subprocess.PIPE,
76+
env=env).communicate()[0]
7777
return out
7878

7979
try:
80-
out = _minimal_ext_cmd(['svn', 'info'])
80+
out = _minimal_ext_cmd(['git', 'rev-parse', 'HEAD'])
81+
GIT_REVISION = out.strip().decode('ascii')
8182
except OSError:
82-
print(" --- Could not run svn info --- ")
83-
return ""
83+
GIT_REVISION = "Unknown"
8484

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

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

110-
FULLVERSION = VERSION
111-
if not ISRELEASED:
112-
FULLVERSION += '.dev'
113-
# If in git or something, bypass the svn rev
114-
if os.path.exists('.svn'):
115-
FULLVERSION += svn_version()
116-
11797
def write_version_py(filename='numpy/version.py'):
11898
cnt = """
11999
# THIS FILE IS GENERATED FROM NUMPY SETUP.PY
120-
short_version='%(version)s'
121-
version='%(version)s'
122-
release=%(isrelease)s
100+
short_version = '%(version)s'
101+
version = '%(version)s'
102+
full_version = '%(full_version)s'
103+
git_revision = '%(git_revision)s'
104+
release = %(isrelease)s
123105
124106
if not release:
125-
version += '.dev'
126-
import os
127-
svn_version_file = os.path.join(os.path.dirname(__file__),
128-
'core','__svn_version__.py')
129-
if os.path.isfile(svn_version_file):
130-
import imp
131-
svn = imp.load_module('numpy.core.__svn_version__',
132-
open(svn_version_file),
133-
svn_version_file,
134-
('.py','U',1))
135-
version += svn.version
107+
version = full_version
136108
"""
109+
FULL_VERSION = VERSION
110+
if not ISRELEASED:
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 = "Unknown"
118+
119+
FULL_VERSION += '.dev-' + GIT_REVISION[:7]
120+
137121
a = open(filename, 'w')
138122
try:
139-
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)})
140127
finally:
141128
a.close()
142129

0 commit comments

Comments
 (0)