From b35000b6cd5942fada1280038da67546bb11e009 Mon Sep 17 00:00:00 2001 From: Jeppe Pedersen Date: Wed, 4 Dec 2024 10:02:43 +0100 Subject: [PATCH 1/2] Only rewrite files which have changes - this speeds up rebuilds as sphinx relies on mtime as an indicator for rebuilding --- src/sphinx_tags/__init__.py | 46 +++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/src/sphinx_tags/__init__.py b/src/sphinx_tags/__init__.py index 6c9ca5c..30257c1 100644 --- a/src/sphinx_tags/__init__.py +++ b/src/sphinx_tags/__init__.py @@ -20,6 +20,22 @@ logger = getLogger("sphinx-tags") +def _update_file_content(filepath: Path, content: str) -> None: + """Update content of file only if it has changed + """ + + if os.path.exists(filepath): + with open(filepath, 'r', encoding='utf8') as f: + old_content = f.read() + if old_content == content: + # File is already up to date + return + + # Override content + with open(filepath, "w", encoding="utf8") as f: + f.write(content) + + class TagLinks(SphinxDirective): """Custom directive for adding tags to Sphinx-generated files. @@ -230,10 +246,8 @@ def create_file( content.append(f" ../{path}") content.append("") - with open( - os.path.join(srcdir, tags_output_dir, filename), "w", encoding="utf8" - ) as f: - f.write("\n".join(content)) + + _update_file_content(Path(srcdir) / tags_output_dir / filename, "\n".join(content)) class Entry: @@ -338,7 +352,7 @@ def tagpage(tags, outdir, title, extension, tags_index_head): content.append(f"{tag.name} ({len(tag.items)}) <{tag.file_basename}>") content.append("```") content.append("") - filename = os.path.join(outdir, "tagsindex.md") + filename = Path(outdir) / "tagsindex.md" else: content = [] content.append(":orphan:") @@ -358,10 +372,9 @@ def tagpage(tags, outdir, title, extension, tags_index_head): f" {tag.name} ({len(tag.items)}) <{tag.file_basename}.rst>" ) content.append("") - filename = os.path.join(outdir, "tagsindex.rst") + filename = Path(outdir) / "tagsindex.rst" - with open(filename, "w", encoding="utf8") as f: - f.write("\n".join(content)) + _update_file_content(filename, "\n".join(content)) def assign_entries(app): @@ -392,13 +405,22 @@ def update_tags(app): if not os.path.exists(os.path.join(app.srcdir, tags_output_dir)): os.makedirs(os.path.join(app.srcdir, tags_output_dir)) - for file in os.listdir(os.path.join(app.srcdir, tags_output_dir)): - if file.endswith("md") or file.endswith("rst"): - os.remove(os.path.join(app.srcdir, tags_output_dir, file)) - # Create pages for each tag tags, pages = assign_entries(app) + # Build list of files to not delete + expected_files = set() + for extension in app.config.tags_extension: + # Add all tags + expected_files.update([f"{tag.file_basename}.{extension}" for tag in tags.values()]) + # Add tagsindex file + expected_files.add(f"tagsindex.{extension}") + + # Remove all files not expected + for file in os.listdir(os.path.join(app.srcdir, tags_output_dir)): + if file not in expected_files: + os.remove(os.path.join(app.srcdir, tags_output_dir, file)) + for tag in tags.values(): tag.create_file( [item for item in pages if tag.name in item.tags], From 00c4ff6e0e0c3c18e77fd3ebb93d5f532dfc6cd0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 4 Dec 2024 09:04:44 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sphinx_tags/__init__.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/sphinx_tags/__init__.py b/src/sphinx_tags/__init__.py index 30257c1..672786d 100644 --- a/src/sphinx_tags/__init__.py +++ b/src/sphinx_tags/__init__.py @@ -21,11 +21,10 @@ def _update_file_content(filepath: Path, content: str) -> None: - """Update content of file only if it has changed - """ + """Update content of file only if it has changed""" if os.path.exists(filepath): - with open(filepath, 'r', encoding='utf8') as f: + with open(filepath, "r", encoding="utf8") as f: old_content = f.read() if old_content == content: # File is already up to date @@ -247,7 +246,9 @@ def create_file( content.append("") - _update_file_content(Path(srcdir) / tags_output_dir / filename, "\n".join(content)) + _update_file_content( + Path(srcdir) / tags_output_dir / filename, "\n".join(content) + ) class Entry: @@ -412,7 +413,9 @@ def update_tags(app): expected_files = set() for extension in app.config.tags_extension: # Add all tags - expected_files.update([f"{tag.file_basename}.{extension}" for tag in tags.values()]) + expected_files.update( + [f"{tag.file_basename}.{extension}" for tag in tags.values()] + ) # Add tagsindex file expected_files.add(f"tagsindex.{extension}")