Skip to content

Commit 12c5591

Browse files
committed
Merge pull request #12 from iguananaut/fix-setup-version
Fix setup version. Fixes #8.
2 parents 1d0b4aa + 5f3e0d9 commit 12c5591

4 files changed

Lines changed: 82 additions & 48 deletions

File tree

‎.gitignore‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#compiled files
1+
# Compiled files
22
*.py[co]
33
*.a
44
*.o
@@ -8,7 +8,10 @@
88
# add a non-generated .c extension, use `git add -f filename.c`.
99
*.c
1010

11-
#sphinx
11+
# Other generated files
12+
astropy/version.py
13+
14+
# Sphinx
1215
_build
1316

1417
# Packages/installer info
@@ -24,5 +27,5 @@ sdist
2427
develop-eggs
2528
.installed.cfg
2629

27-
# other
30+
# Other
2831
.*.swp

‎astropy/__init__.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env python
22
from __future__ import division
3-
from astropy.version import version as __version__
3+
try:
4+
from astropy.version import version as __version__
5+
except ImportError:
6+
# TODO: Issue a warning...
7+
__version__ = ''
48
# The version number can be found in the "version" variable of version.py
59

610
"""_Astropy is a package intended to contain core functionality and some

‎astropy/version_helper.py‎

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
from __future__ import division
33

44
"""
5-
Version numbering for AstroPy. The `major`, `minor`, and `bugfix` variables
6-
hold the respective parts of the version number (bugfix is '0' if absent). The
7-
`release` variable is True if this is a release, and False if this is a
8-
development version of astropy. For the actual version string, use::
5+
Utilities for generating the version string for AstroPy and the version.py
6+
module, which contains version info for the package.
7+
8+
Within the generated astropy.version module, the `major`, `minor`, and `bugfix`
9+
variables hold the respective parts of the version number (bugfix is '0' if
10+
absent). The `release` variable is True if this is a release, and False if this
11+
is a development version of astropy. For the actual version string, use::
912
1013
from astropy.version import version
1114
@@ -15,22 +18,26 @@
1518
1619
"""
1720

18-
version = '0.0dev'
1921

20-
_versplit = version.replace('dev', '').split('.')
21-
major = int(_versplit[0])
22-
minor = int(_versplit[1])
23-
bugfix = 0 if len(_versplit) < 3 else int(_versplit[2])
24-
del _versplit
22+
def _version_split(version):
23+
"""
24+
Split a version string into major, minor, and bugfix numbers (with bugfix
25+
optional, defaulting to 0).
26+
"""
2527

26-
release = not version.endswith('dev')
28+
if '-r' in version:
29+
version = version.split('-r', 1)[0]
30+
versplit = version.replace('dev', '').split('.')
31+
major = int(versplit[0])
32+
minor = int(versplit[1])
33+
bugfix = 0 if len(versplit) < 3 else int(versplit[2])
34+
return major, minor, bugfix
2735

2836

2937
def _get_git_devstr(sha=False):
3038
"""
3139
Determines the number of revisions in this repository.
3240
33-
These
3441
3542
Parameters
3643
----------
@@ -50,9 +57,6 @@ def _get_git_devstr(sha=False):
5057
from subprocess import Popen, PIPE
5158
from warnings import warn
5259

53-
if release:
54-
raise ValueError('revsion devstring should not be used in a ' + \
55-
'release version')
5660

5761
currdir = os.path.abspath(os.path.split(__file__)[0])
5862

@@ -73,31 +77,68 @@ def _get_git_devstr(sha=False):
7377
nrev = stdout.count('\n')
7478
return '-r%i' % nrev
7579

76-
if not release:
77-
version = version + _get_git_devstr(False)
7880

7981
# This is used by setup.py to create a new version.py - see that file for
8082
# details
8183
_frozen_version_py_template = """
8284
# Autogenerated by astropy's setup.py on {timestamp}
85+
86+
try:
87+
import pkg_resources
88+
except ImportError:
89+
pkg_resources = None
90+
8391
version = '{verstr}'
8492
8593
major = {major}
8694
minor = {minor}
8795
bugfix = {bugfix}
8896
8997
release = {rel}
98+
99+
100+
if pkg_resources:
101+
dist = pkg_resources.get_distribution('AstroPy')
102+
if dist.precedence == pkg_resources.DEVELOP_DIST:
103+
from astropy.version_helper import _get_git_devstr
104+
if '-r' in version:
105+
version_base = version.split('-r', 1)[0]
106+
try:
107+
version = version_base + _get_git_devstr()
108+
except OSError:
109+
pass
90110
"""[1:]
91111

92112

93-
def _get_version_py_str():
113+
def _get_version_py_str(version, release):
94114

95115
import datetime
96116

97117
timestamp = str(datetime.datetime.now())
118+
major, minor, bugfix = _version_split(version)
98119
return _frozen_version_py_template.format(timestamp=timestamp,
99120
verstr=version,
100121
major=major,
101122
minor=minor,
102123
bugfix=bugfix,
103124
rel=release)
125+
126+
127+
def _generate_version_py(version, release):
128+
"""Regenerate the version.py module if necessary."""
129+
130+
import os
131+
132+
try:
133+
from astropy.version import version as current_version
134+
except ImportError:
135+
current_version = None
136+
137+
version_py = os.path.join('astropy', 'version.py')
138+
139+
if current_version != version:
140+
print 'Freezing version number to', version_py
141+
142+
with open(version_py, 'w') as f: # This overwrites the actual version.py
143+
f.write(_get_version_py_str(version, release))
144+

‎setup.py‎

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
import os
88
import glob
99
from setuptools import setup, find_packages, Extension
10-
from distutils.command.build_py import build_py as du_build_py
1110
from warnings import warn
1211

1312
import astropy
14-
from astropy.version import version as version_string
15-
from astropy.version import release
13+
from astropy.version_helper import _get_git_devstr, _generate_version_py
14+
15+
16+
VERSION = '0.0dev'
17+
RELEASE = not VERSION.endswith('dev')
18+
19+
if not RELEASE:
20+
VERSION += _get_git_devstr(False)
21+
_generate_version_py(VERSION, RELEASE)
22+
1623

1724
# Use the find_packages tool to locate all packages and modules other than
1825
# those that are in tests/
@@ -47,7 +54,7 @@
4754
extmod = modbase + '.' + fn[:-4] # Package must match file name
4855
pyxfiles.append((extmod, fullfn))
4956

50-
if not release and have_cython:
57+
if not RELEASE and have_cython:
5158

5259
from Cython.Distutils import build_ext as cython_build_ext
5360
cmdclassd['build_ext'] = cython_build_ext
@@ -96,31 +103,10 @@ def finalize_options(self):
96103
pass
97104

98105

99-
# This customize build_py command replaces the source version.py with
100-
# one that has a fixed version number. This is necessary to support version
101-
# numbers pulled from the git repository.
102-
103-
class astropy_build_py(du_build_py):
104-
105-
def run(self):
106-
107-
from astropy.version_helper import _get_version_py_str
108-
109-
res = du_build_py.run(self)
110-
111-
versfile = os.path.join(self.build_lib, 'astropy', 'version.py')
112-
113-
print 'Freezing version number to', versfile
114-
115-
with open(versfile, 'w') as f: # This overwrites the actual version.py
116-
f.write(_get_version_py_str())
117-
118-
return res
119106

120-
cmdclassd['build_py'] = astropy_build_py
121107

122108
setup(name='AstroPy',
123-
version=version_string,
109+
version=VERSION,
124110
description='Community-developed python astronomy tools',
125111
packages=packages,
126112
package_data={'astropy': ['data/*']},

0 commit comments

Comments
 (0)