88
99import builtins
1010import os
11- import sys
1211import subprocess
13- import re
14- import platform
15- try :
16- import configparser
17- except ImportError :
18- import ConfigParser as configparser
1912
2013try :
2114 from skbuild import setup
2215 from skbuild .command .sdist import sdist
2316except ImportError :
2417 raise ImportError ('scikit-build must be installed before running setup.py' )
2518
26- DOCLINES = __doc__ .split ("\n " )
27-
28- CLASSIFIERS = """\
29- Development Status :: 4 - Beta
30- Intended Audience :: Science/Research
31- Intended Audience :: Developers
32- License :: OSI Approved
33- License :: OSI Approved :: GNU General Public License v2 (GPLv2)
34- Programming Language :: C
35- Programming Language :: Fortran
36- Programming Language :: Python
37- Programming Language :: Python :: 3.7
38- Programming Language :: Python :: 3.8
39- Programming Language :: Python :: 3.9
40- Programming Language :: Python :: 3.10
41- Topic :: Software Development
42- Topic :: Scientific/Engineering
43- Operating System :: Microsoft :: Windows
44- Operating System :: POSIX
45- Operating System :: Unix
46- Operating System :: MacOS
47- """
48-
49- # defaults
50- ISRELEASED = True
51- # assume a version set by conda, next update with git,
52- # otherwise count on default
53- VERSION = 'Unknown'
54-
55-
56- class GitError (RuntimeError ):
57- """Exception for git errors occuring in in git_version"""
58- pass
59-
60-
61- def git_version (srcdir = None ):
62- """Return the git version, revision and cycle
63-
64- Uses rev-parse to get the revision tag to get the version number from the
65- latest tag and detects (approximate) revision cycles
66-
67- """
68- def _minimal_ext_cmd (cmd , srcdir ):
69- # construct minimal environment
70- env = {}
71- for k in ['SYSTEMROOT' , 'PATH' ]:
72- v = os .environ .get (k )
73- if v is not None :
74- env [k ] = v
75- # LANGUAGE is used on win32
76- env ['LANGUAGE' ] = 'C'
77- env ['LANG' ] = 'C'
78- env ['LC_ALL' ] = 'C'
79- proc = subprocess .Popen (
80- cmd ,
81- cwd = srcdir ,
82- stdout = subprocess .PIPE ,
83- stderr = subprocess .PIPE ,
84- env = env )
85- out , err = proc .communicate ()
86- if proc .returncode :
87- errmsg = err .decode ('ascii' , errors = 'ignore' ).strip ()
88- raise GitError ("git err; return code %d, error message:\n '%s'"
89- % (proc .returncode , errmsg ))
90- return out
91-
92- try :
93- GIT_VERSION = VERSION
94- GIT_REVISION = 'Unknown'
95- GIT_CYCLE = 0
96- out = _minimal_ext_cmd (['git' , 'rev-parse' , 'HEAD' ], srcdir )
97- GIT_REVISION = out .strip ().decode ('ascii' )
98- out = _minimal_ext_cmd (['git' , 'tag' ], srcdir )
99- GIT_VERSION = out .strip ().decode ('ascii' ).split ('\n ' )[- 1 ][1 :]
100- out = _minimal_ext_cmd (['git' , 'describe' , '--tags' ,
101- '--long' , '--always' ], srcdir )
102- try :
103- # don't get a good description with shallow clones
104- GIT_CYCLE = out .strip ().decode ('ascii' ).split ('-' )[1 ]
105- except IndexError :
106- pass
107- except OSError :
108- pass
109-
110- return GIT_VERSION , GIT_REVISION , GIT_CYCLE
111-
112- # BEFORE importing distutils, remove MANIFEST. distutils doesn't properly
113- # update it when the contents of directories change.
114- if os .path .exists ('MANIFEST' ):
115- os .remove ('MANIFEST' )
19+ try :
20+ from setuptools_scm import get_version
21+ except ImportError :
22+ raise ImportError ('setuptools_scm must be installed before running setup.py' )
11623
11724# This is a bit hackish: we are setting a global variable so that the main
11825# slycot __init__ can detect if it is being loaded by the setup routine, to
@@ -121,69 +28,6 @@ def _minimal_ext_cmd(cmd, srcdir):
12128builtins .__SLYCOT_SETUP__ = True
12229
12330
124- def rewrite_setup_cfg (version , gitrevision , release ):
125- toreplace = dict (locals ())
126- data = '' .join (open ('setup.cfg.in' , 'r' ).readlines ()).split ('@' )
127- for k , v in toreplace .items ():
128- idx = data .index (k )
129- data [idx ] = v
130- cfg = open ('setup.cfg' , 'w' )
131- cfg .write ('' .join (data ))
132- cfg .close ()
133-
134-
135- def get_version_info (srcdir = None ):
136- global ISRELEASED
137- GIT_CYCLE = 0
138-
139- # Adding the git rev number needs to be done inside write_version_py(),
140- # otherwise the import of slycot.version messes up
141- # the build under Python 3.
142- if os .environ .get ('CONDA_BUILD' , False ):
143- FULLVERSION = os .environ .get ('PKG_VERSION' , '???' )
144- GIT_REVISION = os .environ .get ('GIT_DESCRIBE_HASH' , '' )
145- ISRELEASED = True
146- rewrite_setup_cfg (FULLVERSION , GIT_REVISION , 'yes' )
147- elif os .path .exists ('.git' ):
148- FULLVERSION , GIT_REVISION , GIT_CYCLE = git_version (srcdir )
149- ISRELEASED = (GIT_CYCLE == 0 )
150- rewrite_setup_cfg (FULLVERSION , GIT_REVISION ,
151- (ISRELEASED and 'yes' ) or 'no' )
152- elif os .path .exists ('setup.cfg' ):
153- # valid distribution
154- setupcfg = configparser .ConfigParser (allow_no_value = True )
155- setupcfg .read ('setup.cfg' )
156-
157- FULLVERSION = setupcfg .get (section = 'metadata' , option = 'version' )
158-
159- if FULLVERSION is None :
160- FULLVERSION = "Unknown"
161-
162- GIT_REVISION = setupcfg .get (section = 'metadata' , option = 'gitrevision' )
163-
164- if GIT_REVISION is None :
165- GIT_REVISION = ""
166-
167- return FULLVERSION , GIT_REVISION
168- else :
169-
170- # try to find a version number from the dir name
171- dname = os .getcwd ().split (os .sep )[- 1 ]
172-
173- m = re .search (r'[0-9.]+' , dname )
174- if m :
175- FULLVERSION = m .group ()
176- GIT_REVISION = ''
177-
178- else :
179- FULLVERSION = VERSION
180- GIT_REVISION = "Unknown"
181-
182- if not ISRELEASED :
183- FULLVERSION += '.' + str (GIT_CYCLE )
184-
185- return FULLVERSION , GIT_REVISION
186-
18731def check_submodules ():
18832 """ verify that the submodules are checked out and clean
18933 use `git submodule update --init`; on failure
@@ -212,43 +56,11 @@ def run(self):
21256 check_submodules ()
21357 sdist .run (self )
21458
215- def setup_package ():
216- src_path = os .path .dirname (os .path .abspath (__file__ ))
217- sys .path .insert (0 , src_path )
218-
219- # Rewrite the version file everytime
220- VERSION , gitrevision = get_version_info (src_path )
221-
222- metadata = dict (
223- name = 'slycot' ,
224- packages = ['slycot' , 'slycot.tests' ],
225- cmake_languages = ('C' , 'Fortran' ),
226- version = VERSION ,
227- maintainer = "Slycot developers" ,
228- maintainer_email = "[email protected] " ,
229- description = DOCLINES [0 ],
230- long_description = open ('README.rst' ).read (),
231- url = 'https://github.com/python-control/Slycot' ,
232- author = 'Enrico Avventi et al.' ,
233- license = 'GPL-2.0' ,
234- classifiers = [_f for _f in CLASSIFIERS .split ('\n ' ) if _f ],
235- platforms = ["Windows" , "Linux" , "Solaris" , "Mac OS-X" , "Unix" ],
236- cmdclass = {"sdist" : sdist_checked },
237- cmake_args = ['-DSLYCOT_VERSION:STRING=' + VERSION ,
238- '-DGIT_REVISION:STRING=' + gitrevision ,
239- '-DISRELEASE:STRING=' + str (ISRELEASED ),
240- '-DFULL_VERSION=' + VERSION + '.git' + gitrevision [:7 ]],
241- zip_safe = False ,
242- install_requires = ['numpy' ],
243- python_requires = ">=3.7"
244- )
245-
246- try :
247- setup (** metadata )
248- finally :
249- del sys .path [0 ]
250- return
251-
252-
253- if __name__ == '__main__' :
254- setup_package ()
59+ # These need to stay in setup.py
60+ # https://scikit-build.readthedocs.io/en/latest/usage.html#setuptools-options
61+ setup (
62+ packages = ['slycot' , 'slycot.tests' ],
63+ cmdclass = {'sdist' : sdist_checked },
64+ cmake_languages = ('C' , 'Fortran' ),
65+ use_scm_version = True ,
66+ )
0 commit comments