File size: 15,858 Bytes
9abace2 | 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 | """
Layer 4: Filesystem Effects
Part of SOVEREIGN PYTHON LLM ENGINE
Explicit filesystem effect handling with async I/O.
All effects are isolated and auditable.
"""
from pathlib import Path
from typing import AsyncIterator
import aiofiles
import aiofiles.os
from datetime import datetime
from ..core.types import ValidatedPath, ValidatedDirectory, ValidatedFile
from ..core.crypto import ContentHash, hash_content
# ==========================================
# File Operations
# ==========================================
class FilesystemRuntime:
"""
Explicit filesystem effect handler.
All operations are async and return explicit results.
No hidden mutations or side effects.
"""
async def read_file(self, path: Path) -> bytes:
"""
Read file contents.
Args:
path: File path
Returns:
File contents as bytes
Raises:
FileNotFoundError: If file doesn't exist
PermissionError: If file is not readable
"""
async with aiofiles.open(path, 'rb') as f:
return await f.read()
async def read_text(self, path: Path, encoding: str = 'utf-8') -> str:
"""
Read file contents as text.
Args:
path: File path
encoding: Text encoding
Returns:
File contents as string
"""
async with aiofiles.open(path, 'r', encoding=encoding) as f:
return await f.read()
async def write_file(self, path: Path, content: bytes) -> None:
"""
Write file contents.
Creates parent directories if they don't exist.
Args:
path: File path
content: Content to write
Raises:
PermissionError: If file is not writable
"""
# Ensure parent directory exists
path.parent.mkdir(parents=True, exist_ok=True)
async with aiofiles.open(path, 'wb') as f:
await f.write(content)
async def write_text(self, path: Path, content: str, encoding: str = 'utf-8') -> None:
"""
Write text file.
Args:
path: File path
content: Text content
encoding: Text encoding
"""
path.parent.mkdir(parents=True, exist_ok=True)
async with aiofiles.open(path, 'w', encoding=encoding) as f:
await f.write(content)
async def append_file(self, path: Path, content: bytes) -> None:
"""
Append to file.
Args:
path: File path
content: Content to append
"""
path.parent.mkdir(parents=True, exist_ok=True)
async with aiofiles.open(path, 'ab') as f:
await f.write(content)
async def append_text(self, path: Path, content: str, encoding: str = 'utf-8') -> None:
"""
Append text to file.
Args:
path: File path
content: Text to append
encoding: Text encoding
"""
path.parent.mkdir(parents=True, exist_ok=True)
async with aiofiles.open(path, 'a', encoding=encoding) as f:
await f.write(content)
async def delete_file(self, path: Path) -> None:
"""
Delete file.
Args:
path: File path
Raises:
FileNotFoundError: If file doesn't exist
"""
await aiofiles.os.remove(path)
async def file_exists(self, path: Path) -> bool:
"""
Check if file exists.
Args:
path: File path
Returns:
True if file exists
"""
return path.exists()
async def get_file_size(self, path: Path) -> int:
"""
Get file size in bytes.
Args:
path: File path
Returns:
File size in bytes
"""
stat = await aiofiles.os.stat(path)
return stat.st_size
async def get_file_hash(self, path: Path) -> ContentHash:
"""
Compute file hash.
Args:
path: File path
Returns:
Blake2b content hash
"""
content = await self.read_file(path)
return hash_content(content)
async def copy_file(self, source: Path, dest: Path) -> None:
"""
Copy file from source to destination.
Args:
source: Source file path
dest: Destination file path
"""
content = await self.read_file(source)
await self.write_file(dest, content)
async def move_file(self, source: Path, dest: Path) -> None:
"""
Move file from source to destination.
Args:
source: Source file path
dest: Destination file path
"""
await self.copy_file(source, dest)
await self.delete_file(source)
# ==========================================
# Directory Operations
# ==========================================
class DirectoryRuntime:
"""Directory-specific operations"""
async def create_directory(self, path: Path, parents: bool = True) -> None:
"""
Create directory.
Args:
path: Directory path
parents: Create parent directories if they don't exist
"""
path.mkdir(parents=parents, exist_ok=True)
async def delete_directory(self, path: Path, recursive: bool = False) -> None:
"""
Delete directory.
Args:
path: Directory path
recursive: Delete recursively (rmtree)
Raises:
OSError: If directory is not empty and recursive=False
"""
if recursive:
import shutil
shutil.rmtree(path)
else:
path.rmdir()
async def list_directory(self, path: Path) -> list[Path]:
"""
List directory contents.
Args:
path: Directory path
Returns:
List of paths in directory
"""
return list(path.iterdir())
async def list_files(self, path: Path, pattern: str = "*") -> list[Path]:
"""
List files matching pattern.
Args:
path: Directory path
pattern: Glob pattern (e.g., "*.py")
Returns:
List of file paths
"""
return [p for p in path.glob(pattern) if p.is_file()]
async def list_directories(self, path: Path) -> list[Path]:
"""
List subdirectories.
Args:
path: Directory path
Returns:
List of subdirectory paths
"""
return [p for p in path.iterdir() if p.is_dir()]
async def walk_directory(self, path: Path, pattern: str = "*") -> AsyncIterator[Path]:
"""
Recursively walk directory.
Args:
path: Root directory path
pattern: Glob pattern to match
Yields:
Matching file paths
"""
for p in path.rglob(pattern):
if p.is_file():
yield p
async def get_directory_size(self, path: Path) -> int:
"""
Get total size of directory (recursive).
Args:
path: Directory path
Returns:
Total size in bytes
"""
total = 0
async for file_path in self.walk_directory(path):
try:
stat = await aiofiles.os.stat(file_path)
total += stat.st_size
except (FileNotFoundError, PermissionError):
continue
return total
async def count_files(self, path: Path, pattern: str = "*") -> int:
"""
Count files in directory.
Args:
path: Directory path
pattern: Glob pattern
Returns:
Number of matching files
"""
count = 0
async for _ in self.walk_directory(path, pattern):
count += 1
return count
# ==========================================
# Path Utilities
# ==========================================
class PathUtils:
"""Path manipulation utilities"""
@staticmethod
def make_absolute(path: Path) -> Path:
"""Convert to absolute path"""
return path.resolve()
@staticmethod
def make_relative(path: Path, base: Path) -> Path:
"""Make path relative to base"""
return path.relative_to(base)
@staticmethod
def normalize_path(path: Path) -> Path:
"""Normalize path (resolve symlinks, remove ..)"""
return path.resolve()
@staticmethod
def get_extension(path: Path) -> str:
"""Get file extension (including dot)"""
return path.suffix
@staticmethod
def get_stem(path: Path) -> str:
"""Get filename without extension"""
return path.stem
@staticmethod
def join_paths(*parts: str | Path) -> Path:
"""Join path components"""
result = Path(parts[0])
for part in parts[1:]:
result = result / part
return result
@staticmethod
def is_subpath(path: Path, parent: Path) -> bool:
"""Check if path is under parent directory"""
try:
path.relative_to(parent)
return True
except ValueError:
return False
# ==========================================
# Temporary File Management
# ==========================================
class TemporaryFileManager:
"""Manage temporary files with automatic cleanup"""
def __init__(self, base_dir: Path | None = None):
"""
Initialize temporary file manager.
Args:
base_dir: Base directory for temp files (None = system temp)
"""
if base_dir is None:
import tempfile
self.base_dir = Path(tempfile.gettempdir())
else:
self.base_dir = base_dir
self.created_files: list[Path] = []
async def create_temp_file(self, suffix: str = "", prefix: str = "tmp") -> Path:
"""
Create temporary file.
Args:
suffix: File suffix (e.g., ".py")
prefix: File prefix
Returns:
Path to temporary file
"""
import tempfile
import asyncio
# Create temp file in thread pool
fd, path_str = await asyncio.to_thread(
tempfile.mkstemp,
suffix=suffix,
prefix=prefix,
dir=self.base_dir
)
# Close file descriptor
import os
os.close(fd)
path = Path(path_str)
self.created_files.append(path)
return path
async def create_temp_directory(self, prefix: str = "tmpdir") -> Path:
"""
Create temporary directory.
Args:
prefix: Directory prefix
Returns:
Path to temporary directory
"""
import tempfile
import asyncio
path_str = await asyncio.to_thread(
tempfile.mkdtemp,
prefix=prefix,
dir=self.base_dir
)
path = Path(path_str)
self.created_files.append(path)
return path
async def cleanup(self) -> None:
"""Delete all created temporary files/directories"""
for path in self.created_files:
try:
if path.is_file():
await aiofiles.os.remove(path)
elif path.is_dir():
import shutil
shutil.rmtree(path)
except (FileNotFoundError, PermissionError):
pass
self.created_files.clear()
async def __aenter__(self):
"""Context manager entry"""
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
"""Context manager exit (auto-cleanup)"""
await self.cleanup()
# ==========================================
# File Watcher (Optional)
# ==========================================
class FileWatcher:
"""
Watch file for changes.
Simple polling-based implementation.
For production, use watchdog library.
"""
def __init__(self, path: Path, poll_interval: float = 1.0):
"""
Initialize file watcher.
Args:
path: File to watch
poll_interval: Polling interval in seconds
"""
self.path = path
self.poll_interval = poll_interval
self._last_mtime: float | None = None
async def has_changed(self) -> bool:
"""
Check if file has changed since last check.
Returns:
True if file was modified
"""
try:
stat = await aiofiles.os.stat(self.path)
current_mtime = stat.st_mtime
if self._last_mtime is None:
self._last_mtime = current_mtime
return False
changed = current_mtime > self._last_mtime
if changed:
self._last_mtime = current_mtime
return changed
except FileNotFoundError:
return False
async def watch_for_changes(self) -> AsyncIterator[datetime]:
"""
Watch for file changes continuously.
Yields:
Timestamp when change was detected
"""
import asyncio
while True:
if await self.has_changed():
yield datetime.now()
await asyncio.sleep(self.poll_interval)
# ==========================================
# Safe File Operations
# ==========================================
class SafeFileOperations:
"""
Safe file operations with automatic backup.
"""
def __init__(self, fs: FilesystemRuntime):
self.fs = fs
async def safe_write(self, path: Path, content: bytes, backup: bool = True) -> None:
"""
Write file with optional backup.
Args:
path: File path
content: Content to write
backup: Create .bak backup if file exists
"""
# Create backup if file exists
if backup and path.exists():
backup_path = path.with_suffix(path.suffix + '.bak')
await self.fs.copy_file(path, backup_path)
# Write new content
try:
await self.fs.write_file(path, content)
except Exception as e:
# Restore backup on failure
if backup and backup_path.exists():
await self.fs.copy_file(backup_path, path)
raise e
async def atomic_write(self, path: Path, content: bytes) -> None:
"""
Atomic write (write to temp, then rename).
Args:
path: Target file path
content: Content to write
"""
import tempfile
import os
# Write to temporary file in same directory
temp_fd, temp_path_str = tempfile.mkstemp(dir=path.parent)
temp_path = Path(temp_path_str)
try:
# Write content
os.write(temp_fd, content)
os.close(temp_fd)
# Atomic rename
temp_path.replace(path)
except Exception:
# Clean up temp file on failure
if temp_path.exists():
temp_path.unlink()
raise
|