-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
617 lines (505 loc) · 21.1 KB
/
__init__.py
File metadata and controls
617 lines (505 loc) · 21.1 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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
from app.source import target
import pathlib
import socket
import subprocess
import time
import sys
import asyncio
import json
import http.server
import tempfile
import site
import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union, Dict, AsyncIterator
from contextlib import asynccontextmanager
from pathlib import Path, PureWindowsPath
import os
import tracemalloc
import logging
import threading
import gc
import weakref
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional, Callable, Set, TypeVar, Union
from contextlib import contextmanager
from functools import wraps
from dataclasses import dataclass
from enum import Enum, auto
from main import (
Element,
Entity,
SerializableEntity,
Attribute,
ConcreteSerialModel
)
# Type variables for generic type hints
T = TypeVar('T')
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('ObsidianSandbox')
IS_POSIX = os.name == 'posix'
IS_WINDOWS = sys.platform.startswith('win')
# Platform-specific optimizations
if IS_WINDOWS:
from ctypes import windll
from ctypes import wintypes
from ctypes.wintypes import HANDLE, DWORD, LPWSTR, LPVOID, BOOL
def set_process_priority(priority: int):
windll.kernel32.SetPriorityClass(wintypes.HANDLE(-1), priority)
elif IS_POSIX:
import resource
def set_process_priority(priority: int):
try:
os.nice(priority)
except PermissionError:
print("Warning: Unable to set process priority. Running with default priority.")
WINDOWS_SANDBOX_DEFAULT_DESKTOP = Path(PureWindowsPath(r'C:\Users\WDAGUtilityAccount\Desktop'))
@dataclass
class SandboxConfig:
mappings: List['FolderMapping']
networking: bool = True
logon_command: str = ""
virtual_gpu: bool = True
def to_wsb_config(self) -> Dict:
"""Generate Windows Sandbox configuration"""
config = {
'MappedFolders': [mapping.to_wsb_config() for mapping in self.mappings],
'LogonCommand': {'Command': self.logon_command} if self.logon_command else None,
'Networking': self.networking,
'vGPU': self.virtual_gpu
}
return config
class SandboxException(Exception):
"""Base exception for sandbox-related errors"""
pass
class ServerNotResponding(SandboxException):
"""Raised when server is not responding"""
pass
@dataclass
class FolderMapping:
"""Represents a folder mapping between host and sandbox"""
host_path: Path
read_only: bool = True
def __post_init__(self):
self.host_path = Path(self.host_path)
if not self.host_path.exists():
raise ValueError(f"Host path does not exist: {self.host_path}")
@property
def sandbox_path(self) -> Path:
"""Get the mapped path inside the sandbox"""
return WINDOWS_SANDBOX_DEFAULT_DESKTOP / self.host_path.name
def to_wsb_config(self) -> Dict:
"""Convert to Windows Sandbox config format"""
return {
'HostFolder': str(self.host_path),
'ReadOnly': self.read_only
}
class PythonUserSiteMapper:
def read_only(self):
return True
"""
Maps the current Python installation's user site packages to the new sandbox.
"""
def site(self):
return pathlib.Path(site.getusersitepackages())
"""
Maps the current Python installation to the new sandbox.
"""
def path(self):
return pathlib.Path(sys.prefix)
class OnlineSession:
"""Manages the network connection to the sandbox"""
def __init__(self, sandbox: 'SandboxEnvironment'):
self.sandbox = sandbox
self.shared_directory = self._get_shared_directory()
self.server_address_path = self.shared_directory / 'server_address'
self.server_address_path_in_sandbox = self._get_sandbox_server_path()
def _get_shared_directory(self) -> Path:
"""Create and return shared directory path"""
shared_dir = Path(tempfile.gettempdir()) / 'obsidian_sandbox_shared'
shared_dir.mkdir(exist_ok=True)
return shared_dir
def _get_sandbox_server_path(self) -> Path:
"""Get the server address path as it appears in the sandbox"""
return WINDOWS_SANDBOX_DEFAULT_DESKTOP / self.shared_directory.name / 'server_address'
def configure_sandbox(self):
"""Configure sandbox for network communication"""
self.sandbox.config.mappings.append(
FolderMapping(self.shared_directory, read_only=False)
)
self._setup_logon_script()
def _setup_logon_script(self):
"""Generate logon script for sandbox initialization"""
commands = []
# Setup Python environment
python_path = sys.executable
sandbox_python_path = WINDOWS_SANDBOX_DEFAULT_DESKTOP / 'Python' / 'python.exe'
commands.append(f'copy "{python_path}" "{sandbox_python_path}"')
# Start server
commands.append(f'{sandbox_python_path} -m http.server 8000')
self.sandbox.config.logon_command = 'cmd.exe /c "{}"'.format(' && '.join(commands))
def connect(self, timeout: int = 60) -> Tuple[str, int]:
"""Establish connection to sandbox"""
if self._wait_for_file(timeout):
address, port = self.server_address_path.read_text().strip().split(':')
if self._verify_connection(address, int(port)):
return address, int(port)
raise ServerNotResponding("Server is not responding")
raise SandboxException("Failed to establish connection")
def _wait_for_file(self, timeout: int) -> bool:
"""Wait for server address file creation"""
end_time = time.time() + timeout
while time.time() < end_time:
if self.server_address_path.exists():
return True
time.sleep(1)
return False
def _verify_connection(self, address: str, port: int) -> bool:
"""Verify network connection to sandbox"""
try:
with socket.create_connection((address, port), timeout=3):
return True
except (socket.error, socket.timeout):
return False
class SandboxEnvironment:
"""Manages the Windows Sandbox environment"""
def __init__(self, config: SandboxConfig):
self.config = config
self._session = OnlineSession(self)
self._connection: Optional[Tuple[str, int]] = None
if config.networking:
self._session.configure_sandbox()
self._connection = self._session.connect()
def run_executable(self, executable_args: List[str], **kwargs) -> subprocess.Popen:
"""Run an executable in the sandbox"""
kwargs.setdefault('stdout', subprocess.PIPE)
kwargs.setdefault('stderr', subprocess.PIPE)
return subprocess.Popen(executable_args, **kwargs)
def shutdown(self):
"""Safely shutdown the sandbox"""
try:
self.run_executable(['shutdown.exe', '/s', '/t', '0'])
except Exception as e:
logger.error(f"Failed to shutdown sandbox: {e}")
raise SandboxException("Shutdown failed")
class SandboxCommServer:
"""Manages communication with the sandbox environment"""
def __init__(self, shared_dir: Path):
self.shared_dir = shared_dir
self.server: Optional[http.server.HTTPServer] = None
self._port = self._find_free_port()
@staticmethod
def _find_free_port() -> int:
"""Find an available port for the server"""
with socket.socket() as s:
s.bind(('', 0))
return s.getsockname()[1]
async def start(self):
"""Start the communication server"""
class Handler(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
data = self.rfile.read(content_length)
# Process incoming messages from sandbox
logger.info(f"Received from sandbox: {data.decode()}")
self.send_response(200)
self.end_headers()
self.server = http.server.HTTPServer(('localhost', self._port), Handler)
# Write server info for sandbox
server_info = {'host': 'localhost', 'port': self._port}
server_info_path = self.shared_dir / 'server_info.json'
server_info_path.write_text(json.dumps(server_info))
# Run server in background
await asyncio.get_event_loop().run_in_executor(
None, self.server.serve_forever
)
def stop(self):
"""Stop the communication server"""
if self.server:
self.server.shutdown()
self.server = None
class SandboxManager:
"""Manages Windows Sandbox lifecycle and communication"""
def __init__(self, config: SandboxConfig):
self.config = config
self.shared_dir = Path(tempfile.gettempdir()) / 'sandbox_shared'
self.shared_dir.mkdir(exist_ok=True)
# Add shared directory to mappings
self.config.mappings.append(
FolderMapping(self.shared_dir, read_only=False)
)
self.comm_server = SandboxCommServer(self.shared_dir)
self._process: Optional[subprocess.Popen] = None
async def _setup_sandbox(self):
"""Generate WSB file and prepare sandbox environment"""
wsb_config = self.config.to_wsb_config()
wsb_path = self.shared_dir / 'config.wsb'
wsb_path.write_text(json.dumps(wsb_config, indent=2))
# Start communication server
await self.comm_server.start()
# Launch sandbox
self._process = subprocess.Popen(
['WindowsSandbox.exe', str(wsb_path)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
async def _cleanup(self):
"""Clean up sandbox resources"""
self.comm_server.stop()
if self._process:
self._process.terminate()
await asyncio.get_event_loop().run_in_executor(
None, self._process.wait
)
@asynccontextmanager
async def session(self) -> AsyncIterator['SandboxManager']:
"""Context manager for sandbox session"""
try:
await self._setup_sandbox()
yield self
finally:
await self._cleanup()
class MemoryTraceLevel(Enum):
"""Granularity levels for memory tracing."""
BASIC = auto() # Basic memory usage
DETAILED = auto() # Include stack traces
FULL = auto() # Include object references
@dataclass
class MemoryStats:
"""Container for memory statistics with analysis capabilities."""
size: int
count: int
traceback: str
timestamp: float
peak_memory: int
def to_dict(self) -> Dict:
return {
'size': self.size,
'count': self.count,
'traceback': self.traceback,
'timestamp': self.timestamp,
'peak_memory': self.peak_memory
}
class CustomFormatter(logging.Formatter):
"""Custom formatter for color-coded log levels."""
COLORS = {
logging.DEBUG: "\x1b[38;20m",
logging.INFO: "\x1b[32;20m",
logging.WARNING: "\x1b[33;20m",
logging.ERROR: "\x1b[31;20m",
logging.CRITICAL: "\x1b[31;1m"
}
RESET = "\x1b[0m"
def format(self, record: logging.LogRecord) -> str:
color = self.COLORS.get(record.levelno, self.COLORS[logging.DEBUG])
record.msg = f"{color}{record.msg}{self.RESET}"
return super().format(record)
class MemoryTracker:
"""Singleton memory tracking manager with enhanced logging."""
_instance = None
_lock = threading.Lock()
_trace_filter = {"<frozen importlib._bootstrap>", "<frozen importlib._bootstrap_external>"}
def __new__(cls):
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
cls._instance._initialize()
return cls._instance
def _initialize(self):
"""Initialize the memory tracker with logging and storage."""
self._setup_logging()
self._snapshots: Dict[str, List[MemoryStats]] = {}
self._tracked_objects = weakref.WeakSet()
self._trace_level = MemoryTraceLevel.DETAILED
# Start tracemalloc if not already running
if not tracemalloc.is_tracing():
tracemalloc.start()
def _setup_logging(self):
"""Configure logging with custom formatter."""
self.logger = logging.getLogger("MemoryTracker")
self.logger.setLevel(logging.DEBUG)
# Console handler with color formatting
console_handler = logging.StreamHandler()
console_handler.setFormatter(CustomFormatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
))
self.logger.addHandler(console_handler)
# File handler for persistent logging
try:
file_handler = logging.FileHandler("memory_tracker.log")
file_handler.setFormatter(logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
))
self.logger.addHandler(file_handler)
except (PermissionError, IOError) as e:
self.logger.warning(f"Could not create log file: {e}")
def trace_memory(level: MemoryTraceLevel = MemoryTraceLevel.DETAILED):
"""Enhanced decorator for memory tracking with configurable detail level."""
def decorator(method: Callable) -> Callable:
@wraps(method)
def wrapper(self: Any, *args: Any, **kwargs: Any) -> Any:
tracker = MemoryTracker()
# Force garbage collection for accurate measurement
gc.collect()
# Take initial snapshot
snapshot_before = tracemalloc.take_snapshot()
try:
result = method(self, *args, **kwargs)
# Take final snapshot and compute statistics
snapshot_after = tracemalloc.take_snapshot()
stats = snapshot_after.compare_to(snapshot_before, 'lineno')
# Filter and process statistics
filtered_stats = [
stat for stat in stats
if not any(f in str(stat.traceback) for f in tracker._trace_filter)
]
# Log based on trace level
if level in (MemoryTraceLevel.DETAILED, MemoryTraceLevel.FULL):
for stat in filtered_stats[:5]:
tracker.logger.info(
f"Memory change in {method.__name__}: "
f"+{stat.size_diff/1024:.1f} KB at:\n{stat.traceback}"
)
return result
finally:
# Cleanup
del snapshot_before
gc.collect()
return wrapper
return decorator
class MemoryTrackedABC(ABC):
"""Abstract base class for memory-tracked classes with enhanced features."""
def __init__(self):
self._tracker = MemoryTracker()
self._tracker._tracked_objects.add(self)
def __init_subclass__(cls):
super().__init_subclass__()
# Store original methods for introspection
cls._original_methods = {}
# Automatically decorate public methods
for attr_name, attr_value in cls.__dict__.items():
if (callable(attr_value) and
not attr_name.startswith('_') and
not getattr(attr_value, '_skip_trace', False)):
cls._original_methods[attr_name] = attr_value
setattr(cls, attr_name, trace_memory()(attr_value))
@staticmethod
def skip_trace(method: Callable) -> Callable:
"""Decorator to exclude a method from memory tracking."""
method._skip_trace = True
return method
@classmethod
@contextmanager
def trace_section(cls, section_name: str, level: MemoryTraceLevel = MemoryTraceLevel.DETAILED):
"""Context manager for tracking memory usage in specific code sections."""
tracker = MemoryTracker()
gc.collect()
snapshot_before = tracemalloc.take_snapshot()
try:
yield
finally:
snapshot_after = tracemalloc.take_snapshot()
stats = snapshot_after.compare_to(snapshot_before, 'lineno')
filtered_stats = [
stat for stat in stats
if not any(f in str(stat.traceback) for f in tracker._trace_filter)
]
if level != MemoryTraceLevel.BASIC:
tracker.logger.info(f"\nMemory usage for section '{section_name}':")
for stat in filtered_stats[:5]:
tracker.logger.info(f"{stat}")
del snapshot_before
gc.collect()
def display_top(snapshot, key_type='lineno', limit=3):
snapshot = snapshot.filter_traces((
tracemalloc.Filter(True, "<module>"),
))
top_stats = snapshot.statistics(key_type)
print("Top %s lines" % limit)
for index, stat in enumerate(top_stats[:limit], 1):
frame = stat.traceback[0]
print("#%s: %s:%s: %.1f KiB"
% (index, frame.filename, frame.lineno, stat.size / 1024))
line = linecache.getline(frame.filename, frame.lineno).strip()
if line:
print(' %s' % line)
other = top_stats[limit:]
if other:
size = sum(stat.size for stat in other)
print("%s other: %.1f KiB" % (len(other), size / 1024))
total = sum(stat.size for stat in top_stats)
print("Total allocated size: %.1f KiB" % (total / 1024))
class DebuggerMixin:
"""Mixin for debugging memory-tracked classes."""
def __init__(self):
self._tracker = MemoryTracker()
self._tracker._tracked_objects.add(self)
def __init_subclass__(cls):
super().__init_subclass__()
# Store original methods for introspection
cls._original_methods = {}
# Automatically decorate public methods
for attr_name, attr_value in cls.__dict__.items():
if (callable(attr_value) and
not attr_name.startswith('_') and
not getattr(attr_value, '_skip_trace', False)):
cls._original_methods[attr_name] = attr_value
setattr(cls, attr_name, trace_memory()(attr_value))
@staticmethod
def skip_trace(method: Callable) -> Callable:
"""Decorator to exclude a method from memory tracking."""
method._skip_trace = True
return method
@classmethod
@contextmanager
def trace_section(cls, section_name: str, level: MemoryTraceLevel = MemoryTraceLevel.DETAILED):
"""Context manager for tracking memory usage in specific code sections."""
tracker = MemoryTracker()
def main():
class MyTrackedClass(MemoryTrackedABC):
def tracked_method(self):
"""This method will be automatically tracked with detailed memory info."""
large_list = [i for i in range(1000000)]
return sum(large_list)
@MemoryTrackedABC.skip_trace
def untracked_method(self):
"""This method will not be tracked."""
return "Not tracked"
def tracked_with_section(self):
"""Example of using trace_section with different detail levels."""
with self.trace_section("initialization", MemoryTraceLevel.BASIC):
result = []
with self.trace_section("processing", MemoryTraceLevel.DETAILED):
result.extend(i * 2 for i in range(500000))
with self.trace_section("cleanup", MemoryTraceLevel.FULL):
result.clear()
return len(result)
@classmethod
def introspect_methods(cls):
"""Introspect and display tracked methods with their original implementations."""
for method_name, original_method in cls._original_methods.items():
print(f"Method: {method_name}")
print(f"Original implementation: {original_method}")
print("---")
return MyTrackedClass()
return MyTrackedClass()
if __name__ == "__main__":
tracker = MemoryTracker()
tracker.logger.setLevel(logging.DEBUG)
tracker.logger.addHandler(logging.StreamHandler())
tracker.logger.addHandler(logging.FileHandler("memory_tracker.log"))
my_instance = main()
my_instance.__class__.introspect_methods()
MyTrackedClass = main().__class__
MyTrackedClass.introspect_methods()
# Basic usage
obj = MyTrackedClass()
obj.tracked_method() # Automatically tracked with detailed info
# Custom section tracking with different detail levels
obj.tracked_with_section()
# Customize tracking level for specific methods
@trace_memory(level=MemoryTraceLevel.FULL)
def custom_tracked_method(self):
pass