Skip to content

Commit 96735b6

Browse files
authored
blurb: don't overwrite output when not necessary (#403)
This speeds up doc generation. With this change, blurb won't update the NEWS file unnecessarily, which means Sphinx will then use a cache instead of parsing it again.
1 parent 29aaca1 commit 96735b6

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

blurb/blurb.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
from collections import OrderedDict
5050
import glob
5151
import hashlib
52+
import io
5253
import inspect
5354
import itertools
5455
import math
5556
import os
57+
from pathlib import Path
5658
import re
5759
import shlex
5860
import shutil
@@ -1064,11 +1066,15 @@ def merge(output=None, *, forced=False):
10641066
builtins.print("You already have a", repr(output), "file.")
10651067
require_ok("Type ok to overwrite")
10661068

1067-
news = open(output, "wt", encoding="utf-8")
1069+
write_news(output, versions=versions)
1070+
1071+
1072+
def write_news(output, *, versions):
1073+
buff = io.StringIO()
10681074

10691075
def print(*a, sep=" "):
10701076
s = sep.join(str(x) for x in a)
1071-
return builtins.print(s, file=news)
1077+
return builtins.print(s, file=buff)
10721078

10731079
print ("""
10741080
+++++++++++
@@ -1129,7 +1135,20 @@ def print(*a, sep=" "):
11291135
print(text)
11301136
print()
11311137
print("**(For information about older versions, consult the HISTORY file.)**")
1132-
news.close()
1138+
1139+
1140+
new_contents = buff.getvalue()
1141+
1142+
# Only write in `output` if the contents are different
1143+
# This speeds up subsequent Sphinx builds
1144+
try:
1145+
previous_contents = Path(output).read_text(encoding="UTF-8")
1146+
except (FileNotFoundError, UnicodeError):
1147+
previous_contents = None
1148+
if new_contents != previous_contents:
1149+
Path(output).write_text(new_contents, encoding="UTF-8")
1150+
else:
1151+
builtins.print(output, "is already up to date")
11331152

11341153

11351154
git_add_files = []

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy