1212import os
1313import sys
1414import subprocess
15-
15+ try :
16+ import configparser
17+ except ImportError :
18+ import ConfigParser as configparser
1619
1720if sys .version_info [:2 ] < (2 , 6 ) or (3 , 0 ) <= sys .version_info [0 :2 ] < (3 , 2 ):
1821 raise RuntimeError ("Python version 2.6, 2.7 or >= 3.2 required." )
4346"""
4447
4548# defaults
46- ISRELEASED = False
49+ ISRELEASED = True
4750# assume a version set by conda, next update with git,
4851# otherwise count on default
49- VERSION = '0.3.3'
52+ VERSION = 'Unknown'
53+
54+ class GitError (RuntimeError ):
55+ """Exception for git errors occuring in in git_version"""
56+ pass
5057
51- # Return the git revision as a string
58+ # Return the git version, revision and cycle
59+ #
60+ # Uses rev-parse to get the revision
61+ # tag to get the version number from the latest tag
62+ # and detects (approximate) revision cycles
5263def git_version (srcdir = None ):
5364 def _minimal_ext_cmd (cmd , srcdir ):
5465 # construct minimal environment
@@ -61,23 +72,33 @@ def _minimal_ext_cmd(cmd, srcdir):
6172 env ['LANGUAGE' ] = 'C'
6273 env ['LANG' ] = 'C'
6374 env ['LC_ALL' ] = 'C'
64- out = subprocess .Popen (
75+ proc = subprocess .Popen (
6576 cmd ,
6677 cwd = srcdir ,
6778 stdout = subprocess .PIPE ,
68- env = env ).communicate ()[0 ]
79+ stderr = subprocess .PIPE ,
80+ env = env )
81+ out , err = proc .communicate ()
82+ if proc .returncode :
83+ errmsg = err .decode ('ascii' ,errors = 'ignore' ).strip ()
84+ raise GitError ("git err; return code %d, error message:\n '%s'"
85+ % (proc .returncode , errmsg ))
6986 return out
7087
7188 try :
7289 GIT_VERSION = VERSION
7390 GIT_REVISION = 'Unknown'
74- CIT_CYCLE = 0
91+ GIT_CYCLE = 0
7592 out = _minimal_ext_cmd (['git' , 'rev-parse' , 'HEAD' ], srcdir )
7693 GIT_REVISION = out .strip ().decode ('ascii' )
7794 out = _minimal_ext_cmd (['git' , 'tag' ], srcdir )
7895 GIT_VERSION = out .strip ().decode ('ascii' ).split ('\n ' )[- 1 ][1 :]
79- out = _minimal_ext_cmd (['git' , 'describe' , '--tags' ], srcdir )
80- GIT_CYCLE = out .strip ().decode ('ascii' ).split ('-' )[1 ]
96+ out = _minimal_ext_cmd (['git' , 'describe' , '--tags' , '--long' ,'--always' ], srcdir )
97+ try :
98+ # don't get a good description with shallow clones, e.g., on Travis
99+ GIT_CYCLE = out .strip ().decode ('ascii' ).split ('-' )[1 ]
100+ except IndexError :
101+ pass
81102 except OSError :
82103 pass
83104
@@ -94,49 +115,60 @@ def _minimal_ext_cmd(cmd, srcdir):
94115# a lot more robust than what was previously being used.
95116builtins .__SLYCOT_SETUP__ = True
96117
118+ def rewrite_setup_cfg (version , gitrevision , release ):
119+ toreplace = dict (locals ())
120+ data = '' .join (open ('setup.cfg.in' , 'r' ).readlines ()).split ('@' )
121+ for k , v in toreplace .items ():
122+ idx = data .index (k )
123+ data [idx ] = v
124+ cfg = open ('setup.cfg' , 'w' )
125+ cfg .write ('' .join (data ))
126+ cfg .close ()
97127
98128def get_version_info (srcdir = None ):
99129 global ISRELEASED
130+ GIT_CYCLE = 0
100131
101132 # Adding the git rev number needs to be done inside write_version_py(),
102133 # otherwise the import of slycot.version messes up
103134 # the build under Python 3.
104135 if os .environ .get ('CONDA_BUILD' , False ):
105136 FULLVERSION = os .environ .get ('PKG_VERSION' , '???' )
106- GIT_REVISION = ''
107- GIT_CYCLE = 0
137+ GIT_REVISION = os .environ .get ('GIT_DESCRIBE_HASH' , '' )
108138 ISRELEASED = True
139+ rewrite_setup_cfg (FULLVERSION , GIT_REVISION , 'yes' )
109140 elif os .path .exists ('.git' ):
110- FULLVERSION , GIT_REVISION , GIT_CYCLE = git_version (srcdir )
111- elif os .path .exists ('slycot/version.py' ):
112- # must be a source distribution, use existing version file
113- try :
114- from slycot .version import git_revision as GIT_REVISION
115- except ImportError :
116- raise ImportError ("Unable to import git_revision. Try removing "
117- "slycot/version.py and the build directory "
118- "before building." )
141+ FULLVERSION , GIT_REVISION , GIT_CYCLE = git_version (srcdir )
142+ ISRELEASED = (GIT_CYCLE == 0 )
143+ rewrite_setup_cfg (FULLVERSION , GIT_REVISION ,
144+ (ISRELEASED and 'yes' ) or 'no' )
145+ elif os .path .exists ('setup.cfg' ):
146+ # valid distribution
147+ setupcfg = configparser .ConfigParser ()
148+ setupcfg .read ('setup.cfg' )
149+ FULLVERSION = setupcfg ['metadata' ].get ('version' , 'Unknown' )
150+ GIT_REVISION = setupcfg ['metadata' ].get ('gitrevision' , '' )
151+ return FULLVERSION , GIT_REVISION
119152 else :
120- FULLVERSION = VERSION
121- GIT_REVISION = "Unknown"
153+
154+ # try to find a version number from the dir name
155+ dname = os .getcwd ().split (os .sep )[- 1 ]
156+ import re
157+
158+ m = re .search (r'[0-9.]+' , dname )
159+ if m :
160+ FULLVERSION = m .group ()
161+ GIT_REVISION = ''
162+
163+ else :
164+ FULLVERSION = VERSION
165+ GIT_REVISION = "Unknown"
122166
123167 if not ISRELEASED :
124168 FULLVERSION += '.' + str (GIT_CYCLE )
125169
126170 return FULLVERSION , GIT_REVISION
127171
128- def configuration (parent_package = '' , top_path = None ):
129- from numpy .distutils .misc_util import Configuration
130- config = Configuration (None , parent_package , top_path )
131- config .set_options (ignore_setup_xxx_py = True ,
132- assume_default_configuration = True ,
133- delegate_options_to_subpackages = True ,
134- quiet = True )
135- config .add_subpackage ('slycot' )
136- config .get_version ('slycot/version.py' ) # sets config.version
137- return config
138-
139-
140172def check_submodules ():
141173 """ verify that the submodules are checked out and clean
142174 use `git submodule update --init`; on failure
@@ -158,8 +190,7 @@ def check_submodules():
158190 if line .startswith ('-' ) or line .startswith ('+' ):
159191 raise ValueError ('Submodule not clean: %s' % line )
160192
161- from distutils .command .sdist import sdist
162-
193+ from skbuild .command .sdist import sdist
163194
164195class sdist_checked (sdist ):
165196 """ check submodules on sdist to prevent incomplete tarballs """
@@ -188,7 +219,7 @@ def setup_package():
188219 long_description = "\n " .join (DOCLINES [2 :]),
189220 url = 'https://github.com/python-control/Slycot' ,
190221 author = 'Enrico Avventi et al.' ,
191- license = 'GPLv2 ' ,
222+ license = 'GPL-2.0 ' ,
192223 classifiers = [_f for _f in CLASSIFIERS .split ('\n ' ) if _f ],
193224 platforms = ["Windows" , "Linux" , "Solaris" , "Mac OS-X" , "Unix" ],
194225 cmdclass = {"sdist" : sdist_checked },
0 commit comments