Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use force=True with copyfile #819

Merged
merged 5 commits into from
Nov 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading