From 0dd9f16e95baf6fdbbb784bc09dcfaea4f5ba114 Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Wed, 12 May 2021 14:31:28 +0200 Subject: [PATCH 1/2] blurb: extract `write_news()` function --- blurb/blurb.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/blurb/blurb.py b/blurb/blurb.py index 2b9475b..7c87c56 100755 --- a/blurb/blurb.py +++ b/blurb/blurb.py @@ -1064,8 +1064,10 @@ def merge(output=None, *, forced=False): builtins.print("You already have a", repr(output), "file.") require_ok("Type ok to overwrite") - news = open(output, "wt", encoding="utf-8") + write_news(output, versions=versions) + +def write_news(output, *, versions): def print(*a, sep=" "): s = sep.join(str(x) for x in a) return builtins.print(s, file=news) From cf225d674a4efa3508bd2188ba1474dfcf27b1bc Mon Sep 17 00:00:00 2001 From: Dimitri Merejkowsky Date: Wed, 12 May 2021 14:31:52 +0200 Subject: [PATCH 2/2] blurb: don't overwrite output when not necessary 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. --- blurb/blurb.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/blurb/blurb.py b/blurb/blurb.py index 7c87c56..0cb87cc 100755 --- a/blurb/blurb.py +++ b/blurb/blurb.py @@ -49,10 +49,12 @@ from collections import OrderedDict import glob import hashlib +import io import inspect import itertools import math import os +from pathlib import Path import re import shlex import shutil @@ -1068,9 +1070,11 @@ def merge(output=None, *, forced=False): def write_news(output, *, versions): + buff = io.StringIO() + def print(*a, sep=" "): s = sep.join(str(x) for x in a) - return builtins.print(s, file=news) + return builtins.print(s, file=buff) print (""" +++++++++++ @@ -1131,7 +1135,20 @@ def print(*a, sep=" "): print(text) print() print("**(For information about older versions, consult the HISTORY file.)**") - news.close() + + + new_contents = buff.getvalue() + + # Only write in `output` if the contents are different + # This speeds up subsequent Sphinx builds + try: + previous_contents = Path(output).read_text(encoding="UTF-8") + except (FileNotFoundError, UnicodeError): + previous_contents = None + if new_contents != previous_contents: + Path(output).write_text(new_contents, encoding="UTF-8") + else: + builtins.print(output, "is already up to date") git_add_files = []