-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
141 lines (129 loc) · 4.79 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
# Released under Apache 2.0; refer to LICENSE.txt
import os
import re
import subprocess
# If some modules are not found, we use others, so no need to warn:
try:
from setuptools import setup
from setuptools import Extension
from setuptools.command.build_py import build_py
from setuptools.command.sdist import sdist
except ImportError:
from distutils.cmd import Command
from distutils.command.build_py import build_py
from distutils.command.sdist import sdist
from distutils.core import Extension
from distutils.core import setup
def get_version():
# The .git directory does not exist in the sdist, so read VERSION.
if not os.path.exists('.git'):
with open('VERSION', 'r') as f:
version = f.read().strip()
return version, version
# git describe a commit using the most recent tag reachable from it.
# Release tags start with v* (XXX what about other tags starting with v?)
# and are of the form `v1.1.2`.
#
# The output `desc` will be of the form v1.1.2-2-gb92bef6[-dirty]:
# - verpart v1.1.2
# - revpart 2
# - localpart gb92bef6[-dirty]
try:
desc = subprocess.check_output([
'git', 'describe', '--dirty', '--long', '--match', 'v*',
])
except subprocess.CalledProcessError:
return '0.0', '0.0'
match = re.match(r'^v([^-]*)-([0-9]+)-(.*)$', desc.decode('utf-8'))
assert match is not None
verpart, revpart, localpart = match.groups()
# Create a post version.
if revpart > '0' or 'dirty' in localpart:
# Local part may be g0123abcd or g0123abcd-dirty.
# Hyphens not kosher here, so replace by dots.
localpart = localpart.replace('-', '.')
full_version = '%s.post%s+%s' % (verpart, revpart, localpart)
# Create a release version.
else:
full_version = verpart
# Strip the local part if there is one, to appease pkg_resources,
# which handles only PEP 386, not PEP 440.
if '+' in full_version:
pkg_version = full_version[:full_version.find('+')]
else:
pkg_version = full_version
# Sanity-check the result. XXX Consider checking the full PEP 386
# and PEP 440 regular expressions here?
assert '-' not in full_version, '%r' % (full_version,)
assert '-' not in pkg_version, '%r' % (pkg_version,)
assert '+' not in pkg_version, '%r' % (pkg_version,)
return pkg_version, full_version
PKG_VERSION, FULL_VERSION = get_version()
VERSION_PY = 'src/version.py'
def write_version_py(path):
try:
with open(path, 'rb') as f:
version_old = f.read()
except IOError:
version_old = None
version_new = '__version__ = %r\n' % (FULL_VERSION,)
if version_old != version_new:
print('writing %s' % (path,))
with open(path, 'w') as f:
f.write(version_new)
with open('VERSION', 'w') as f:
f.write('%s\n' % (PKG_VERSION,))
class local_build_py(build_py):
def run(self):
write_version_py(VERSION_PY)
build_py.run(self)
# Make sure the VERSION file in the sdist is exactly specified, even
# if it is a development version, so that we do not need to run git to
# discover it -- which won't work because there's no .git directory in
# the sdist.
class local_sdist(sdist):
def make_release_tree(self, base_dir, files):
sdist.make_release_tree(self, base_dir, files)
version_file = os.path.join(base_dir, 'VERSION')
print('updating %s' % (version_file,))
# Write to temporary file first and rename over permanent not
# just to avoid atomicity issues (not likely an issue since if
# interrupted the whole sdist directory is only partially
# written) but because the upstream sdist may have made a hard
# link, so overwriting in place will edit the source tree.
with open(version_file + '.tmp', 'w') as f:
f.write('%s\n' % (PKG_VERSION,))
os.rename(version_file + '.tmp', version_file)
setup(
name='discrete_sampling',
version=PKG_VERSION,
description='A Suite of Discrete Sampling Algorithms',
license='Apache-2.0',
classifiers=[
'Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 2.7',
],
packages=[
'discrete_sampling',
'discrete_sampling.tests',
],
package_dir={
'discrete_sampling': 'src',
'discrete_sampling.tests': 'tests',
},
cmdclass={
'build_py': local_build_py,
'sdist': local_sdist,
},
package_data={
'discrete_sampling': [
'binexp',
'decimal',
'orderm2',
'phi',
],
},
install_requires=['numpy', 'pytest', 'scipy']
)