Skip to content

Commit

Permalink
Use force=True with copyfile (#819)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabalafou authored Nov 10, 2024
1 parent 1fda481 commit 40d8cd4
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/nbsphinx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,22 @@ def html_page_context(app, pagename, templatename, context, doctree):
app.add_css_file('nbsphinx-gallery.css')


def backwards_compat_overwrite(copyfile=sphinx.util.copyfile):
"""Return kwargs dictionary to pass to copyfile() for consistent behavior
Before version 8 of Sphinx, the default behavior of the copyfile function
was to overwrite the file at the target path if it already existed.
Version 8 requires passing force=True.
Ref: https://github.com/sphinx-doc/sphinx/pull/12647
"""
from inspect import signature
if "force" in signature(copyfile).parameters:
return {"force": True}
else:
return {}


def html_collect_pages(app):
"""This event handler is abused to copy local files around."""
files = set()
Expand All @@ -1699,7 +1715,10 @@ def html_collect_pages(app):
target = os.path.join(app.builder.outdir, file)
sphinx.util.ensuredir(os.path.dirname(target))
try:
sphinx.util.copyfile(os.path.join(app.env.srcdir, file), target)
sphinx.util.copyfile(
os.path.join(app.env.srcdir, file),
target,
**backwards_compat_overwrite())
except OSError as err:
logger.warning(
'Cannot copy local file %r: %s', file, err,
Expand All @@ -1710,7 +1729,8 @@ def html_collect_pages(app):
'brown', len(notebooks)):
sphinx.util.copyfile(
os.path.join(app.env.nbsphinx_auxdir, notebook),
os.path.join(app.builder.outdir, notebook))
os.path.join(app.builder.outdir, notebook),
**backwards_compat_overwrite())
return [] # No new HTML pages are created

def env_updated(app, env):
Expand Down

0 comments on commit 40d8cd4

Please sign in to comment.