mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Use `copyfile
` where possible
This commit is contained in:
parent
f09bc62969
commit
eb2186490a
@ -373,13 +373,12 @@ example of code to accomplish this:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
from os import path
|
||||
from sphinx.util.fileutil import copy_asset_file
|
||||
import shutil
|
||||
|
||||
def copy_custom_files(app, exc):
|
||||
if app.builder.format == 'html' and not exc:
|
||||
staticdir = path.join(app.builder.outdir, '_static')
|
||||
copy_asset_file('path/to/myextension/_static/myjsfile.js', staticdir)
|
||||
static_dir = app.outdir / '_static'
|
||||
shutil.copyfile('path/to/myextension/_static/myjsfile.js', static_dir)
|
||||
|
||||
def setup(app):
|
||||
app.connect('build-finished', copy_custom_files)
|
||||
|
@ -412,8 +412,11 @@ class EpubBuilder(StandaloneHTMLBuilder):
|
||||
logger.warning(__('cannot read image file %r: copying it instead'),
|
||||
path.join(self.srcdir, src))
|
||||
try:
|
||||
copyfile(path.join(self.srcdir, src),
|
||||
path.join(self.outdir, self.imagedir, dest))
|
||||
copyfile(
|
||||
self.srcdir / src,
|
||||
self.outdir / self.imagedir / dest,
|
||||
force=True,
|
||||
)
|
||||
except OSError as err:
|
||||
logger.warning(__('cannot copy image file %r: %s'),
|
||||
path.join(self.srcdir, src), err)
|
||||
@ -475,14 +478,14 @@ class EpubBuilder(StandaloneHTMLBuilder):
|
||||
def build_mimetype(self) -> None:
|
||||
"""Write the metainfo file mimetype."""
|
||||
logger.info(__('writing mimetype file...'))
|
||||
copy_asset_file(path.join(self.template_dir, 'mimetype'), self.outdir)
|
||||
copyfile(path.join(self.template_dir, 'mimetype'), self.outdir, force=True)
|
||||
|
||||
def build_container(self, outname: str = 'META-INF/container.xml') -> None:
|
||||
"""Write the metainfo file META-INF/container.xml."""
|
||||
logger.info(__('writing META-INF/container.xml file...'))
|
||||
outdir = path.join(self.outdir, 'META-INF')
|
||||
ensuredir(outdir)
|
||||
copy_asset_file(path.join(self.template_dir, 'container.xml'), outdir)
|
||||
copyfile(path.join(self.template_dir, 'container.xml'), outdir, force=True)
|
||||
|
||||
def content_metadata(self) -> dict[str, Any]:
|
||||
"""Create a dictionary with all metadata for the content.opf
|
||||
@ -625,7 +628,8 @@ class EpubBuilder(StandaloneHTMLBuilder):
|
||||
copy_asset_file(
|
||||
path.join(self.template_dir, 'content.opf.jinja'),
|
||||
self.outdir,
|
||||
context=metadata
|
||||
context=metadata,
|
||||
force=True,
|
||||
)
|
||||
|
||||
def new_navpoint(self, node: dict[str, Any], level: int, incr: bool = True) -> NavPoint:
|
||||
@ -709,8 +713,12 @@ class EpubBuilder(StandaloneHTMLBuilder):
|
||||
navpoints = self.build_navpoints(refnodes)
|
||||
level = max(item['level'] for item in self.refnodes)
|
||||
level = min(level, self.config.epub_tocdepth)
|
||||
copy_asset_file(path.join(self.template_dir, 'toc.ncx.jinja'), self.outdir,
|
||||
context=self.toc_metadata(level, navpoints))
|
||||
copy_asset_file(
|
||||
path.join(self.template_dir, 'toc.ncx.jinja'),
|
||||
self.outdir,
|
||||
context=self.toc_metadata(level, navpoints),
|
||||
force=True,
|
||||
)
|
||||
|
||||
def build_epub(self) -> None:
|
||||
"""Write the epub file.
|
||||
|
@ -14,7 +14,7 @@ from sphinx.theming import HTMLThemeFactory
|
||||
from sphinx.util import logging
|
||||
from sphinx.util.console import bold
|
||||
from sphinx.util.fileutil import copy_asset_file
|
||||
from sphinx.util.osutil import ensuredir, os_path
|
||||
from sphinx.util.osutil import copyfile, ensuredir, os_path
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from sphinx.application import Sphinx
|
||||
@ -140,10 +140,18 @@ class ChangesBuilder(Builder):
|
||||
f.write(self.templates.render('changes/rstsource.html', ctx))
|
||||
themectx = {'theme_' + key: val for (key, val) in
|
||||
self.theme.get_options({}).items()}
|
||||
copy_asset_file(path.join(package_dir, 'themes', 'default', 'static', 'default.css.jinja'), # NoQA: E501
|
||||
self.outdir, context=themectx, renderer=self.templates)
|
||||
copy_asset_file(path.join(package_dir, 'themes', 'basic', 'static', 'basic.css'),
|
||||
self.outdir)
|
||||
copy_asset_file(
|
||||
path.join(package_dir, 'themes', 'default', 'static', 'default.css.jinja'),
|
||||
self.outdir,
|
||||
context=themectx,
|
||||
renderer=self.templates,
|
||||
force=True,
|
||||
)
|
||||
copyfile(
|
||||
path.join(package_dir, 'themes', 'basic', 'static', 'basic.css'),
|
||||
self.outdir,
|
||||
force=True,
|
||||
)
|
||||
|
||||
def hl(self, text: str, version: str) -> str:
|
||||
text = html.escape(text)
|
||||
|
@ -197,7 +197,8 @@ class Epub3Builder(_epub_base.EpubBuilder):
|
||||
copy_asset_file(
|
||||
path.join(self.template_dir, 'nav.xhtml.jinja'),
|
||||
self.outdir,
|
||||
context=self.navigation_doc_metadata(navlist)
|
||||
context=self.navigation_doc_metadata(navlist),
|
||||
force=True,
|
||||
)
|
||||
|
||||
# Add nav.xhtml to epub file
|
||||
|
@ -26,7 +26,7 @@ from sphinx.util.docutils import SphinxFileOutput, new_document
|
||||
from sphinx.util.fileutil import copy_asset_file
|
||||
from sphinx.util.i18n import format_date
|
||||
from sphinx.util.nodes import inline_all_toctrees
|
||||
from sphinx.util.osutil import SEP, make_filename_from_project
|
||||
from sphinx.util.osutil import SEP, copyfile, make_filename_from_project
|
||||
from sphinx.util.template import LaTeXRenderer
|
||||
from sphinx.writers.latex import LaTeXTranslator, LaTeXWriter
|
||||
|
||||
@ -432,7 +432,7 @@ class LaTeXBuilder(Builder):
|
||||
def copy_latex_additional_files(self) -> None:
|
||||
for filename in self.config.latex_additional_files:
|
||||
logger.info(' ' + filename, nonl=True)
|
||||
copy_asset_file(
|
||||
copyfile(
|
||||
self.confdir / filename,
|
||||
self.outdir,
|
||||
force=True,
|
||||
@ -446,7 +446,7 @@ class LaTeXBuilder(Builder):
|
||||
stringify_func=stringify_func):
|
||||
dest = self.images[src]
|
||||
try:
|
||||
copy_asset_file(
|
||||
copyfile(
|
||||
self.srcdir / src,
|
||||
self.outdir / dest,
|
||||
force=True,
|
||||
@ -457,7 +457,7 @@ class LaTeXBuilder(Builder):
|
||||
if self.config.latex_logo:
|
||||
if not path.isfile(path.join(self.confdir, self.config.latex_logo)):
|
||||
raise SphinxError(__('logo file %r does not exist') % self.config.latex_logo)
|
||||
copy_asset_file(
|
||||
copyfile(
|
||||
self.confdir / self.config.latex_logo,
|
||||
self.outdir,
|
||||
force=True,
|
||||
|
@ -20,9 +20,8 @@ from sphinx.util import logging
|
||||
from sphinx.util.console import darkgreen
|
||||
from sphinx.util.display import progress_message, status_iterator
|
||||
from sphinx.util.docutils import new_document
|
||||
from sphinx.util.fileutil import copy_asset_file
|
||||
from sphinx.util.nodes import inline_all_toctrees
|
||||
from sphinx.util.osutil import SEP, ensuredir, make_filename_from_project
|
||||
from sphinx.util.osutil import SEP, copyfile, ensuredir, make_filename_from_project
|
||||
from sphinx.writers.texinfo import TexinfoTranslator, TexinfoWriter
|
||||
|
||||
if TYPE_CHECKING:
|
||||
@ -189,10 +188,13 @@ class TexinfoBuilder(Builder):
|
||||
stringify_func=stringify_func):
|
||||
dest = self.images[src]
|
||||
try:
|
||||
imagedir = path.join(self.outdir, targetname + '-figures')
|
||||
imagedir = self.outdir / f'{targetname}-figures'
|
||||
ensuredir(imagedir)
|
||||
copy_asset_file(path.join(self.srcdir, src),
|
||||
path.join(imagedir, dest))
|
||||
copyfile(
|
||||
self.srcdir / src,
|
||||
imagedir / dest,
|
||||
force=True,
|
||||
)
|
||||
except Exception as err:
|
||||
logger.warning(__('cannot copy image file %r: %s'),
|
||||
path.join(self.srcdir, src), err)
|
||||
@ -201,7 +203,7 @@ class TexinfoBuilder(Builder):
|
||||
try:
|
||||
with progress_message(__('copying Texinfo support files')):
|
||||
logger.info('Makefile ', nonl=True)
|
||||
copy_asset_file(os.path.join(template_dir, 'Makefile'), self.outdir)
|
||||
copyfile(os.path.join(template_dir, 'Makefile'), self.outdir, force=True)
|
||||
except OSError as err:
|
||||
logger.warning(__("error writing file Makefile: %s"), err)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user