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 4 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
25 changes: 23 additions & 2 deletions src/nbsphinx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1689,17 +1689,37 @@ 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()
for file_list in app.env.nbsphinx_files.values():
files.update(file_list)
overwrite = backwards_compat_overwrite()
for file in status_iterator(files, 'copying linked files... ',
sphinx.util.console.brown, len(files)):
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,
**overwrite)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have this as a function, you could get rid of the additional line above by directly calling the function here (and again below):

Suggested change
**overwrite)
**backwards_compat_overwrite())

What do you think about this?

except OSError as err:
logger.warning(
'Cannot copy local file %r: %s', file, err,
Expand All @@ -1710,7 +1730,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),
**overwrite)
return [] # No new HTML pages are created

def env_updated(app, env):
Expand Down
Loading