Skip to content

Commit 6da5055

Browse files
authored
BUG: Fix bug with doc rebuilds being forced (#12778)
1 parent b08f759 commit 6da5055

5 files changed

Lines changed: 38 additions & 15 deletions

File tree

doc/sphinxext/contrib_avatars.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import os
44
from pathlib import Path
55

6+
from mne.utils import _replace_md5
7+
68

79
def generate_contrib_avatars(app, config):
810
"""Render a template webpage with avatars generated by JS and a GitHub API call."""
911
root = Path(app.srcdir)
1012
infile = root / "sphinxext" / "_avatar_template.html"
11-
outfile = root / "_templates" / "avatars.html"
13+
outfile = root / "_templates" / "avatars.html.new"
1214
if os.getenv("MNE_ADD_CONTRIBUTOR_IMAGE", "false").lower() != "true":
1315
body = """\
1416
<p>Contributor avators will appear here in full doc builds. Set \
@@ -36,6 +38,7 @@ def generate_contrib_avatars(app, config):
3638
driver.quit()
3739
with open(outfile, "w") as fid:
3840
fid.write(body)
41+
_replace_md5(str(outfile))
3942

4043

4144
def setup(app):

doc/sphinxext/gen_commands.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
# License: BSD-3-Clause
22
# Copyright the MNE-Python contributors.
33
import glob
4-
import os
5-
import shutil
64
from importlib import import_module
75
from pathlib import Path
86

9-
from mne.utils import ArgvSetter, hashfunc
7+
from mne.utils import ArgvSetter, _replace_md5
108

119

1210
def setup(app):
@@ -108,17 +106,6 @@ def generate_commands_rst(app=None):
108106
_replace_md5(str(out_fname))
109107

110108

111-
def _replace_md5(fname):
112-
"""Replace a file based on MD5sum."""
113-
# adapted from sphinx-gallery
114-
assert fname.endswith(".new")
115-
fname_old = fname[:-4]
116-
if os.path.isfile(fname_old) and hashfunc(fname) == hashfunc(fname_old):
117-
os.remove(fname)
118-
else:
119-
shutil.move(fname, fname_old)
120-
121-
122109
# This is useful for testing/iterating to see what the result looks like
123110
if __name__ == "__main__":
124111
generate_commands_rst()

mne/utils/__init__.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ __all__ = [
9898
"_reg_pinv",
9999
"_reject_data_segments",
100100
"_repeated_svd",
101+
"_replace_md5",
101102
"_require_version",
102103
"_resource_path",
103104
"_safe_input",
@@ -361,6 +362,7 @@ from .numerics import (
361362
_mask_to_onsets_offsets,
362363
_reg_pinv,
363364
_reject_data_segments,
365+
_replace_md5,
364366
_ReuseCycle,
365367
_scaled_array,
366368
_stamp_to_dt,

mne/utils/numerics.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import inspect
99
import numbers
1010
import operator
11+
import os
12+
import shutil
1113
import sys
1214
from contextlib import contextmanager
1315
from datetime import date, datetime, timedelta, timezone
@@ -1104,3 +1106,14 @@ def _array_repr(x):
11041106
"""Produce compact info about float ndarray x."""
11051107
assert isinstance(x, np.ndarray), type(x)
11061108
return f"shape : {x.shape}, range : [{np.nanmin(x):+0.2g}, {np.nanmax(x):+0.2g}]"
1109+
1110+
1111+
def _replace_md5(fname):
1112+
"""Replace a file based on MD5sum."""
1113+
# adapted from sphinx-gallery
1114+
assert fname.endswith(".new")
1115+
fname_old = fname[:-4]
1116+
if os.path.isfile(fname_old) and hashfunc(fname) == hashfunc(fname_old):
1117+
os.remove(fname)
1118+
else:
1119+
shutil.move(fname, fname_old)

mne/utils/tests/test_numerics.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
_get_inst_data,
2727
_julian_to_date,
2828
_reg_pinv,
29+
_replace_md5,
2930
_ReuseCycle,
3031
_time_mask,
3132
_undo_scaling_array,
@@ -602,3 +603,20 @@ def my_fun_2(*args):
602603
with pytest.raises(RuntimeError, match="Unsupported sparse type"):
603604
my_fun_2(1, _eye_array(1, format="coo"))
604605
assert n_calls == [2, 2] # never did any computation
606+
607+
608+
def test_replace_md5(tmp_path):
609+
"""Test _replace_md5."""
610+
old = tmp_path / "test"
611+
new = old.with_suffix(".new")
612+
old.write_text("abcd")
613+
new.write_text("abcde")
614+
assert old.is_file()
615+
assert new.is_file()
616+
_replace_md5(str(new))
617+
assert not new.is_file()
618+
assert old.read_text() == "abcde"
619+
new.write_text(old.read_text())
620+
_replace_md5(str(new))
621+
assert old.read_text() == "abcde"
622+
assert not new.is_file()

0 commit comments

Comments
 (0)