forked from jonghwanhyeon/python-mecab-ko
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
181 lines (148 loc) · 5.43 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
import os
import shutil
import subprocess
import sys
from setuptools import Extension, find_packages, setup
from setuptools.command.build_ext import build_ext
from setuptools.command.install import install
# Based on https://github.com/pybind/python_example
os.environ['PATH'] += os.pathsep + os.path.join(sys.prefix, 'bin')
class BuildExtensionCommand(build_ext):
compiler_options = {
'msvc': ['/EHsc'],
'unix': [],
}
if sys.platform == 'darwin':
compiler_options['unix'] += [
'-stdlib=libc++',
'-mmacosx-version-min=10.7',
]
def build_extensions(self):
compiler_type = self.compiler.compiler_type
options = self.compiler_options.get(compiler_type, [])
if compiler_type == 'unix':
options.append('-DVERSION_INFO="{}"'.format(self.distribution.get_version()))
options.append(self._cpp_flag())
if self._has_flag('-fvisibility=hidden'):
options.append('-fvisibility=hidden')
elif compiler_type == 'msvc':
options.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version())
for extension in self.extensions:
extension.extra_compile_args = options
super().build_extensions()
# As of Python 3.6, CCompiler has a `has_flag` method.
# cf http://bugs.python.org/issue26689
def _has_flag(self, flag):
"""Return a boolean indicating whether a flag name is supported on
the specified compiler.
"""
import tempfile
with tempfile.NamedTemporaryFile('w', suffix='.cpp') as output_file:
output_file.write('int main (int argc, char **argv) { return 0; }')
try:
self.compiler.compile([output_file.name], extra_postargs=[flag])
except setuptools.distutils.errors.CompileError:
return False
return True
def _cpp_flag(self):
"""Return the -std=c++[11/14] compiler flag.
The c++14 is prefered over c++11 (when it is available).
"""
if self._has_flag('-std=c++14'):
return '-std=c++14'
elif self._has_flag('-std=c++11'):
return '-std=c++11'
else:
raise RuntimeError('Unsupported compiler -- at least C++11 support is needed!')
class InstallCommand(install):
def run(self):
if not shutil.which('mecab'):
self.install_mecab()
super().run()
def install_mecab(self):
base_path = os.path.abspath(os.path.dirname(__file__))
scripts_directory = os.path.join(base_path, 'scripts')
subprocess.check_call([sys.executable,
os.path.join(scripts_directory, 'install-mecab-ko.py')],
cwd=scripts_directory)
def lazy(func):
class Decorator:
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
def __str__(self):
return func(*self.args, **self.kwargs)
def __add__(self, other):
return str(self) + other
def __radd__(self, other):
return other + str(self)
return Decorator
@lazy
def get_pybind_include(user=False):
import pybind11
return pybind11.get_include(user)
@lazy
def get_mecab_include_directory():
return subprocess.check_output([
'mecab-config', '--inc-dir']).decode('utf-8').strip()
@lazy
def get_mecab_library_directory():
return subprocess.check_output([
'mecab-config', '--libs-only-L']).decode('utf-8').strip()
with open('README.md', 'r', encoding='utf-8') as input_file:
long_description = input_file.read()
setup(
name='python-mecab-ko',
version='1.0.12',
url='https://github.com/jonghwanhyeon/python-mecab-ko',
author='Jonghwan Hyeon',
author_email='[email protected]',
description='A python binding for mecab-ko',
long_description=long_description,
long_description_content_type='text/markdown',
license='BSD',
keywords='mecab mecab-ko',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: BSD License',
'Natural Language :: Korean',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3 :: Only',
'Topic :: Text Processing',
'Topic :: Text Processing :: Linguistic',
],
zip_safe=False,
install_requires=[
'pybind11 ~= 2.0'
],
python_requires='>=3',
packages=find_packages(),
data_files=[('scripts', ['scripts/install-mecab-ko.py'])],
ext_modules=[
Extension(
name='_mecab',
sources=[
'mecab/pybind/_mecab/node.cpp',
'mecab/pybind/_mecab/path.cpp',
'mecab/pybind/_mecab/lattice.cpp',
'mecab/pybind/_mecab/dictionaryinfo.cpp',
'mecab/pybind/_mecab/tagger.cpp',
'mecab/pybind/_mecab/_mecab.cpp',
],
include_dirs=[
get_pybind_include(),
get_pybind_include(user=True),
get_mecab_include_directory(),
],
libraries=['mecab'],
library_dirs=[get_mecab_library_directory()],
runtime_library_dirs=[get_mecab_library_directory()],
language='c++',
),
],
cmdclass={
'install': InstallCommand,
'build_ext': BuildExtensionCommand
},
)