Browse code

Merge branch 'simplify_version'

reduce the amount of version related code in setup.py

See merge request qt/adaptive!50

Joseph Weston authored on 21/02/2018 16:46:29
Showing 2 changed files
... ...
@@ -1,7 +1,10 @@
1
-import sys
2
-import subprocess
3 1
 from collections import namedtuple
4 2
 import os
3
+import subprocess
4
+import sys
5
+
6
+from distutils.command.build import build as build_orig
7
+from setuptools.command.sdist import sdist as sdist_orig
5 8
 
6 9
 Version = namedtuple('Version', ('release', 'dev', 'labels'))
7 10
 
... ...
@@ -9,11 +12,11 @@ Version = namedtuple('Version', ('release', 'dev', 'labels'))
9 12
 __all__ = []
10 13
 
11 14
 package_root = os.path.dirname(os.path.realpath(__file__))
15
+package_name = os.path.basename(package_root)
12 16
 distr_root = os.path.dirname(package_root)
13 17
 
14 18
 STATIC_VERSION_FILE = '_static_version.py'
15 19
 
16
-version = None
17 20
 
18 21
 def get_version(version_file=STATIC_VERSION_FILE):
19 22
     version_info = {}
... ...
@@ -127,3 +130,37 @@ def get_version_from_git_archive(version_info):
127 130
 
128 131
 
129 132
 version = get_version()
133
+
134
+# The following section defines a module global 'cmdclass',
135
+# which can be used from setup.py. The 'package_name' and
136
+# 'version' module globals are used (but not modified).
137
+
138
+def _write_version(fname):
139
+    # This could be a hard link, so try to delete it first.  Is there any way
140
+    # to do this atomically together with opening?
141
+    try:
142
+        os.remove(fname)
143
+    except OSError:
144
+        pass
145
+    with open(fname, 'w') as f:
146
+        f.write("# This file has been created by setup.py.\n"
147
+                "version = '{}'\n".format(version))
148
+
149
+
150
+class _build(build_orig):
151
+    def run(self):
152
+        super().run()
153
+        _write_version(os.path.join(self.build_lib, package_name,
154
+                                    STATIC_VERSION_FILE),
155
+                      version=version)
156
+
157
+
158
+class _sdist(sdist_orig):
159
+    def make_release_tree(self, base_dir, files):
160
+        super().make_release_tree(base_dir, files)
161
+        _write_version(os.path.join(base_dir, package_name,
162
+                                    STATIC_VERSION_FILE),
163
+                       version=version)
164
+
165
+
166
+cmdclass = dict(sdist=_sdist, build=_build)
... ...
@@ -1,13 +1,8 @@
1 1
 #!/usr/bin/env python3
2 2
 # -*- coding: utf-8 -*-
3 3
 
4
-import os
5
-import sys
6
-import importlib
7
-from importlib.util import module_from_spec, spec_from_file_location
8 4
 from setuptools import setup, find_packages
9
-from setuptools.command.sdist import sdist as sdist_orig
10
-from distutils.command.build import build as build_orig
5
+import sys
11 6
 
12 7
 
13 8
 if sys.version_info < (3, 6):
... ...
@@ -15,43 +10,18 @@ if sys.version_info < (3, 6):
15 10
     sys.exit(1)
16 11
 
17 12
 
18
-package_name = 'adaptive'
19
-
20
-# Load version.py module without importing 'adaptive'
21
-def load_version_module():
13
+# Loads version.py module without importing the whole package.
14
+def get_version_and_cmdclass(package_name):
15
+    import os
16
+    from importlib.util import module_from_spec, spec_from_file_location
22 17
     spec = spec_from_file_location('version',
23
-                                   '{}/version.py'.format(package_name))
18
+                                   os.path.join(package_name, 'version.py'))
24 19
     module = module_from_spec(spec)
25 20
     spec.loader.exec_module(module)
26
-    return module
27
-
28
-version = load_version_module()
29
-
30
-
31
-def write_version(fname):
32
-    # This could be a hard link, so try to delete it first.  Is there any way
33
-    # to do this atomically together with opening?
34
-    try:
35
-        os.remove(fname)
36
-    except OSError:
37
-        pass
38
-    with open(fname, 'w') as f:
39
-        f.write("# This file has been created by setup.py.\n"
40
-                "version = '{}'\n".format(version.version))
41
-
42
-
43
-class build(build_orig):
44
-    def run(self):
45
-        super().run()
46
-        write_version(os.path.join(self.build_lib, package_name,
47
-                                   version.STATIC_VERSION_FILE))
21
+    return module.version, module.cmdclass
48 22
 
49 23
 
50
-class sdist(sdist_orig):
51
-    def make_release_tree(self, base_dir, files):
52
-        super().make_release_tree(base_dir, files)
53
-        write_version(os.path.join(base_dir, package_name,
54
-                                   version.STATIC_VERSION_FILE))
24
+version, cmdclass = get_version_and_cmdclass('adaptive')
55 25
 
56 26
 
57 27
 install_requires = [
... ...
@@ -75,7 +45,7 @@ extras_require = {
75 45
 setup(
76 46
     name='adaptive',
77 47
     description='Adaptive parallel sampling of mathematical functions',
78
-    version=version.version,
48
+    version=version,
79 49
     url='https://gitlab.kwant-project.org/qt/adaptive',
80 50
     author='Adaptive authors',
81 51
     license='BSD',
... ...
@@ -88,5 +58,5 @@ setup(
88 58
     packages=find_packages('.'),
89 59
     install_requires=install_requires,
90 60
     extras_require=extras_require,
91
-    cmdclass=dict(sdist=sdist, build=build),
61
+    cmdclass=cmdclass,
92 62
 )