fix: keep Langflow flow names in sync files, write them world-readable
This commit is contained in:
parent
95a1c1afb7
commit
3ee997cb72
8 changed files with 58 additions and 769 deletions
|
|
@ -16,10 +16,17 @@ from pathlib import Path
|
|||
FLOWS_GLOB = "*.json"
|
||||
|
||||
|
||||
def slugify(name: str) -> str:
|
||||
"""Map a flow name to a safe filename stem (lowercase, [a-z0-9-])."""
|
||||
slug = re.sub(r"[^a-z0-9]+", "-", name.lower()).strip("-")
|
||||
return slug or "flow"
|
||||
def filename_stem(name: str) -> str:
|
||||
"""Map a flow name to a filename stem, keeping the original name.
|
||||
|
||||
Only filesystem-reserved characters (``/ \\ : * ? " < > |`` and control
|
||||
chars) become ``-``; leading/trailing whitespace and trailing dots are
|
||||
stripped (Windows). Case, spaces, underscores and umlauts are preserved:
|
||||
``test_flow`` stays ``test_flow``.
|
||||
"""
|
||||
stem = re.sub(r'[\\/:*?"<>|\x00-\x1f]', "-", name).strip()
|
||||
stem = stem.rstrip(".")
|
||||
return stem or "flow"
|
||||
|
||||
|
||||
def canonical_bytes(doc: dict) -> bytes:
|
||||
|
|
@ -70,7 +77,12 @@ def find_reuse_path(flows_dir: str | Path, doc: dict) -> tuple[Path | None, bool
|
|||
|
||||
|
||||
def write_flow_bytes(target: Path, doc: dict) -> Path:
|
||||
"""Write a flow document to an explicit path atomically (tmp + rename)."""
|
||||
"""Write a flow document to an explicit path atomically (tmp + rename).
|
||||
|
||||
The file ends up world-readable (0o644): mkstemp creates 0o600, which
|
||||
would lock out the other containers (Langflow/LFX run as different
|
||||
users and crash on unreadable seed files).
|
||||
"""
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
fd, tmp_name = tempfile.mkstemp(
|
||||
dir=str(target.parent), prefix=f".{target.stem}-", suffix=".tmp"
|
||||
|
|
@ -79,6 +91,7 @@ def write_flow_bytes(target: Path, doc: dict) -> Path:
|
|||
with os.fdopen(fd, "wb") as tmp:
|
||||
tmp.write(canonical_bytes(doc))
|
||||
os.replace(tmp_name, target)
|
||||
os.chmod(target, 0o644)
|
||||
except BaseException:
|
||||
try:
|
||||
os.unlink(tmp_name)
|
||||
|
|
@ -96,7 +109,7 @@ def write_flow_file(flows_dir: str | Path, doc: dict, *, name: str | None = None
|
|||
"""
|
||||
flows_dir = Path(flows_dir)
|
||||
flows_dir.mkdir(parents=True, exist_ok=True)
|
||||
stem = slugify(name or doc.get("name") or "flow")
|
||||
stem = filename_stem(name or doc.get("name") or "flow")
|
||||
return write_flow_bytes(flow_path(flows_dir, stem), doc)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue