-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsetup.py
More file actions
150 lines (124 loc) · 3.82 KB
/
Copy pathsetup.py
File metadata and controls
150 lines (124 loc) · 3.82 KB
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
import os
import subprocess
import sys
from distutils.errors import DistutilsExecError
from distutils.spawn import spawn
from distutils.version import LooseVersion
from glob import glob
from itertools import chain
from os.path import exists, join
from subprocess import run
import numpy as np
import setuptools.command.build_py
import setuptools.command.develop
from setuptools import Extension, find_packages, setup
# Reference: https://github.com/r9y9/pyopenjtalk/blob/master/setup.py
version = "0.1.1"
min_cython_ver = "0.21.0"
try:
import Cython
ver = Cython.__version__
_CYTHON_INSTALLED = ver >= LooseVersion(min_cython_ver)
except ImportError:
_CYTHON_INSTALLED = False
try:
if not _CYTHON_INSTALLED:
raise ImportError("No supported version of Cython installed.")
from Cython.Distutils import build_ext
cython = True
except ImportError:
cython = False
if cython:
ext = ".pyx"
cmdclass = {"build_ext": build_ext}
else:
ext = ".cpp"
cmdclass = {}
if not os.path.exists(join("natsume", "openjtalk" + ext)):
raise RuntimeError("Cython is required to generate C++ code")
# open_jtalk sources
src_top = join("libs", "open_jtalk", "src")
if not exists(join(src_top, "mecab", "src", "config.h")):
cwd = os.getcwd()
build_dir = join(src_top, "build")
os.makedirs(build_dir, exist_ok=True)
os.chdir(build_dir)
r = run(["cmake", "..", "-DHTS_ENGINE_INCLUDE_DIR=.", "-DHTS_ENGINE_LIB=dummy"])
r.check_returncode()
os.chdir(cwd)
all_src = []
include_dirs = []
for s in [
"mecab/src",
"mecab2njd",
"njd",
"njd_set_accent_phrase",
"njd_set_accent_type",
"njd_set_digit",
"njd_set_long_vowel",
"njd_set_pronunciation",
"njd_set_unvoiced_vowel",
"text2mecab",
]:
all_src += glob(join(src_top, s, "*.c"))
all_src += glob(join(src_top, s, "*.cpp"))
include_dirs.append(join(os.getcwd(), src_top, s))
# Extension for OpenJTalk frontend
ext_modules = [
Extension(
name="natsume.openjtalk",
sources=[join("natsume", "openjtalk" + ext)] + all_src,
include_dirs=[np.get_include()] + include_dirs,
extra_compile_args=[],
extra_link_args=[],
language="c++",
define_macros=[
("HAVE_CONFIG_H", None),
("DIC_VERSION", 102),
("MECAB_DEFAULT_RC", '"dummy"'),
("PACKAGE", '"open_jtalk"'),
("VERSION", '"1.10"'),
("CHARSET_UTF_8", None),
]
)
]
cwd = os.path.dirname(os.path.abspath(__file__))
class build_py(setuptools.command.build_py.build_py):
def run(self):
self.create_version_file()
setuptools.command.build_py.build_py.run(self)
@staticmethod
def create_version_file():
global version, cwd
print("-- Building version " + version)
version_path = os.path.join(cwd, "natsume", "version.py")
with open(version_path, "w") as f:
f.write("__version__ = '{}'\n".format(version))
class develop(setuptools.command.develop.develop):
def run(self):
build_py.create_version_file()
setuptools.command.develop.develop.run(self)
cmdclass["build_py"] = build_py
cmdclass["develop"] = develop
with open("README.md", "r", encoding="utf8") as fd:
long_description = fd.read()
setup(
name="natsume",
version=version,
description="A Japanese text frontend processing toolkit",
long_description=long_description,
long_description_content_type="text/markdown",
author="Francis Hu",
author_email="franciskomizu@gmail.com",
url="https://github.com/Francis-Komizu/natsume",
license="GPL",
packages=find_packages(),
ext_modules=ext_modules,
cmdclass=cmdclass,
install_requires=[
"numpy >= 1.20.0",
"cython >= " + min_cython_ver + ",<3.0.0",
"six",
"tqdm",
]
)