tenseleyflow/sway / 72f11d4

Browse files

integrations/dlm/autogen: cwd-relative dlm_source when applicable (F09)

Authored by espadonne
SHA
72f11d458ab5f56a898e444184591f8fa20ac5a6
Parents
d243f7f
Tree
40896c1

1 changed file

StatusFile+-
M src/dlm_sway/integrations/dlm/autogen.py 25 1
src/dlm_sway/integrations/dlm/autogen.pymodified
@@ -102,10 +102,34 @@ def write_sway_yaml(dlm_path: Path, out: Path) -> None:
102
             f"{dlm_path}: no trained adapter found at ~/.dlm/store/{handle.dlm_id}/adapter; "
102
             f"{dlm_path}: no trained adapter found at ~/.dlm/store/{handle.dlm_id}/adapter; "
103
             "train the document with `dlm train` before generating a sway suite."
103
             "train the document with `dlm train` before generating a sway suite."
104
         )
104
         )
105
-    spec = build_spec_dict(handle, dlm_source=str(dlm_path.resolve()))
105
+    spec = build_spec_dict(handle, dlm_source=_portable_dlm_source(dlm_path))
106
     out.write_text(_render_annotated_yaml(spec, handle, dlm_path), encoding="utf-8")
106
     out.write_text(_render_annotated_yaml(spec, handle, dlm_path), encoding="utf-8")
107
 
107
 
108
 
108
 
109
+def _portable_dlm_source(dlm_path: Path) -> str:
110
+    """Return a ``dlm_source`` string that survives cross-machine checkout.
111
+
112
+    F09 (Audit 03) — the pre-fix code unconditionally wrote an
113
+    absolute path (``/Users/mfwolffe/.../fortran.dlm``) which breaks
114
+    when the autogen'd ``sway.yaml`` is committed to a repo and
115
+    re-run from a different working tree (CI agents, another dev's
116
+    checkout). The cwd-relative form is round-trippable across
117
+    machines; only fall back to absolute when the ``.dlm`` lives
118
+    outside the cwd (e.g. a global user dir) where relativization
119
+    doesn't resolve on a fresh checkout.
120
+    """
121
+    abs_path = dlm_path.resolve()
122
+    cwd = Path.cwd().resolve()
123
+    try:
124
+        # ``is_relative_to`` lands in 3.9+; this path is guaranteed
125
+        # to exist because sway requires ``>=3.11``.
126
+        if abs_path.is_relative_to(cwd):
127
+            return str(abs_path.relative_to(cwd))
128
+    except ValueError:
129
+        pass
130
+    return str(abs_path)
131
+
132
+
109
 def _render_annotated_yaml(spec: dict[str, Any], handle: DlmHandle, dlm_path: Path) -> str:
133
 def _render_annotated_yaml(spec: dict[str, Any], handle: DlmHandle, dlm_path: Path) -> str:
110
     """Render the spec as YAML with a provenance header + per-probe intent lines (D5).
134
     """Render the spec as YAML with a provenance header + per-probe intent lines (D5).
111
 
135