forked from BYVoid/OpenCC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
173 lines (141 loc) · 5.34 KB
/
setup.py
File metadata and controls
173 lines (141 loc) · 5.34 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import json
import os
import re
import shutil
import subprocess
import setuptools
import setuptools.command.build_py
_this_dir = os.path.dirname(os.path.abspath(__file__))
_author_file = os.path.join(_this_dir, 'AUTHORS')
_readme_file = os.path.join(_this_dir, 'README.md')
_fallback_version = '1.3.1'
def _get_version_from_git():
try:
raw = subprocess.check_output(
['git', 'describe', '--tags', '--long', '--always'],
cwd=_this_dir,
stderr=subprocess.DEVNULL,
).decode('utf-8').strip()
except (OSError, subprocess.CalledProcessError):
return ''
dirty = ''
for diff_cmd in (['git', 'diff', '--quiet'], ['git', 'diff', '--cached', '--quiet']):
try:
subprocess.check_call(
diff_cmd,
cwd=_this_dir,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
except (OSError, subprocess.CalledProcessError):
dirty = '.dirty'
break
release_match = re.match(r'^(?:v|ver\.)(\d+\.\d+\.\d+)-0-g[0-9a-f]+$', raw)
if release_match:
return '{}{}'.format(release_match.group(1), dirty)
dev_match = re.match(r'^(?:v|ver\.)(\d+\.\d+\.\d+)-(\d+)-g([0-9a-f]+)$', raw)
if dev_match:
return '{}.dev{}+g{}{}'.format(
dev_match.group(1),
dev_match.group(2),
dev_match.group(3),
dirty,
)
return '{}+g{}{}'.format(_fallback_version, raw, dirty)
def get_version_info():
# Prefer Git-derived version from environment (set by CI or compute-version.sh)
env_version = os.environ.get('VERSION', '')
if env_version:
# Strip leading 'v' prefix for PEP 440 compatibility
return env_version.lstrip('v')
git_version = _get_version_from_git()
if git_version:
return git_version
return _fallback_version
def get_author_info():
if not os.path.isfile(_author_file):
return 'BYVoid', 'byvoid@byvoid.com'
authors = []
emails = []
author_pattern = re.compile(r'(.+) <(.+)>')
with open(_author_file, 'rb') as f:
for line in f:
match = author_pattern.search(line.decode('utf-8'))
if not match:
continue
authors.append(match.group(1))
emails.append(match.group(2))
if len(authors) == 0:
return 'BYVoid', 'byvoid@byvoid.com'
return ', '.join(authors), ', '.join(emails)
def get_long_description():
with open(_readme_file, 'rb') as f:
return f.read().decode('utf-8')
class BuildPyCommand(setuptools.command.build_py.build_py, object):
"""Bundle OpenCC's JSON configs and text dictionaries into the package."""
def run(self):
super(BuildPyCommand, self).run()
self._copy_opencc_data()
def _copy_opencc_data(self):
src_config = os.path.join(_this_dir, 'data', 'config')
src_dict = os.path.join(_this_dir, 'data', 'dictionary')
dst_base = os.path.join(self.build_lib, 'opencc')
dst_config = os.path.join(dst_base, 'config')
dst_dict = os.path.join(dst_base, 'dictionary')
os.makedirs(dst_config, exist_ok=True)
for fname in os.listdir(src_config):
if not fname.endswith('.json'):
continue
src_config_path = os.path.join(src_config, fname)
with open(src_config_path, encoding='utf-8') as f:
config = json.load(f)
if config.get('segmentation', {}).get('type') != 'mmseg':
continue
shutil.copy2(src_config_path, os.path.join(dst_config, fname))
os.makedirs(dst_dict, exist_ok=True)
for fname in os.listdir(src_dict):
if fname.endswith('.txt'):
shutil.copy2(
os.path.join(src_dict, fname),
os.path.join(dst_dict, fname),
)
packages = ['opencc']
version_info = get_version_info()
author_info = get_author_info()
setuptools.setup(
name='OpenCC',
version=version_info,
author=author_info[0],
author_email=author_info[1],
description=" Conversion between Traditional and Simplified Chinese",
long_description=get_long_description(),
long_description_content_type="text/markdown",
url="https://github.com/BYVoid/OpenCC",
packages=packages,
package_dir={'opencc': 'python/opencc'},
package_data={
'opencc': ['py.typed', 'config/*.json', 'dictionary/*.txt'],
},
cmdclass={
'build_py': BuildPyCommand,
},
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Intended Audience :: Science/Research',
'Natural Language :: Chinese (Simplified)',
'Natural Language :: Chinese (Traditional)',
'Programming Language :: Python',
'Programming Language :: Python :: 3',
'License :: OSI Approved :: Apache Software License',
'Topic :: Scientific/Engineering',
'Topic :: Software Development',
'Topic :: Software Development :: Libraries',
'Topic :: Software Development :: Libraries :: Python Modules',
'Topic :: Software Development :: Localization',
'Topic :: Text Processing :: Linguistic',
'Typing :: Typed',
],
license='Apache License 2.0',
keywords=['opencc', 'convert', 'chinese'],
)