22from __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
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
2937def _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+
8391version = '{verstr}'
8492
8593major = {major}
8694minor = {minor}
8795bugfix = {bugfix}
8896
8997release = {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+
0 commit comments