Browse code

further reduce boilerplate in setup.py

Revert to a style in 'version.py' that uses module globals
to report information to the outside world. Doing this means
less "plumbing" when using the module.

Joseph Weston authored on 21/02/2018 16:04:32
Showing 2 changed files
... ...
@@ -12,23 +12,12 @@ Version = namedtuple('Version', ('release', 'dev', 'labels'))
12 12
 __all__ = []
13 13
 
14 14
 package_root = os.path.dirname(os.path.realpath(__file__))
15
+package_name = os.path.basename(package_root)
15 16
 distr_root = os.path.dirname(package_root)
16 17
 
17 18
 STATIC_VERSION_FILE = '_static_version.py'
18 19
 
19 20
 
20
-def write_version(fname, version):
21
-    # This could be a hard link, so try to delete it first.  Is there any way
22
-    # to do this atomically together with opening?
23
-    try:
24
-        os.remove(fname)
25
-    except OSError:
26
-        pass
27
-    with open(fname, 'w') as f:
28
-        f.write("# This file has been created by setup.py.\n"
29
-                "version = '{}'\n".format(version))
30
-
31
-
32 21
 def get_version(version_file=STATIC_VERSION_FILE):
33 22
     version_info = {}
34 23
     with open(os.path.join(package_root, version_file), 'rb') as f:
... ...
@@ -140,20 +129,38 @@ def get_version_from_git_archive(version_info):
140 129
         return Version('unknown', dev=None, labels=[f'g{git_hash}'])
141 130
 
142 131
 
143
-def cmdclass(version, package_name):
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
+
144 157
 
145
-    class build(build_orig):
146
-        def run(self):
147
-            super().run()
148
-            write_version(os.path.join(self.build_lib, package_name,
149
-                                       STATIC_VERSION_FILE),
150
-                          version=version)
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)
151 164
 
152
-    class sdist(sdist_orig):
153
-        def make_release_tree(self, base_dir, files):
154
-            super().make_release_tree(base_dir, files)
155
-            write_version(os.path.join(base_dir, package_name,
156
-                                       STATIC_VERSION_FILE),
157
-                          version=version)
158 165
 
159
-    return dict(sdist=sdist, build=build)
166
+cmdclass = dict(sdist=_sdist, build=_build)
... ...
@@ -1,7 +1,6 @@
1 1
 #!/usr/bin/env python3
2 2
 # -*- coding: utf-8 -*-
3 3
 
4
-import os
5 4
 from setuptools import setup, find_packages
6 5
 import sys
7 6
 
... ...
@@ -11,19 +10,19 @@ if sys.version_info < (3, 6):
11 10
     sys.exit(1)
12 11
 
13 12
 
14
-# Load version.py module without importing 'adaptive'
15
-def load_version_module(package_name):
13
+# Loads version.py module without importing the whole package.
14
+def get_version_and_cmdclass(package_name):
15
+    import os
16 16
     from importlib.util import module_from_spec, spec_from_file_location
17 17
     spec = spec_from_file_location('version',
18
-                                   '{}/version.py'.format(package_name))
18
+                                   os.path.join(package_name, 'version.py'))
19 19
     module = module_from_spec(spec)
20 20
     spec.loader.exec_module(module)
21
-    return module
21
+    return module.version, module.cmdclass
22 22
 
23 23
 
24
-version_module = load_version_module('adaptive')
25
-version = version_module.get_version()
26
-cmdclass = version_module.cmdclass(version, 'adaptive')
24
+version, cmdclass = get_version_and_cmdclass('adaptive')
25
+
27 26
 
28 27
 install_requires = [
29 28
     'scipy',