mirror of
https://github.com/agentscope-ai/ReMe.git
synced 2026-11-27 23:58:47 +00:00
Some checks failed
CI and Release / Docker / Build and test / amd64 (push) Has been cancelled
CI and Release / Docker / Build and test / arm64 (push) Has been cancelled
Security / CodeQL / Analyze python (push) Has been cancelled
CI / Python quality / Pre-commit (push) Has been cancelled
CI / Python tests / Unit Tests - py3.12 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.13 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.14 (push) Has been cancelled
CI / Python packages / Build and verify distributions (push) Has been cancelled
CI / Python quality / GitHub Actions (push) Has been cancelled
CI / Python tests / Hermes embedded - ubuntu-latest (push) Has been cancelled
CI / Python tests / Hermes embedded - windows-latest (push) Has been cancelled
CI / Python tests / Unit Tests - py3.11 (push) Has been cancelled
CI / ReMe Studio / Studio checks (push) Has been cancelled
Deploy / Documentation / Build documentation (push) Has been cancelled
CI / Windows / CLI smoke - py3.11 (push) Has been cancelled
Security / CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI and Release / Docker / Publish multi-platform tags (push) Has been cancelled
Deploy / Documentation / deploy (push) Has been cancelled
* refactor(core): reorganize the ReMe runtime and shared helpers * test(status): import the base component from its canonical module * test: remove redundant unit checks * refactor(core): use canonical runtime imports and shared CLI discovery Restore evolve behavior to the base branch and update its imports. Use Application as the runtime entry point and encode SSE in the HTTP service. * fix(cli): honor caller config when service replay is incomplete Use partial startup overrides for port probes and resolve named configs before requesting the service process working directory.
110 lines
3.8 KiB
Python
110 lines
3.8 KiB
Python
"""Shared directory rules and file snapshots for watchers and catalogs."""
|
|
|
|
from dataclasses import dataclass, field
|
|
from pathlib import Path
|
|
from typing import TYPE_CHECKING
|
|
|
|
from watchfiles import Change
|
|
|
|
from .paths import normalize_posix_path
|
|
|
|
if TYPE_CHECKING:
|
|
from ...schema import ApplicationConfig
|
|
|
|
|
|
def normalize_change(raw: object) -> Change | None:
|
|
"""Accept a watchfiles change or its name from a batch payload."""
|
|
if isinstance(raw, Change):
|
|
return raw
|
|
if isinstance(raw, str):
|
|
return Change.__members__.get(raw)
|
|
return None
|
|
|
|
|
|
@dataclass
|
|
class WatchRule:
|
|
"""A single directory-monitoring rule."""
|
|
|
|
path: Path
|
|
suffixes: list[str] = field(default_factory=list)
|
|
filenames: list[str] = field(default_factory=list)
|
|
|
|
|
|
def build_watch_rules(
|
|
app_config: "ApplicationConfig",
|
|
workspace_path: Path,
|
|
*,
|
|
watch_dirs: list[str],
|
|
watch_suffixes: list[str],
|
|
watch_filenames: list[str] | None = None,
|
|
) -> list[WatchRule]:
|
|
"""Build shared directory, suffix, and exact-filename rules."""
|
|
rules: list[WatchRule] = []
|
|
for dir_field in watch_dirs:
|
|
literal_path = Path(dir_field)
|
|
if literal_path.is_absolute():
|
|
rule_path = literal_path
|
|
else:
|
|
config_field, separator, child_path = dir_field.partition("/")
|
|
dir_value = getattr(app_config, config_field, config_field)
|
|
if hasattr(app_config, config_field) and dir_value in (None, ""):
|
|
dir_value = getattr(type(app_config)(), config_field)
|
|
if separator:
|
|
dir_value = normalize_posix_path(f"{dir_value}/{child_path}")
|
|
dir_name = Path(dir_value)
|
|
rule_path = dir_name if dir_name.is_absolute() else workspace_path / dir_name
|
|
rules.append(WatchRule(path=rule_path, suffixes=list(watch_suffixes), filenames=list(watch_filenames or [])))
|
|
return rules
|
|
|
|
|
|
def collect_existing(rules: list[WatchRule], recursive: bool) -> dict[str, float]:
|
|
"""Walk rule paths and return {abs_path: st_mtime} for matching files."""
|
|
existing: dict[str, float] = {}
|
|
for rule in rules:
|
|
if not rule.path.exists():
|
|
continue
|
|
candidates = rule.path.rglob("*") if recursive else rule.path.iterdir()
|
|
for p in candidates:
|
|
if not p.is_file():
|
|
continue
|
|
if not _match_rule(p, rule):
|
|
continue
|
|
abs_p = p.absolute()
|
|
existing[str(abs_p)] = abs_p.stat().st_mtime
|
|
return existing
|
|
|
|
|
|
def match_file(file_path: str, rules: list[WatchRule]) -> bool:
|
|
"""Return True if a file path matches any of the watch rules."""
|
|
p = Path(file_path)
|
|
for rule in rules:
|
|
try:
|
|
p.relative_to(rule.path)
|
|
except ValueError:
|
|
continue
|
|
if _match_rule(p, rule):
|
|
return True
|
|
return False
|
|
|
|
|
|
def _match_rule(p: Path, rule: WatchRule) -> bool:
|
|
"""Require both configured filters; filenames match exactly."""
|
|
if rule.filenames and p.name not in rule.filenames:
|
|
return False
|
|
filename = p.name.casefold()
|
|
if rule.suffixes and not any(filename.endswith("." + suffix.strip(".").casefold()) for suffix in rule.suffixes):
|
|
return False
|
|
return True
|
|
|
|
|
|
def diff_files(existing: dict[str, float], previous: dict[str, float]) -> tuple[list[dict], dict[str, int]]:
|
|
"""Compare path/mtime snapshots without changing either one."""
|
|
groups = {
|
|
"added": sorted(existing.keys() - previous.keys()),
|
|
"modified": sorted(path for path in existing.keys() & previous.keys() if existing[path] != previous[path]),
|
|
"deleted": sorted(previous.keys() - existing.keys()),
|
|
}
|
|
return (
|
|
[{"change": change, "path": path} for change, paths in groups.items() for path in paths],
|
|
{change: len(paths) for change, paths in groups.items()},
|
|
)
|