-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
135 lines (115 loc) · 3.74 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
import glob
import os
import os.path
import shutil
import sys
from setuptools import setup
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext
from setuptools.command.install import install
from setuptools.command.sdist import sdist
from distutils.command.clean import clean
VERSION = '1.1.1'
MPACK_VERSION = '1.0.5'
REPO = 'https://github.com/libmpack/libmpack-python'
class Clean(clean):
def run(self):
if os.path.exists('.gitignore'):
files = []
with open('.gitignore') as gitignore:
for pattern in gitignore:
for f in glob.glob(pattern.strip()):
if os.path.isdir(f): shutil.rmtree(f)
elif os.path.isfile(f): os.unlink(f)
clean.run(self)
extension_src = 'mpack/_mpack.c'
extensions = [Extension("mpack._mpack", [extension_src])]
def _download_mpack():
import tarfile
if sys.version_info >= (3, 0):
import urllib.request as urllib
else:
import urllib
url = 'https://github.com/libmpack/libmpack/archive/{}.tar.gz'.format(
MPACK_VERSION)
print('downloading libmpack...')
file_tmp = urllib.urlretrieve(url, filename=None)[0]
print('extracting libmpack...')
tar = tarfile.open(file_tmp)
tar.extractall('mpack')
directory = glob.glob('mpack/libmpack*')[0]
shutil.move(directory, 'mpack/mpack-src')
def _autopxd():
from autopxd import translate
# due to some current limitations in autopxd, we must change
# directories to ensure the pxd is generated with the correct includes
cwd = os.getcwd()
os.chdir('mpack')
mpack_src = 'mpack-src/src/mpack.c'
with open(mpack_src) as f:
hdr = f.read()
with open('_cmpack.pxd', 'w') as f:
f.write(translate(hdr, mpack_src))
os.chdir(cwd)
def _cythonize():
from Cython.Build import cythonize
kwargs = {
'gdb_debug': True,
'language_level': 3
}
if os.getenv('NDEBUG', False):
kwargs['gdb_debug'] = False
cythonize([Extension('mpack._mpack', ['mpack/_mpack.pyx'])], **kwargs)
def _should_download_mpack():
return not os.path.exists('mpack/mpack-src/src/mpack.c')
def _should_autopxd():
return not os.path.exists('mpack/_cmpack.pxd')
def _should_cythonize():
try:
import Cython.Build
except ImportError:
return False
return (not os.path.exists(extension_src)
or os.environ.get('CYTHONIZE_MPACK', None) is not None)
def with_hooks(cmdclass):
class Sub(cmdclass):
def run(self):
if _should_cythonize():
_cythonize()
cmdclass.run(self)
return Sub
def datafiles():
if _should_download_mpack():
_download_mpack()
if _should_autopxd():
_autopxd()
if _should_cythonize():
_cythonize()
dataexts = (".c", ".h", ".pxd", ".pyx")
datafiles = []
getext = lambda filename: os.path.splitext(filename)[1]
for datadir in ['mpack']:
datafiles.extend([(
root, [os.path.join(root, f)
for f in files if getext(f) in dataexts])
for root, dirs, files in os.walk(datadir)])
return datafiles
if __name__ == '__main__':
setup(
name="mpack",
version=VERSION,
description="Python binding to libmpack",
packages=['mpack'],
ext_modules=extensions,
data_files=datafiles(),
install_requires=['future'],
url=REPO,
download_url='{0}/archive/{1}.tar.gz'.format(REPO, VERSION),
license='MIT',
cmdclass={
'build_ext': with_hooks(build_ext),
'clean': Clean,
},
author="Thiago de Arruda",
author_email="tpadilha84@gmail.com"
)