-
Notifications
You must be signed in to change notification settings - Fork 60
/
setup.py
194 lines (162 loc) · 6.75 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
from __future__ import print_function
import contextlib
import os
import platform
import shutil
import subprocess
import sys
import tempfile
from setuptools import setup, Extension
from setuptools.command import build_ext, bdist_egg, develop
from distutils.spawn import find_executable
from distutils import log, sysconfig
from distutils.command import build
VERSION = "0.2.13"
# See:
# http://stackoverflow.com/questions/3223604/how-to-create-a-temporary-directory-and-get-the-path-file-name-in-python
@contextlib.contextmanager
def cd(newdir, cleanup=lambda: True):
previdr = os.getcwd()
os.chdir(os.path.expanduser(newdir))
try:
yield
finally:
os.chdir(previdr)
cleanup()
@contextlib.contextmanager
def tempdir():
dirpath = tempfile.mkdtemp()
def cleanup():
# see http://stackoverflow.com/questions/2656322/shutil-rmtree-fails-on-windows-with-access-is-denied
def onerror(func, path, exc_info):
import stat
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
shutil.rmtree(dirpath, onerror=onerror)
with cd(dirpath, cleanup):
yield dirpath
# Based on https://github.com/symengine/symengine.py/blob/master/setup.py
class CMakeBuildExt(build.build):
user_options = build.build.user_options + \
[('icu-root=', None, "Path to ICU root"),
('generator=', None, "CMake build generator")]
def initialize_options(self):
build.build.initialize_options(self)
self.icu_root = None
self.generator = 'MSYS Makefiles' if platform.system() == 'Windows' else None
def cmake_build(self):
src_dir = os.path.dirname(os.path.realpath(__file__))
cmake_exe = find_executable("cmake")
if not cmake_exe:
raise EnvironmentError("Could not find cmake executable")
py_version = "{}.{}".format(sys.version_info[0], sys.version_info[1])
cmake_cmd = [cmake_exe, src_dir, "-DCMAKE_BUILD_TYPE=Release",
"-DMETA_STATIC_UTF=On", "-DBUILD_STATIC_ICU=On"]
cmake_cmd.append("-DPYTHON_INCLUDE_DIRS={}".format(sysconfig.get_python_inc()))
if platform.system() == 'Windows':
libpython = "libpython{}{}.a".format(sys.version_info[0],
sys.version_info[1])
libpython_path = os.path.join(sysconfig.get_python_inc(),
'..', 'libs', libpython)
cmake_cmd.append("-DPYTHON_LIBRARY={}".format(libpython_path))
if self.icu_root:
cmake_cmd.extend(["-DICU_ROOT={}".format(self.icu_root)])
if self.generator:
cmake_cmd.extend(["-G{}".format(self.generator)])
with tempdir() as dirpath:
print("Build directory: {}".format(os.getcwd()))
if subprocess.call(cmake_cmd) != 0:
raise EnvironmentError("CMake invocation failed")
if subprocess.call([cmake_exe, "--build", "."]) != 0:
raise EnvironmentError("CMake build failed")
if subprocess.call([cmake_exe, "--build", ".", "--target",
"install"]) != 0:
raise EnvironmentError("CMake install failed")
# Make dummy __init__.py
initpy = os.path.join(src_dir, "dist", "metapy", "__init__.py")
with open(initpy, "w") as f:
f.write("from .metapy import *\n")
f.write('__version__ = "{}"\n'.format(VERSION))
# Copy over extra DLLs on Windows
if platform.system() == 'Windows':
dlls = ['libwinpthread-1.dll', 'libgcc_s_seh-1.dll', 'libstdc++-6.dll', 'zlib1.dll']
for dll in dlls:
shutil.copyfile(os.path.join("c:", os.sep, "msys64", "mingw64", "bin", dll),
os.path.join(src_dir, "dist", "metapy", dll))
def run(self):
self.cmake_build()
return build.build.run(self)
class DummyBuildExt(build_ext.build_ext):
def __init__(self, *args, **kwargs):
build_ext.build_ext.__init__(self, *args, **kwargs)
def run(self):
# do nothing; cmake already built the extension
pass
class DummyBDistEgg(bdist_egg.bdist_egg):
def __init__(self, *args, **kwargs):
bdist_egg.bdist_egg.__init__(self, *args, **kwargs)
def run(self):
self.run_command("build")
return bdist_egg.bdist_egg.run(self)
class DummyDevelop(develop.develop):
def __init__(self, *args, **kwargs):
develop.develop.__init__(self, *args, **kwargs)
def run(self):
self.run_command("build")
return develop.develop.run(self)
def clean_dist():
src_dir = os.path.dirname(os.path.realpath(__file__))
dist_dir = os.path.join(src_dir, "dist", "metapy")
if os.path.exists(dist_dir):
log.info("Deleting distribution directory {}".format(dist_dir))
shutil.rmtree(dist_dir)
os.makedirs(dist_dir)
clean_dist()
setup(name = 'metapy',
version = VERSION,
description = 'Python bindings for MeTA',
author = 'Chase Geigle',
author_email = '[email protected]',
url = 'https://github.com/meta-toolkit/metapy',
license = 'MIT',
packages = ['metapy'],
package_dir = { '': 'dist' },
include_package_data = True,
cmdclass = {
'build': CMakeBuildExt,
'build_ext': DummyBuildExt,
'bdist_egg': DummyBDistEgg,
'develop': DummyDevelop
},
zip_safe = False,
ext_modules = [Extension('metapy', [])],
ext_package='metapy',
classifiers = [
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License',
'License :: OSI Approved :: University of Illinois/NCSA Open Source License',
'Operating System :: POSIX :: Linux',
'Operating System :: MacOS :: MacOS X',
'Operating System :: Microsoft :: Windows',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Artificial Intelligence',
'Topic :: Scientific/Engineering :: Information Analysis',
'Topic :: Text Processing',
'Topic :: Text Processing :: Filters',
'Topic :: Text Processing :: General',
'Topic :: Text Processing :: Indexing',
'Topic :: Text Processing :: Linguistic',
],
keywords = [
'NLP', 'natural language processing',
'IR', 'information retrieval',
'CL', 'computational lingusitics',
'parsing', 'tagging', 'tokenizing', 'syntax', 'lingustics',
'natural language', 'text mining', 'text analysis'
])