forked from ckiplab/ckipnlp
/
setup.py
executable file
·250 lines (206 loc) · 8.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
__author__ = 'Mu Yang <http://muyang.pro>'
__copyright__ = '2018-2019 CKIP Lab'
__license__ = 'CC BY-NC-SA 4.0'
from setuptools import dist
dist.Distribution().fetch_build_eggs([
'Cython>=0.29',
])
################################################################################
import os
import sys
import warnings
from distutils.version import StrictVersion
import setuptools
assert StrictVersion(setuptools.__version__) >= StrictVersion('40.0'), \
'Please update setuptools to 40.0+ using `pip install -U setuptools`.'
################################################################################
from setuptools import setup, find_namespace_packages
from setuptools.extension import Extension
from setuptools.command.install import install
from setuptools.command.develop import develop
from Cython.Build import cythonize
import ckipnlp as about
with open('README.rst', encoding='utf-8') as fin:
readme = fin.read()
################################################################################
def os_environ_append(name, dirpath):
if name in os.environ:
os.environ[name] += os.pathsep + dirpath
else:
os.environ[name] = dirpath
def os_environ_prepend(name, dirpath):
if name in os.environ:
os.environ[name] = dirpath + os.pathsep + os.environ[name]
else:
os.environ[name] = dirpath
class CommandMixin(object):
user_options = [
('ws', None, 'with CKIPWS [default]'),
('parser', None, 'with CKIP-Parser [default]'),
('no-ws', None, 'without CKIPWS'),
('no-parser', None, 'without CKIP-Parser'),
('ws-dir=', None, 'CKIPWS root directory'),
('ws-lib-dir=', None, 'CKIPWS libraries directory [default is <ws-dir>/lib]'),
('ws-share-dir=', None, 'CKIPWS share directory [default is <ws-dir>]'),
('parser-dir=', None, 'CKIP-Parser root directory'),
('parser-lib-dir=', None, 'CKIP-Parser libraries directory [default is "<parser-dir>/lib"]'),
('parser-share-dir=', None, 'CKIPWS share directory [default is "<parser-dir>"]'),
('data2-dir=', None, 'CKIPWS "Data2" directory [default is "<ws-share-dir>/Data2" or "<parser-share-dir>/Data2"]'),
('rule-dir=', None, 'CKIP-Parser "Rule" directory [default is "<parser-share-dir>/Rule"]'),
('rdb-dir=', None, 'CKIP-Parser "RDB" directory [default is "<parser-share-dir>/RDB"]'),
]
negative_opt = {
'no-ws': 'ws',
'no-parser': 'parser',
}
def initialize_options(self):
self.ws = False
self.parser = False
self.ws_dir = None
self.ws_lib_dir = None
self.ws_share_dir = None
self.parser_dir = None
self.parser_lib_dir = None
self.parser_share_dir = None
self.data2_dir = None
self.rule_dir = None
self.rdb_dir = None
super(CommandMixin, self).initialize_options()
def finalize_options(self):
# subdirectory
opt_subdirectory = [
('ws_lib_dir', 'ws_dir', 'lib',),
('ws_share_dir', 'ws_dir', '',),
('parser_lib_dir', 'parser_dir', 'lib',),
('parser_share_dir','parser_dir', '',),
('data2_dir', 'ws_share_dir', 'Data2',),
('data2_dir', 'parser_share_dir', 'Data2',),
('rule_dir', 'parser_share_dir', 'Rule',),
('rdb_dir', 'parser_share_dir', 'RDB',),
]
for opt0, opt1, subdir in opt_subdirectory:
dir0 = getattr(self, opt0)
dir1 = getattr(self, opt1)
if not dir0 and dir1:
setattr(self, opt0, os.path.join(dir1, subdir))
# directory
opt_directory = [
'ws_lib_dir',
'parser_lib_dir',
'data2_dir',
'rule_dir',
'rdb_dir',
]
for opt0 in opt_directory:
dir0 = getattr(self, opt0)
if dir0 and not os.path.isdir(dir0):
raise IOError('--%s (%s) is not a directory' % (opt0.replace('_', '-'), dir0,))
super(CommandMixin, self).finalize_options()
def run(self):
# CKIPWS
if self.ws:
print('- Enable CKIPWS support')
if self.ws_lib_dir:
print('- Use CKIPWS library from (%s)' % self.ws_lib_dir)
i = next((i for i, em in enumerate(self.distribution.ext_modules) if em.name == 'ckipnlp._core.ws'), None)
self.distribution.ext_modules[i].library_dirs.append(self.ws_lib_dir)
self.distribution.ext_modules[i].runtime_library_dirs.append(self.ws_lib_dir)
else:
print('- Disable CKIPWS support')
i = next((i for i, em in enumerate(self.distribution.ext_modules) if em.name == 'ckipnlp._core.ws'), None)
if i is not None: del self.distribution.ext_modules[i]
# CKIP-Parser
if self.parser:
print('- Enable CKIP-Parser support')
if self.parser_lib_dir:
print('- Use CKIP-Parser library from (%s)' % self.parser_lib_dir)
i = next((i for i, em in enumerate(self.distribution.ext_modules) if em.name == 'ckipnlp._core.parser'), None)
self.distribution.ext_modules[i].library_dirs.append(self.parser_lib_dir)
self.distribution.ext_modules[i].runtime_library_dirs.append(self.parser_lib_dir)
else:
print('- Disable CKIP-Parser support')
i = next((i for i, em in enumerate(self.distribution.ext_modules) if em.name == 'ckipnlp._core.parser'), None)
if i is not None: del self.distribution.ext_modules[i]
# Data
if self.data2_dir:
print('- Use "Data2" from (%s)' % self.data2_dir)
self.data_files('share/ckipnlp/Data2/', self.data2_dir)
if self.rule_dir:
print('- Use "Rule" from (%s)' % self.rule_dir)
self.data_files('share/ckipnlp/Rule/', self.rule_dir)
if self.rdb_dir:
print('- Use "RDB" from (%s)' % self.rdb_dir)
self.data_files('share/ckipnlp/RDB/', self.rdb_dir)
# Python packages
# self.distribution.packages = list(self.distribution.package_dir.keys())
super(CommandMixin, self).run()
def data_files(self, prefix, dirtop):
for dirpath, _, files in os.walk(dirtop):
self.distribution.data_files.append((
os.path.join(prefix, os.path.relpath(dirpath, dirtop)),
[os.path.join(dirpath, file) for file in files],
))
class InstallCommand(CommandMixin, install):
user_options = install.user_options + CommandMixin.user_options
negative_opt = install.negative_opt
negative_opt.update(CommandMixin.negative_opt)
def __init__(self, *args, **kwargs):
install.__init__(self, *args, **kwargs)
class DevelopCommand(CommandMixin, develop):
user_options = develop.user_options + CommandMixin.user_options
negative_opt = develop.negative_opt
negative_opt.update(CommandMixin.negative_opt)
def __init__(self, *args, **kwargs):
develop.__init__(self, *args, **kwargs)
################################################################################
setup(
name=about.__name__,
version=about.__version__,
author=about.__author_name__,
author_email=about.__author_email__,
description=about.__description__,
long_description=readme,
long_description_content_type='text/x-rst',
url=about.__url__,
download_url=about.__download_url__,
platforms=['linux_x86_64'],
license=about.__license__,
classifiers=[
'Development Status :: 4 - Beta',
'Environment :: Console',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Cython',
'License :: Free for non-commercial use',
'Operating System :: POSIX :: Linux',
'Natural Language :: Chinese (Traditional)',
],
python_requires='>=3.5',
packages=find_namespace_packages(include=['ckipnlp', 'ckipnlp.*', 'ckipws', 'ckipparser',]),
install_requires=[
'treelib>=1.5',
],
ext_modules=cythonize(
[
Extension('ckipnlp._core.ws',
sources=['src/ws/ckipws.pyx'],
libraries=['WordSeg'],
),
Extension('ckipnlp._core.parser',
sources=['src/parser/ckipparser.pyx'],
libraries=['CKIPCoreNLP','CKIPParser','CKIPWS','CKIPSRL'],
),
],
build_dir='build',
),
data_files=[],
cmdclass={
'install': InstallCommand,
'develop': DevelopCommand,
},
)