-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
144 lines (125 loc) · 5.24 KB
/
setup.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""
Setup script for quantiphyse-asl
"""
import os
import subprocess
import re
import io
import glob
import sys
from setuptools import setup
from setuptools import find_packages
from setuptools.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
import numpy
MODULE = 'quantiphyse_t1'
def get_filetext(rootdir, filename):
""" Get the text of a local file """
with io.open(os.path.join(rootdir, filename), encoding='utf-8') as f:
return f.read()
def git_version():
""" Get the full and python standardized version from Git tags (if possible) """
try:
# Full version includes the Git commit hash
full_version = subprocess.check_output('git describe --dirty', shell=True).decode("utf-8").strip(" \n")
# Python standardized version in form major.minor.patch.post<build>
version_regex = re.compile(r"v?(\d+\.\d+(\.\d+)?(-\d+)?).*")
match = version_regex.match(full_version)
if match:
std_version = match.group(1).replace("-", ".post")
else:
raise RuntimeError("Failed to parse version string %s" % full_version)
return full_version, std_version
except:
# Any failure, return None. We may not be in a Git repo at all
return None, None
def git_timestamp():
""" Get the last commit timestamp from Git (if possible)"""
try:
return subprocess.check_output('git log -1 --format=%cd', shell=True).decode("utf-8").strip(" \n")
except:
# Any failure, return None. We may not be in a Git repo at all
return None
def update_metadata(rootdir, version_str, timestamp_str):
""" Update the version and timestamp metadata in the module _version.py file """
with io.open(os.path.join(rootdir, MODULE, "_version.py"), "w", encoding='utf-8') as f:
f.write("__version__ = '%s'\n" % version_str)
f.write("__timestamp__ = '%s'\n" % timestamp_str)
def get_requirements(rootdir):
""" Get a list of all entries in the requirements file """
with io.open(os.path.join(rootdir, 'requirements.txt'), encoding='utf-8') as f:
return [l.strip() for l in f.readlines()]
def get_version(rootdir):
""" Get the current version number (and update it in the module _version.py file if necessary)"""
version, timestamp = git_version()[1], git_timestamp()
if version is not None and timestamp is not None:
# We got the metadata from Git - update the version file
update_metadata(rootdir, version, timestamp)
else:
# Could not get metadata from Git - use the version file if it exists
with io.open(os.path.join(rootdir, MODULE, '_version.py'), encoding='utf-8') as f:
md = f.read()
match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", md, re.M)
if match:
version = match.group(1)
else:
version = "unknown"
return version
def get_package_data(rootdir):
"""
Get extra data files to install into the package, e.g. icons
"""
return {
MODULE : glob.glob("%s/*.png" % MODULE) + glob.glob("%s/*.svg" % MODULE)
}
def get_extensions(rootdir):
extensions = []
compile_args = []
link_args = []
if sys.platform.startswith('win'):
compile_args.append('/EHsc')
elif sys.platform.startswith('darwin'):
compile_args += ["-mmacosx-version-min=10.9"]
link_args += ["-stdlib=libc++", "-mmacosx-version-min=10.9"]
# T1 map generation extension
extensions.append(Extension("%s.t1_model" % MODULE,
sources=['%s/t1_model.pyx' % MODULE,
'%s/src/linear_regression.cpp' % MODULE,
'%s/src/T10_calculation.cpp' % MODULE],
include_dirs=['%s/src/' % MODULE,
numpy.get_include()],
language="c++", extra_compile_args=compile_args, extra_link_args=link_args))
return cythonize(extensions)
module_dir = os.path.abspath(os.path.dirname(__file__))
kwargs = {
'name' : 'quantiphyse-t1',
'version' : get_version(module_dir),
'description' : 'Quantiphyse plugin for Bayesian T1 mapping',
'long_description' : get_filetext(module_dir, 'README.md'),
'long_description_content_type' : 'text/markdown',
'url' : 'https://quantiphyse.readthedocs.io/',
'author' : 'Martin Craig',
'author_email' : '[email protected]',
'license' : 'Apache-2.0',
'setup_requires' : ['cython'],
'install_requires' : get_requirements(module_dir),
'packages' : find_packages(),
'package_data' : get_package_data(module_dir),
'ext_modules' : get_extensions(module_dir),
'include_package_data' : True,
'entry_points' : {
'quantiphyse_plugins' : [
'%s = %s:QP_MANIFEST' % (MODULE, MODULE),
],
},
'classifiers' : [
'Development Status :: 3 - Alpha',
'Intended Audience :: Science/Research',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Topic :: Scientific/Engineering :: Bio-Informatics',
'License :: OSI Approved :: Apache Software License',
],
}
setup(**kwargs)