Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
128 lines (109 loc) · 4.41 KB
/
Copy pathsetup.py
File metadata and controls
128 lines (109 loc) · 4.41 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
# SPDX-License-Identifier: Apache-2.0
"""LMCache setup script — policy-driven extension build.
Uses the strategy pattern so that each platform lives in its own file
under ``setup_extensions/build_profiles/``. Adding a new platform requires
zero changes to this file.
"""
# Standard
from pathlib import Path
from types import ModuleType
import importlib.util
import re
import sys
ROOT_DIR = Path(__file__).parent
# Ensure the project root is importable when ``setup.py`` runs inside a
# PEP 517 build subprocess (where CWD is not added to ``sys.path``).
if str(ROOT_DIR) not in sys.path:
sys.path.insert(0, str(ROOT_DIR))
# Third Party
from setuptools import find_packages, setup # noqa: E402
from setuptools.command.build_py import build_py as _build_py # noqa: E402
from setuptools.command.egg_info import egg_info as _egg_info # noqa: E402
# First Party
from setup_extensions import BuildPolicy, BuildProfile # noqa: E402
def _read_requirements(path: Path) -> list[str]:
if not path.exists():
return []
reqs: list[str] = []
for raw in path.read_text().splitlines():
line = raw.strip()
if line and not line.startswith("#"):
reqs.append(line)
return reqs
def _load_proto_generator() -> ModuleType:
"""Load the proto generator without importing the LMCache package."""
generator_path = (
ROOT_DIR
/ "lmcache"
/ "v1"
/ "transport"
/ "grpc"
/ "_proto_gen"
/ "_generate.py"
)
spec = importlib.util.spec_from_file_location(
"_lmcache_proto_generate", generator_path
)
if spec is None or spec.loader is None:
raise RuntimeError(f"Cannot load gRPC proto generator: {generator_path}")
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
class _BuildPyWithGrpcStubs(_build_py):
"""Generate ignored gRPC bindings before packaging LMCache."""
def run(self) -> None:
distribution_name = self.distribution.get_name().replace("_", "-").lower()
if distribution_name != "lmcache-cli":
generator = _load_proto_generator()
for package in generator.GENERATED_PACKAGES:
generator.generate(package)
super().run()
class _EggInfoWithNixlConnector(_egg_info):
"""Add the native NIXL connector, at this version, to the nixl extra."""
def run(self) -> None:
nixl_extra = self.distribution.extras_require.get("nixl")
version = self.distribution.get_version()
# lmcache-nixl is published only at release versions, so dev builds
# keep the extra to the nixl runtime. The release-version pattern is
# also in stamp_nixl_version.sh. egg_info can run more than once in a
# process, so append only once.
if (
nixl_extra is not None
and re.fullmatch(r"\d+\.\d+\.\d+((a|b|rc)\d+)?(\.post\d+)?", version)
and not any(req.startswith("lmcache-nixl") for req in nixl_extra)
):
nixl_extra.append(f"lmcache-nixl=={version}")
super().run()
if __name__ == "__main__":
policy = BuildPolicy()
profile = policy.resolve_profile()
ext_modules, cmdclass, req_file = policy.collect_extensions(profile)
cmdclass["build_py"] = _BuildPyWithGrpcStubs
cmdclass["egg_info"] = _EggInfoWithNixlConnector
install_requires = _read_requirements(ROOT_DIR / "requirements" / "common.txt")
# Platform-neutral extras; the build profile adds its own below.
extras_require: dict[str, list[str]] = {
"kafka": _read_requirements(ROOT_DIR / "requirements" / "kafka.txt"),
"raw-block": ["lmcache-rust-raw-block-io"],
"grpc-native": _read_requirements(
ROOT_DIR / "requirements" / "grpc_native.txt"
),
}
if (
not BuildProfile.is_gpu_ext_disabled()
and req_file is not None
and profile is not None
):
install_requires += _read_requirements(ROOT_DIR / "requirements" / req_file)
for extra_name, extra_req_file in profile.extras_requirements().items():
extras_require[extra_name] = _read_requirements(
ROOT_DIR / "requirements" / extra_req_file
)
setup(
packages=find_packages(exclude=("csrc",)),
ext_modules=ext_modules,
cmdclass=cmdclass,
include_package_data=True,
install_requires=install_requires,
extras_require=extras_require,
)