Skip to content

Commit

Permalink
BUG: Fix bug with doc rebuilds being forced (#12778)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsoner authored Aug 6, 2024
1 parent b08f759 commit 6da5055
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 15 deletions.
5 changes: 4 additions & 1 deletion doc/sphinxext/contrib_avatars.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import os
from pathlib import Path

from mne.utils import _replace_md5


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


def setup(app):
Expand Down
15 changes: 1 addition & 14 deletions doc/sphinxext/gen_commands.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# License: BSD-3-Clause
# Copyright the MNE-Python contributors.
import glob
import os
import shutil
from importlib import import_module
from pathlib import Path

from mne.utils import ArgvSetter, hashfunc
from mne.utils import ArgvSetter, _replace_md5


def setup(app):
Expand Down Expand Up @@ -108,17 +106,6 @@ def generate_commands_rst(app=None):
_replace_md5(str(out_fname))


def _replace_md5(fname):
"""Replace a file based on MD5sum."""
# adapted from sphinx-gallery
assert fname.endswith(".new")
fname_old = fname[:-4]
if os.path.isfile(fname_old) and hashfunc(fname) == hashfunc(fname_old):
os.remove(fname)
else:
shutil.move(fname, fname_old)


# This is useful for testing/iterating to see what the result looks like
if __name__ == "__main__":
generate_commands_rst()
2 changes: 2 additions & 0 deletions mne/utils/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ __all__ = [
"_reg_pinv",
"_reject_data_segments",
"_repeated_svd",
"_replace_md5",
"_require_version",
"_resource_path",
"_safe_input",
Expand Down Expand Up @@ -361,6 +362,7 @@ from .numerics import (
_mask_to_onsets_offsets,
_reg_pinv,
_reject_data_segments,
_replace_md5,
_ReuseCycle,
_scaled_array,
_stamp_to_dt,
Expand Down
13 changes: 13 additions & 0 deletions mne/utils/numerics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import inspect
import numbers
import operator
import os
import shutil
import sys
from contextlib import contextmanager
from datetime import date, datetime, timedelta, timezone
Expand Down Expand Up @@ -1104,3 +1106,14 @@ def _array_repr(x):
"""Produce compact info about float ndarray x."""
assert isinstance(x, np.ndarray), type(x)
return f"shape : {x.shape}, range : [{np.nanmin(x):+0.2g}, {np.nanmax(x):+0.2g}]"


def _replace_md5(fname):
"""Replace a file based on MD5sum."""
# adapted from sphinx-gallery
assert fname.endswith(".new")
fname_old = fname[:-4]
if os.path.isfile(fname_old) and hashfunc(fname) == hashfunc(fname_old):
os.remove(fname)
else:
shutil.move(fname, fname_old)
18 changes: 18 additions & 0 deletions mne/utils/tests/test_numerics.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
_get_inst_data,
_julian_to_date,
_reg_pinv,
_replace_md5,
_ReuseCycle,
_time_mask,
_undo_scaling_array,
Expand Down Expand Up @@ -602,3 +603,20 @@ def my_fun_2(*args):
with pytest.raises(RuntimeError, match="Unsupported sparse type"):
my_fun_2(1, _eye_array(1, format="coo"))
assert n_calls == [2, 2] # never did any computation


def test_replace_md5(tmp_path):
"""Test _replace_md5."""
old = tmp_path / "test"
new = old.with_suffix(".new")
old.write_text("abcd")
new.write_text("abcde")
assert old.is_file()
assert new.is_file()
_replace_md5(str(new))
assert not new.is_file()
assert old.read_text() == "abcde"
new.write_text(old.read_text())
_replace_md5(str(new))
assert old.read_text() == "abcde"
assert not new.is_file()

0 comments on commit 6da5055

Please sign in to comment.