106 """Load a baseline from disk. Returns ``None`` on missing/corrupt file."""
107 if not baseline_path.is_file():
110 raw = json.loads(baseline_path.read_text(encoding=
"utf-8"))
111 if not isinstance(raw, dict):
113 if raw.get(
"_schema_version") != _SCHEMA_VERSION:
115 entries_raw = raw.get(
"entries", [])
116 if not isinstance(entries_raw, list):
119 BaselineEntry(path=e[
"path"], rule=e[
"rule"], message=e[
"message"])
121 if isinstance(e, dict)
and "path" in e
and "rule" in e
125 oct_version=raw.get(
"oct_version",
""),
126 timestamp=raw.get(
"timestamp",
""),
127 profile=raw.get(
"profile",
""),
129 except (json.JSONDecodeError, OSError, KeyError):
135 baseline: LintBaseline,
136) -> tuple[list[tuple[str, str, str]], list[tuple[str, str, str]]]:
137 """Compare current *results* against *baseline*.
139 Returns ``(new_violations, resolved_violations)`` where each item is
140 ``(path, rule, message)``.
142 A violation is identified by its ``(path, rule)`` pair. If a
143 ``(path, rule)`` appears in current results but not in the baseline,
144 it is "new". If it appears in the baseline but not in current results,
147 baseline_set: set[tuple[str, str]] = set()
148 baseline_msgs: dict[tuple[str, str], str] = {}
149 for entry
in baseline.entries:
150 key = (entry.path, entry.rule)
151 baseline_set.add(key)
152 baseline_msgs[key] = entry.message
154 current_set: set[tuple[str, str]] = set()
155 current_msgs: dict[tuple[str, str], str] = {}
157 detail = r.get(
"json_detail", {})
158 rel_path = detail.get(
"path", r.get(
"rel",
""))
159 checks = detail.get(
"checks", {})
160 for rule_name, check
in checks.items():
161 if not check.get(
"pass",
True):
162 key = (rel_path, rule_name)
164 current_msgs[key] = check.get(
"message",
"")
166 new_keys = current_set - baseline_set
167 resolved_keys = baseline_set - current_set
169 new_violations = sorted(
170 [(p, r, current_msgs[(p, r)])
for p, r
in new_keys],
172 resolved_violations = sorted(
173 [(p, r, baseline_msgs[(p, r)])
for p, r
in resolved_keys],
175 return new_violations, resolved_violations