@@ -757,12 +757,51 @@ def fetch_ids(label, query):
757757 print (f" Retrieved { len (ids )} IDs for { label } " )
758758 return ids
759759
760+ _VERSION_SUFFIX = re .compile (r"_v(\d+)\.md$" )
761+
762+
763+ def prune_old_versions (label ):
764+ """Delete superseded term pages in the current directory.
765+
766+ process_term() used to remove only version-1 after writing a page. A term
767+ absent from a single run's ID list therefore kept its older file forever:
768+ the corpus still held _v6 pages long after version reached 9. Those orphans
769+ are live pages -- they can collide on URL with the current version, they
770+ are walked and parsed on every build, and pages old enough to predate the
771+ move off Docsy call shortcodes the theme no longer ships, which aborts the
772+ whole build.
773+
774+ One directory scan rather than a stat per term per version. The per-term
775+ form costs version-1 extra syscalls for every term, which at ~763k terms is
776+ millions of round trips; on the NFS volume this corpus lives on, metadata
777+ operations run at a few hundred per second, so that is hours. This also
778+ removes one stat per term relative to the old code.
779+ """
780+ removed = 0
781+ try :
782+ with os .scandir ("." ) as entries :
783+ for entry in entries :
784+ match = _VERSION_SUFFIX .search (entry .name )
785+ if match and int (match .group (1 )) < version :
786+ try :
787+ os .remove (entry .name )
788+ removed += 1
789+ except OSError :
790+ pass
791+ except OSError as e :
792+ print (f"WARNING: could not prune { label } : { e } " )
793+ return
794+ if removed :
795+ print (f" pruned { removed } superseded pages from { label } " )
796+
797+
760798def process_group (base_path , relative_dir , label , query ):
761799 """Change directory, fetch IDs, and generate pages for one ontology group."""
762800 target_dir = os .path .normpath (join (base_path , relative_dir ))
763801 print (f"\n [{ label } ] { target_dir } " )
764802 chdir (target_dir )
765803 save_terms (fetch_ids (label , query ))
804+ prune_old_versions (label )
766805
767806def process_term (term_id ):
768807 """Fetch, render and write one term page. Returns a status string.
@@ -786,13 +825,6 @@ def process_term(term_id):
786825 f .write (page_content )
787826 os .replace (tmp , filename )
788827
789- # Clean up previous version
790- old_filename = term_id + "_v" + str (version - 1 ) + ".md"
791- if os .path .isfile (old_filename ):
792- try :
793- os .remove (old_filename )
794- except OSError :
795- pass
796828 return "ok"
797829
798830def save_terms (ids ):
0 commit comments