-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_protected.py
More file actions
279 lines (227 loc) · 8.57 KB
/
build_protected.py
File metadata and controls
279 lines (227 loc) · 8.57 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
"""
Synapse Protected Build — Cython compilation for IP-sensitive modules.
Compiles 7 HIGH IP modules to .pyd (Windows) / .so (Linux) native extensions.
Source is unrecoverable from compiled output.
Usage:
python build_protected.py # Compile in-place (for dev/testing)
python build_protected.py --dist # Build distributable package (no source)
python build_protected.py --clean # Remove all compiled artifacts
The compiled .pyd files are importable identically to .py files.
Python's import system prefers .pyd/.so over .py when both exist.
"""
import os
import sys
import shutil
import argparse
from pathlib import Path
# ============================================================================
# HIGH IP Modules — these get compiled to native extensions
# ============================================================================
PROTECTED_MODULES = [
"python/synapse/routing/router.py",
"python/synapse/routing/cache.py",
"python/synapse/memory/store.py",
"python/synapse/memory/markdown.py",
"python/synapse/agent/executor.py",
"python/synapse/agent/learning.py",
"python/synapse/core/determinism.py",
]
# ============================================================================
# Build
# ============================================================================
ROOT = Path(__file__).parent
def get_ext_modules():
"""Create Cython Extension objects for each protected module."""
from Cython.Build import cythonize
from setuptools import Extension
extensions = []
for mod_path in PROTECTED_MODULES:
full_path = ROOT / mod_path
if not full_path.exists():
print(f" SKIP {mod_path} (not found)")
continue
# Convert file path to dotted module name
# python/synapse/routing/router.py -> synapse.routing.router
parts = Path(mod_path).with_suffix("").parts
# Strip leading "python/" from module name
if parts[0] == "python":
parts = parts[1:]
module_name = ".".join(parts)
extensions.append(
Extension(
module_name,
sources=[str(full_path)],
)
)
return cythonize(
extensions,
compiler_directives={
"language_level": "3",
"boundscheck": False,
"wraparound": False,
},
quiet=False,
)
def build_inplace():
"""Compile protected modules in-place (.pyd next to .py)."""
print("=" * 60)
print("Synapse Protected Build — In-Place Compilation")
print("=" * 60)
print()
print(f"Target: {len(PROTECTED_MODULES)} HIGH IP modules")
print()
# Use setuptools to build extensions in-place
from setuptools import setup, Distribution
ext_modules = get_ext_modules()
if not ext_modules:
print("ERROR: No modules to compile.")
return False
dist = Distribution({
"name": "synapse-protected",
"ext_modules": ext_modules,
"package_dir": {"": "python"},
})
# Build in-place
cmd = dist.get_command_obj("build_ext")
cmd.inplace = True
cmd.ensure_finalized()
try:
cmd.run()
except Exception as e:
print(f"\nBUILD FAILED: {e}")
print("\nEnsure you have a C compiler installed:")
print(" - Windows: Visual Studio Build Tools (MSVC)")
print(" - Linux: gcc / build-essential")
print(" - macOS: Xcode Command Line Tools")
return False
print()
print("=" * 60)
print("BUILD COMPLETE")
print("=" * 60)
# Report what was built
for mod_path in PROTECTED_MODULES:
py_file = ROOT / mod_path
# Look for .pyd (Windows) or .so (Linux/macOS)
stem = py_file.stem
parent = py_file.parent
pyd_files = list(parent.glob(f"{stem}*.pyd")) + list(parent.glob(f"{stem}*.so"))
if pyd_files:
for pyd in pyd_files:
size_kb = pyd.stat().st_size / 1024
print(f" COMPILED {pyd.name} ({size_kb:.0f} KB)")
else:
print(f" MISSING {mod_path}")
return True
def build_dist():
"""Build a distributable package with .pyd replacing .py for protected modules."""
dist_dir = ROOT / "dist" / "synapse-protected"
print("=" * 60)
print("Synapse Protected Build — Distribution Package")
print("=" * 60)
print()
print(f"Output: {dist_dir}")
print()
# Step 1: Compile in-place first
if not build_inplace():
return False
# Step 2: Create dist directory
if dist_dir.exists():
shutil.rmtree(dist_dir)
dist_dir.mkdir(parents=True)
# Step 3: Copy entire python/synapse/ tree
src = ROOT / "python" / "synapse"
dst = dist_dir / "python" / "synapse"
shutil.copytree(src, dst)
# Step 4: Copy package files
for f in ["pyproject.toml", "README.md", "LICENSE"]:
src_file = ROOT / f
if src_file.exists():
shutil.copy2(src_file, dist_dir / f)
# Step 5: Copy assets
assets_src = ROOT / "assets"
if assets_src.exists():
shutil.copytree(assets_src, dist_dir / "assets")
# Step 6: Copy houdini directory
houdini_src = ROOT / "houdini"
if houdini_src.exists():
shutil.copytree(houdini_src, dist_dir / "houdini")
# Step 7: Remove .py source for protected modules (keep .pyd only)
removed = 0
for mod_path in PROTECTED_MODULES:
# mod_path is relative to ROOT: "python/synapse/routing/router.py"
# In dist, it's at: dist_dir / "python/synapse/routing/router.py"
rel = Path(mod_path)
py_file = dist_dir / rel
parent = py_file.parent
stem = py_file.stem
# Check that .pyd/.so exists before removing .py
compiled = list(parent.glob(f"{stem}*.pyd")) + list(parent.glob(f"{stem}*.so"))
if compiled and py_file.exists():
py_file.unlink()
removed += 1
print(f" PROTECTED {rel.name} -> {compiled[0].name}")
elif py_file.exists():
print(f" WARNING {rel.name} has no compiled version, keeping .py")
# Step 8: Remove build artifacts (Cython/MSVC intermediates) from dist
for pattern in ["*.c", "*.lib", "*.exp", "*.obj"]:
for artifact in (dist_dir / "python").rglob(pattern):
artifact.unlink()
print()
print("=" * 60)
print(f"DISTRIBUTION READY: {dist_dir}")
print(f" {removed} modules compiled (source removed)")
print(f" {len(PROTECTED_MODULES) - removed} modules kept as source")
print("=" * 60)
return True
def clean():
"""Remove all compiled artifacts."""
print("Cleaning compiled artifacts...")
removed = 0
for mod_path in PROTECTED_MODULES:
py_file = ROOT / mod_path
parent = py_file.parent
stem = py_file.stem
# Remove .pyd, .so, .c, .lib, .exp, .obj files
for pattern in [f"{stem}*.pyd", f"{stem}*.so", f"{stem}.c",
f"{stem}*.lib", f"{stem}*.exp", f"{stem}*.obj"]:
for f in parent.glob(pattern):
f.unlink()
print(f" REMOVED {f.relative_to(ROOT)}")
removed += 1
# Remove build/ directory
build_dir = ROOT / "build"
if build_dir.exists():
shutil.rmtree(build_dir)
print(" REMOVED build/")
removed += 1
# Remove dist/synapse-protected/
dist_dir = ROOT / "dist" / "synapse-protected"
if dist_dir.exists():
shutil.rmtree(dist_dir)
print(" REMOVED dist/synapse-protected/")
removed += 1
print(f"\nCleaned {removed} artifacts.")
# ============================================================================
# CLI
# ============================================================================
def main():
parser = argparse.ArgumentParser(
description="Synapse Protected Build — Cython compilation for IP-sensitive modules"
)
parser.add_argument(
"--dist", action="store_true",
help="Build distributable package (compiled .pyd, no source for protected modules)"
)
parser.add_argument(
"--clean", action="store_true",
help="Remove all compiled artifacts"
)
args = parser.parse_args()
if args.clean:
clean()
elif args.dist:
build_dist()
else:
build_inplace()
if __name__ == "__main__":
main()