Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
95 lines (79 loc) · 3.25 KB
/
Copy pathsetup.py
File metadata and controls
95 lines (79 loc) · 3.25 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
"""Require qualified assets in distributions, while editable source setup stays cheap."""
from pathlib import Path
from contextlib import contextmanager
import importlib.util
import shutil
import sys
from setuptools import setup
from setuptools.command.build_py import build_py
from setuptools.command.bdist_wheel import bdist_wheel
from setuptools.command.sdist import sdist
def verify_frontend():
root = Path(__file__).parent
# Same reason as scripts/chat_bundle.py: the contract delegates its digest shape to
# the shared owner, and a source distribution is verified before LoopX is installed.
if str(root) not in sys.path:
sys.path.insert(0, str(root))
spec = importlib.util.spec_from_file_location(
"chat_bundle_contract", root / "loopx/presentation/chat_bundle.py"
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
module.validate_bundle(root / "loopx/web/chat", source_root=root)
@contextmanager
def oar_dependencies():
root = Path(__file__).parent
if not (root / "loopx/control_plane/oar/session_host_server.ts").is_file():
yield lambda _destination: None
return
spec = importlib.util.spec_from_file_location(
"oar_distribution", root / "loopx/hosts/oar_distribution.py"
)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
with module.prepared_oar_bundle(root) as bundle:
yield lambda destination: module.copy_oar_bundle(bundle, Path(destination))
def clear_built_package(directory):
stale = Path(directory) / "loopx"
source = (Path(__file__).parent / "loopx").resolve()
destination = stale.resolve()
if destination.is_relative_to(source) or source.is_relative_to(destination):
raise RuntimeError("build destination must be outside the LoopX source package")
if stale.exists():
shutil.rmtree(stale)
class BuildWithFrontend(build_py):
def run(self):
if self.editable_mode:
super().run()
return
verify_frontend()
with oar_dependencies() as copy_dependencies:
# setuptools copies changed files but does not remove deleted modules or
# package data. Rebuild only our package, including qualified asset history.
clear_built_package(self.build_lib)
super().run()
copy_dependencies(self.build_lib)
class WheelWithFrontend(bdist_wheel):
def run(self):
if not self.skip_build:
# Validate/build before clearing staging, and let setuptools reuse that
# completed command. --skip-build still uses the explicitly selected build.
self.run_command("build")
# Failed or --keep-temp builds can retain a second copy of deleted files.
clear_built_package(self.bdist_dir)
super().run()
class SourceWithFrontend(sdist):
def run(self):
verify_frontend()
super().run()
def make_release_tree(self, base_dir, files):
with oar_dependencies() as copy_dependencies:
super().make_release_tree(base_dir, files)
copy_dependencies(base_dir)
setup(
cmdclass={
"build_py": BuildWithFrontend,
"bdist_wheel": WheelWithFrontend,
"sdist": SourceWithFrontend,
}
)