|
49 | 49 | from collections import OrderedDict |
50 | 50 | import glob |
51 | 51 | import hashlib |
| 52 | +import io |
52 | 53 | import inspect |
53 | 54 | import itertools |
54 | 55 | import math |
55 | 56 | import os |
| 57 | +from pathlib import Path |
56 | 58 | import re |
57 | 59 | import shlex |
58 | 60 | import shutil |
@@ -1064,11 +1066,15 @@ def merge(output=None, *, forced=False): |
1064 | 1066 | builtins.print("You already have a", repr(output), "file.") |
1065 | 1067 | require_ok("Type ok to overwrite") |
1066 | 1068 |
|
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() |
1068 | 1074 |
|
1069 | 1075 | def print(*a, sep=" "): |
1070 | 1076 | s = sep.join(str(x) for x in a) |
1071 | | - return builtins.print(s, file=news) |
| 1077 | + return builtins.print(s, file=buff) |
1072 | 1078 |
|
1073 | 1079 | print (""" |
1074 | 1080 | +++++++++++ |
@@ -1129,7 +1135,20 @@ def print(*a, sep=" "): |
1129 | 1135 | print(text) |
1130 | 1136 | print() |
1131 | 1137 | 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") |
1133 | 1152 |
|
1134 | 1153 |
|
1135 | 1154 | git_add_files = [] |
|
0 commit comments