-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-full.py
More file actions
358 lines (290 loc) · 11.3 KB
/
Copy pathsetup-full.py
File metadata and controls
358 lines (290 loc) · 11.3 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
__SHORT__DESCRIPTION__ = """
PLWordNet Handler - narzędzie do pracy z polskim słownikiem semantycznym.
Pozwala na odczyt z bazy/grafów, zarządzanie elementami, wyszukiwanie
i eksportowanie danych.
"""
import os
import shutil
import tarfile
import urllib.request
from pathlib import Path
from dataclasses import dataclass
from setuptools import setup, find_packages
from setuptools.command.install import install
from setuptools.command.develop import develop
from setuptools.command.egg_info import egg_info
with open(".version", "r") as fh:
file_version = fh.read().strip()
class _SetupConfiguration:
REPO_URL = "https://github.com/radlab-dev-group/radlab-plwordnet"
@dataclass
class SetupConfig:
module_name: str = "plwordnet-handler"
module_version: str = file_version
handler_module_name: str = "plwordnet_handler"
short_description: str = "Narzędzie do pracy ze Słowosiecią"
author: str = "RadLab Team"
author_email: str = "pawel@radlab.dev"
radlab_site: str = "https://radlab.dev"
radlab_resources: str = (
"https://resources.radlab.dev/persistant/plwn_handler"
)
remote_base_resources_dir: str = "resources"
remote_base_graphs_dir: str = "plwordnet/graphs"
remote_test_graph_name: str = "slowosiec_test.gz"
remote_full_graph_name: str = "slowosiec_full.gz"
@property
def full_plwn_graph_url(self):
return (
f"{self.radlab_resources}/"
f"{self.remote_base_resources_dir}/"
f"{self.remote_base_graphs_dir}/"
f"{self.remote_full_graph_name}"
)
@property
def test_plwn_graph_url(self):
return (
f"{self.radlab_resources}/"
f"{self.remote_base_resources_dir}/"
f"{self.remote_base_graphs_dir}/"
f"{self.remote_test_graph_name}"
)
class _ReqReadmeHandler:
@staticmethod
def read_requirements():
requirements_path = os.path.join(
os.path.dirname(__file__), "requirements.txt"
)
if os.path.exists(requirements_path):
with open(requirements_path, "r", encoding="utf-8") as f:
return [
line.strip()
for line in f
if line.strip() and not line.startswith("#")
]
return []
@staticmethod
def read_long_description():
readme_files = ["README.md"]
for readme_file in readme_files:
readme_path = os.path.join(os.path.dirname(__file__), readme_file)
if os.path.exists(readme_path):
with open(readme_path, "r", encoding="utf-8") as f:
return f.read()
return __SHORT__DESCRIPTION__
class _ResourcesHandler:
"""
Handler for collecting and installing resources directory.
"""
base_package_data = {
"": ["*.txt", "*.md", "*.rst"],
_SetupConfiguration.SetupConfig.handler_module_name: [
"resources/*",
"resources/**/*",
],
}
@staticmethod
def get_resources_files():
"""
Recursively collect all files in the resources directory.
Returns:
Dictionary mapping package paths to file lists
"""
resources_dir = Path("resources")
if not resources_dir.exists():
print("Warning: resources directory not found in the repository")
return {}
package_data = {}
for root, dirs, files in os.walk(resources_dir):
if not files:
continue
rel_root = Path(root).relative_to(resources_dir)
if str(rel_root) == ".":
file_patterns = [f"resources/{f}" for f in files]
else:
file_patterns = [f"resources/{rel_root}/{f}" for f in files]
package_path = _SetupConfiguration.SetupConfig.handler_module_name
if package_path not in package_data:
package_data[package_path] = []
package_data[package_path].extend(file_patterns)
return package_data
@staticmethod
def copy_resources_to_package():
"""
Copy resources directory to plwordnet_handler package.
This ensures resources are included in the package structure.
"""
source_resources = Path("resources")
target_resources = Path("plwordnet_handler/resources")
if not source_resources.exists():
print("Warning: Source resources directory not found")
return False
try:
# Remove the existing target if it exists
if target_resources.exists():
shutil.rmtree(target_resources)
# Copy the entire resources directory
shutil.copytree(source_resources, target_resources)
print(
f"Successfully copied resources from "
f"{source_resources} to {target_resources}"
)
# Create __init__.py in the resources directory to make it a package
init_file = target_resources / "__init__.py"
init_file.write_text('"""PLWordNet resources package."""\n')
return True
except Exception as e:
print(f"Error copying resources: {e}")
return False
class _GraphDownloadMixin:
"""
Mixin for downloading graphs during installation.
"""
ENV_DOWN_TEST_PLWN = "PLWORDNET_DOWNLOAD_TEST"
ENV_DOWN_FULL_PLWN = "PLWORDNET_DOWNLOAD_FULL"
def download_graphs_if_requested(self):
"""
Download graphs if environment variables are set.
"""
config = _SetupConfiguration.SetupConfig()
download_test = os.environ.get(self.ENV_DOWN_TEST_PLWN, "").lower() in (
"1",
"true",
"yes",
)
download_full = os.environ.get(self.ENV_DOWN_FULL_PLWN, "").lower() in (
"1",
"true",
"yes",
)
if not (download_test or download_full):
print("No graph download requested.")
return
# Determine target directory
if hasattr(self, "install_lib") and self.install_lib:
target_dir = self.install_lib
else:
# Fallback for develop mode
import site
target_dir = site.getsitepackages()[0]
graphs_dir = os.path.join(
target_dir, "plwordnet_handler", "resources", "graphs"
)
os.makedirs(graphs_dir, exist_ok=True)
print(f"Target graphs directory: {graphs_dir}")
if download_test:
print("Downloading test graph data...")
self._download_and_extract_graph(
config.test_plwn_graph_url,
graphs_dir,
"plwn_test_graph",
)
if download_full:
print("Downloading full graph data...")
self._download_and_extract_graph(
config.full_plwn_graph_url,
graphs_dir,
"plwn_full_graph",
)
@staticmethod
def _download_and_extract_graph(url, destination_path, graph_type):
"""
Download and extract a gzipped graph file.
"""
archive_path = graph_type + ".gz"
try:
print(f"Downloading {graph_type} from {url}...")
urllib.request.urlretrieve(url, archive_path)
print(f"Extracting {graph_type}...")
with tarfile.open(archive_path, "r:gz") as tar:
tar.extractall(path=destination_path)
if os.path.exists(archive_path):
os.remove(archive_path)
print(f"Successfully installed {graph_type} to {destination_path}")
except Exception as e:
print(f"Error downloading {graph_type}: {e}")
for path in [archive_path, destination_path]:
if os.path.exists(path):
os.remove(path)
class _CustomInstallCommand(install, _GraphDownloadMixin):
"""
Custom installation command that copies resources and downloads graphs.
"""
def run(self):
# Copy resources before installation
print("Copying resources directory to package...")
_ResourcesHandler.copy_resources_to_package()
# Run normal installation
install.run(self)
# Download graphs if requested
self.download_graphs_if_requested()
class _CustomDevelopCommand(develop, _GraphDownloadMixin):
"""Custom develop command that copies resources and downloads graphs."""
def run(self):
# Copy resources before development installation
print("Copying resources directory to package...")
_ResourcesHandler.copy_resources_to_package()
# Run normal develop installation
develop.run(self)
# Download graphs if requested
self.download_graphs_if_requested()
class _CustomEggInfoCommand(egg_info, _GraphDownloadMixin):
"""Custom egg_info command that ensures resources are available."""
def run(self):
# Copy resources before egg_info
print("Ensuring resources are available...")
_ResourcesHandler.copy_resources_to_package()
# Run normal egg_info
egg_info.run(self)
# Download graphs if explicitly requested during egg_info
if any(
env in os.environ
for env in [self.ENV_DOWN_TEST_PLWN, self.ENV_DOWN_FULL_PLWN]
):
try:
self.download_graphs_if_requested()
except Exception as e:
print(f"Warning: Could not download graphs during egg_info: {e}")
class _ResourceInit:
"""
Mixin for downloading graphs during installation.
"""
@staticmethod
def install_resources():
print("Preparing resources for installation...")
_ResourcesHandler.copy_resources_to_package()
resources_package_data = _ResourcesHandler.get_resources_files()
for package, files in resources_package_data.items():
if package in _ResourcesHandler.base_package_data:
_ResourcesHandler.base_package_data[package].extend(files)
else:
_ResourcesHandler.base_package_data[package] = files
_ResourceInit.install_resources()
setup(
name=_SetupConfiguration.SetupConfig.module_name,
version=_SetupConfiguration.SetupConfig.module_version,
description=_SetupConfiguration.SetupConfig.short_description,
long_description=_ReqReadmeHandler.read_long_description(),
long_description_content_type="text/markdown",
author=_SetupConfiguration.SetupConfig.author,
author_email=_SetupConfiguration.SetupConfig.author_email,
url=_SetupConfiguration.REPO_URL,
packages=find_packages(),
include_package_data=True,
package_data=_ResourcesHandler.base_package_data,
install_requires=_ReqReadmeHandler.read_requirements(),
python_requires=">=3.6",
entry_points={
"console_scripts": [
"plwordnet-cli=apps.cli.plwordnet_cli:main",
"plwordnet-milvus=apps.cli.plwordnet_milvus_cli:main",
],
},
keywords="nlp, wordnet, polish, linguistics, semantic",
zip_safe=False,
cmdclass={
"install": _CustomInstallCommand,
"develop": _CustomDevelopCommand,
"egg_info": _CustomEggInfoCommand,
},
)