Skip to content

Commit 21dc0e6

Browse files
committed
docs: system reference generator cache file handling improved
1 parent a19b822 commit 21dc0e6

File tree

1 file changed

+30
-13
lines changed

1 file changed

+30
-13
lines changed

scripts/systems_reference.py

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,9 @@ def update_mkdocs_config(self, mkdocs_path: Path):
19041904
def _build_systems_reference_yaml(self):
19051905
"""Build the YAML text for Systems Reference section"""
19061906
lines = [" - Systems Reference:\n"]
1907-
lines.append(" - Systems Reference: reference/systems_reference/index.md\n")
1907+
lines.append(
1908+
" - Systems Reference: reference/systems_reference/index.md\n"
1909+
)
19081910
lines.append(" - Systems:\n")
19091911

19101912
for namespace in sorted(self.parser.systems.keys()):
@@ -4262,45 +4264,60 @@ def compute_source_hash(source_root: Path) -> str:
42624264
return hasher.hexdigest()
42634265

42644266

4265-
def should_regenerate(source_root: Path, cache_file: Path) -> bool:
4266-
"""Check if regeneration is needed based on source file changes"""
4267+
def should_regenerate(source_root: Path, cache_file: Path) -> tuple[bool, str]:
4268+
"""Check if regeneration is needed based on source file changes.
4269+
4270+
Returns a tuple of (needs_regeneration, current_hash) so the caller can
4271+
reuse the hash that was computed before processing started, rather than
4272+
re-hashing after processing finishes (which could capture intermediate
4273+
file changes made while the script was running).
4274+
"""
4275+
current_hash = compute_source_hash(source_root)
4276+
42674277
if not cache_file.exists():
4268-
return True
4278+
return True, current_hash
42694279

42704280
try:
42714281
with open(cache_file, "r") as f:
42724282
cache_data = json.load(f)
42734283
cached_hash = cache_data.get("source_hash", "")
42744284
except (json.JSONDecodeError, OSError):
4275-
return True
4285+
return True, current_hash
4286+
4287+
return current_hash != cached_hash, current_hash
42764288

4277-
current_hash = compute_source_hash(source_root)
4278-
return current_hash != cached_hash
42794289

4290+
def save_cache(cache_file: Path, source_hash: str):
4291+
"""Save the given source hash to cache.
42804292
4281-
def save_cache(source_root: Path, cache_file: Path):
4282-
"""Save current source hash to cache"""
4293+
The hash passed in must be the one computed *before* processing started so
4294+
that any files modified while the script was running don't get silently
4295+
marked as up-to-date on the next invocation.
4296+
"""
42834297
cache_file.parent.mkdir(parents=True, exist_ok=True)
4284-
current_hash = compute_source_hash(source_root)
42854298

42864299
with open(cache_file, "w") as f:
4287-
json.dump({"source_hash": current_hash}, f, indent=2)
4300+
json.dump({"source_hash": source_hash}, f, indent=2)
42884301
f.write("\n") # Add trailing newline for pre-commit
42894302

42904303

42914304
def generate_if_needed(source_root: Path, force: bool = False) -> int:
42924305
"""Generate documentation only if sources changed or forced"""
42934306
cache_file = source_root / "docs/reference/systems_reference/.cache.json"
42944307

4295-
if not force and not should_regenerate(source_root, cache_file):
4308+
# Compute the hash *before* processing so that any files modified while
4309+
# the script is running are not silently treated as already processed.
4310+
needs_regen, pre_run_hash = should_regenerate(source_root, cache_file)
4311+
4312+
if not force and not needs_regen:
42964313
print("Systems reference documentation is up to date (sources unchanged)")
42974314
return 0
42984315

42994316
print("Generating systems reference documentation...")
43004317
result = main()
43014318

43024319
if result == 0:
4303-
save_cache(source_root, cache_file)
4320+
save_cache(cache_file, pre_run_hash)
43044321

43054322
return result
43064323

0 commit comments

Comments
 (0)