-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.py
More file actions
496 lines (449 loc) · 12 KB
/
Copy pathsetup.py
File metadata and controls
496 lines (449 loc) · 12 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
import re
import os
import importlib.util
from setuptools import setup, find_packages
import subprocess
import sys
PROJECT_ROOT = os.path.dirname(__file__)
SRC_DIR = os.path.join(PROJECT_ROOT, "src")
if SRC_DIR not in sys.path:
sys.path.insert(0, SRC_DIR)
from setuptools.command.build_py import build_py as _build_py
from setuptools.command.develop import develop as _develop
# ==========================================
# 1. 工具函数
# ==========================================
# 全局缓存状态,防止重复扫描和重复执行
BUILD_HOOKS_EXECUTED = False
def parse_requirements(filename):
"""从 requirements.txt 解析依赖列表,跳过空行和注释行"""
with open(filename, "r") as f:
return [line.strip() for line in f if line.strip() and not line.startswith("#")]
def requirement_key(dep):
"""Return the package name used by setup.py requirement groups."""
if " @ " in dep:
return dep.split(" @ ", 1)[0]
return re.split(r"[=<>~!]", dep)[0]
def resolve(requires, deps_dict):
"""将短名称列表映射为 requirements.txt 中的完整版本约束"""
return [deps_dict[r] for r in requires]
def unique(items):
"""列表去重,保持原始顺序"""
seen = set()
result = []
for item in items:
if item not in seen:
seen.add(item)
result.append(item)
return result
def discover_package_data():
"""
自动发现子模块的 package_data 配置
扫描 src/onescience 下所有子模块,查找 package_config.py 文件
"""
package_data = {}
src_dir = os.path.join(os.path.dirname(__file__), "src", "onescience")
for root, dirs, files in os.walk(src_dir):
if "package_config.py" in files:
try:
rel_path = os.path.relpath(root, src_dir)
module_parts = ["onescience"] + rel_path.split(os.sep) if rel_path != "." else ["onescience"]
module_name = ".".join(module_parts)
config_path = os.path.join(root, "package_config.py")
spec = importlib.util.spec_from_file_location(f"{module_name}.package_config", config_path)
config_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(config_module)
if hasattr(config_module, 'get_package_data'):
submodule_data = config_module.get_package_data()
package_data.update(submodule_data)
print(f" Discovered package config from: {module_name}")
except Exception as e:
print(f" Failed to load package config from {root}: {e}")
return package_data
def discover_build_hooks():
"""收集所有子模块声明的构建钩子"""
hooks = []
src_dir = os.path.join(os.path.dirname(__file__), "src", "onescience")
for root, dirs, files in os.walk(src_dir):
if "package_config.py" in files:
try:
rel_path = os.path.relpath(root, src_dir)
module_parts = ["onescience"] + rel_path.split(os.sep) if rel_path != "." else ["onescience"]
module_name = ".".join(module_parts)
config_path = os.path.join(root, "package_config.py")
spec = importlib.util.spec_from_file_location(f"{module_name}.package_config", config_path)
config_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(config_module)
if hasattr(config_module, "get_build_hook"):
hook = config_module.get_build_hook()
if hook is not None:
hooks.append(hook)
except Exception as e:
print(f" Failed to load build hook from {root}: {e}")
return hooks
def discover_extensions():
"""Collect extension modules declared by package configs."""
extensions = []
src_dir = os.path.join(os.path.dirname(__file__), "src", "onescience")
for root, dirs, files in os.walk(src_dir):
if "package_config.py" in files:
try:
rel_path = os.path.relpath(root, src_dir)
module_parts = ["onescience"] + rel_path.split(os.sep) if rel_path != "." else ["onescience"]
module_name = ".".join(module_parts)
config_path = os.path.join(root, "package_config.py")
spec = importlib.util.spec_from_file_location(f"{module_name}.package_config", config_path)
config_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(config_module)
if hasattr(config_module, "get_extensions"):
extensions.extend(config_module.get_extensions(PROJECT_ROOT))
except Exception as e:
raise RuntimeError(f"Failed to load extensions from {root}: {e}") from e
return extensions
def run_build_hooks():
"""
执行所有构建钩子
使用全局标记确保整个构建过程中只执行一次
"""
global BUILD_HOOKS_EXECUTED
if BUILD_HOOKS_EXECUTED:
return
project_root = os.path.dirname(__file__)
env = os.environ.copy()
src_dir = os.path.join(project_root, "src")
env["PYTHONPATH"] = (
src_dir + os.pathsep + env.get("PYTHONPATH", "")
if env.get("PYTHONPATH")
else src_dir
)
hooks = discover_build_hooks()
print(f"Running {len(hooks)} package build hook(s)")
for hook in hooks:
hook(
project_root=project_root,
env=env,
python_executable=sys.executable,
subprocess_module=subprocess,
)
BUILD_HOOKS_EXECUTED = True
# ==========================================
# 构建钩子自定义命令
# 在 build_py / develop 阶段自动触发所有构建钩子
# ==========================================
class build_py(_build_py):
def run(self):
run_build_hooks()
super().run()
class develop(_develop):
def run(self):
run_build_hooks()
super().run()
# ==========================================
# 2. 依赖配置区 (后续维护仅需修改此处)
# ==========================================
# 约定:
# - core_requires: 所有领域都依赖的基础包
# - earth/cfd/bio/matchem_requires: 特定领域的定制依赖
one_deps = parse_requirements("requirements.txt")
deps = {requirement_key(dep): dep for dep in one_deps}
core_requires = [
"numpy",
"tqdm",
"treelib",
"hydra-core",
"termcolor",
"wandb",
"mlflow",
"pyyaml",
"h5py",
"ruamel.yaml",
"scikit-learn",
"einops",
"pandas",
"omegaconf",
"pybind11",
"matplotlib",
"pytz",
"s3fs",
"requests",
"importlib_metadata",
"scipy",
"setuptools",
"click",
]
earth_requires = [
"torchdata",
"timm",
"xarray",
"zarr",
"netcdf4",
"dask",
"cftime",
"seaborn",
"opencv-python",
"absl-py",
"torch_geometric",
"pytest",
"ecmwf-opendata",
"earthkit-regrid",
"Cartopy",
"anemoi-graphs",
"anemoi-models",
]
cfd_requires = [
"torchdata",
"timm",
"vtk",
"pyvista",
"shapely",
"torch_geometric",
"deepxde",
"gpytorch",
"seaborn",
"numba",
]
bio_requires = [
"torchdata",
#"megatron-core",
"lmdb",
"orjson",
"ml-collections",
"dm-tree",
"dm-haiku",
"diffrax",
"biopandas",
"biopython",
"pyrsistent",
"chex",
"flax",
"fiddle",
"lightning",
"sentencepiece",
"datasets",
"braceexpand",
"webdataset",
"nemo_run",
"tiktoken",
"zstandard",
"transformers",
"ftfy",
"modelcif",
"ihm",
"mashumaro",
"py3Dmol",
"biotite",
"rdkit",
"p_tqdm",
"gemmi",
"hydra-colorlog",
"fairscale",
# boltz dependencies
"types-requests",
"einx",
"chembl-structure-pipeline",
# State dependencies
"anndata",
"cell-load",
"geomloss",
"scanpy",
# alphagenome dependencies
"alphagenome",
"kagglehub",
"orbax-checkpoint",
"pyfaidx",
"jaxtyping",
"einshape",
"filelock",
"absl-py",
"jmp",
"ml-dtypes",
"opt-einsum",
"ninja",
"contextlib2",
"psutil",
"optree",
"gpytorch",
"torch_geometric",
"redis",
"pillow",
"tabulate",
"typeguard",
"pytest",
"pdbfixer",
"dllogger",
"e3nn",
# diffdock dependencies
"ProDy",
"fair-esm",
"spyrmsd",
# targetdiff dependencies
"aiohttp",
"async-timeout",
"attrs",
"blinker",
"brotli",
"brotlipy",
"cachetools",
"chardet",
"charset-normalizer",
"colorama",
"cryptography",
"docutils",
"easydict",
"google-auth",
"google-auth-oauthlib",
"greenlet",
"grpcio",
"idna",
"jinja2",
"joblib",
"markdown",
"markupsafe",
"mmcif-pdbx",
"mpmath",
"munkres",
"networkx",
"oauthlib",
"pooch",
"propka",
"pyasn1",
"pyasn1-modules",
"pycairo",
"pycparser",
"pyjwt",
"pyopenssl",
"pyparsing",
"pysocks",
"python-dateutil",
"protobuf",
"reportlab",
"requests-oauthlib",
"rsa",
"six",
"sqlalchemy",
"tensorboard",
"tensorboard-data-server",
"threadpoolctl",
"unicodedata2",
"urllib3",
"werkzeug",
"yarl",
"meeko",
"pdb2pqr",
"pyranges",
"python-dotenv",
"loguru",
"loralib",
"wget",
"rich",
"deepdiff",
"multipledispatch",
"plotly",
"pydantic",
"rich-click",
"graphein",
"cpdb-protein",
"MDAnalysis",
"openbabel-wheel",
"backports.tarfile",
"peft",
"trl",
"evaluate",
"tf-keras",
"scikit-image",
"botocore",
"boto3",
"huggingface-hub",
"langchain",
"langchain-community",
"google-cloud-aiplatform",
"pydicom",
]
matchem_requires = [
"emmet-core",
"contextlib2",
"fire",
"pytorch-lightning",
"smact",
"pydantic-settings",
"blake3",
"inflect",
"pubchempy",
"pybtex",
"pymatgen-io-validation",
"python-dotenv",
"torchdata",
"ase",
"pymatgen",
"e3nn",
"matscipy",
"python-hostlist",
"configargparse",
"lmdb",
"orjson",
"ase_db_backends",
"submitit",
"clusterscope",
"huggingface_hub",
"numba",
"opt_einsum-fx",
"torchtnt",
"torchmetrics",
"torch-ema",
"torch-runstats",
"deprecated",
"prettytable",
'pytest',
"cuequivariance",
"pwdata",
"scikit-learn-intelex",
"pwact",
"torch_geometric",
"xtb",
"rdkit",
"loguru",
"pydantic",
"prettytable",
]
# ==========================================
# 3. 构建与安装
# ==========================================
extras = {
"earth": resolve(unique(earth_requires), deps),
"cfd": resolve(unique(cfd_requires), deps),
"bio": resolve(unique(bio_requires), deps),
"matchem": resolve(unique(matchem_requires), deps),
"all": resolve(unique(earth_requires + cfd_requires + bio_requires + matchem_requires), deps),
}
setup(
name="onescience",
version="0.3.0",
author="sugon-ai4s",
author_email="ai4s@sugon.com",
description="First release",
long_description="OneScience is a scientific computing toolkit built on an advanced deep learning framework",
url="https://github.com/onescience-ai/OneScience",
python_requires=">=3.10.0",
package_dir={
"": "src",
"cli": "cli/cli",
"cli.commands": "cli/cli/commands",
"cli.core": "cli/cli/core",
},
packages=find_packages("src") + ["cli", "cli.commands", "cli.core"],
install_requires=resolve(unique(core_requires), deps),
extras_require=extras,
include_package_data=True,
package_data=discover_package_data(),
ext_modules=discover_extensions(),
zip_safe=False,
entry_points={
"console_scripts": [
"onescience=cli.main:cli",
],
},
cmdclass={
"build_py": build_py,
"develop": develop,
},
)