Browse code

move version related functions and classes from setup.py to version.py

Bas Nijholt authored on 21/02/2018 13:56:08
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
 
... ...
@@ -15,6 +18,21 @@ STATIC_VERSION_FILE = '_static_version.py'
15 18
 
16 19
 version = None
17 20
 
21
+package_name = 'adaptive'
22
+
23
+
24
+def write_version(fname, version):
25
+    # This could be a hard link, so try to delete it first.  Is there any way
26
+    # to do this atomically together with opening?
27
+    try:
28
+        os.remove(fname)
29
+    except OSError:
30
+        pass
31
+    with open(fname, 'w') as f:
32
+        f.write("# This file has been created by setup.py.\n"
33
+                "version = '{}'\n".format(version))
34
+
35
+
18 36
 def get_version(version_file=STATIC_VERSION_FILE):
19 37
     version_info = {}
20 38
     with open(os.path.join(package_root, version_file), 'rb') as f:
... ...
@@ -126,4 +144,22 @@ def get_version_from_git_archive(version_info):
126 144
         return Version('unknown', dev=None, labels=[f'g{git_hash}'])
127 145
 
128 146
 
147
+def cmdclass(version, package_name):
148
+
149
+    class build(build_orig):
150
+        def run(self):
151
+            super().run()
152
+            write_version(os.path.join(self.build_lib, package_name,
153
+                                       STATIC_VERSION_FILE),
154
+                          version=version)
155
+
156
+    class sdist(sdist_orig):
157
+        def make_release_tree(self, base_dir, files):
158
+            super().make_release_tree(base_dir, files)
159
+            write_version(os.path.join(base_dir, package_name,
160
+                                       STATIC_VERSION_FILE),
161
+                          version=version)
162
+
163
+    return dict(sdist=sdist, build=build)
164
+
129 165
 version = get_version()
... ...
@@ -2,12 +2,8 @@
2 2
 # -*- coding: utf-8 -*-
3 3
 
4 4
 import os
5
-import sys
6
-import importlib
7
-from importlib.util import module_from_spec, spec_from_file_location
8 5
 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
6
+import sys
11 7
 
12 8
 
13 9
 if sys.version_info < (3, 6):
... ...
@@ -15,44 +11,18 @@ if sys.version_info < (3, 6):
15 11
     sys.exit(1)
16 12
 
17 13
 
18
-package_name = 'adaptive'
19
-
20 14
 # Load version.py module without importing 'adaptive'
21
-def load_version_module():
15
+def load_version_module(package_name):
16
+    from importlib.util import module_from_spec, spec_from_file_location
22 17
     spec = spec_from_file_location('version',
23 18
                                    '{}/version.py'.format(package_name))
24 19
     module = module_from_spec(spec)
25 20
     spec.loader.exec_module(module)
26 21
     return module
27 22
 
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))
48
-
49
-
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))
55 23
 
24
+version = load_version_module('adaptive')
25
+cmdclass = version.cmdclass(version.version, 'adaptive')
56 26
 
57 27
 install_requires = [
58 28
     'scipy',
... ...
@@ -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
 )