Refactor: Replace copy_extra_entry() with copy_asset()

This commit is contained in:
Takeshi KOMIYA 2016-07-03 15:45:01 +09:00
parent 829b5631a3
commit bb1e6f9044
7 changed files with 38 additions and 39 deletions

View File

@ -27,12 +27,13 @@ from docutils.frontend import OptionParser
from docutils.readers.doctree import Reader as DoctreeReader
from sphinx import package_dir, __display_version__
from sphinx.util import jsonimpl, copy_static_entry, copy_extra_entry
from sphinx.util import jsonimpl, copy_static_entry
from sphinx.util.i18n import format_date
from sphinx.util.osutil import SEP, os_path, relative_uri, ensuredir, \
movefile, copyfile
from sphinx.util.nodes import inline_all_toctrees
from sphinx.util.matching import patmatch, compile_matchers
from sphinx.util.fileutil import copy_asset
from sphinx.util.matching import patmatch, compile_matchers, Matcher
from sphinx.config import string_classes
from sphinx.locale import _, l_
from sphinx.search import js_index
@ -650,14 +651,15 @@ class StandaloneHTMLBuilder(Builder):
def copy_extra_files(self):
# copy html_extra_path files
self.info(bold('copying extra files... '), nonl=True)
extraentries = [path.join(self.confdir, epath)
for epath in self.config.html_extra_path]
matchers = compile_matchers(self.config.exclude_patterns)
for entry in extraentries:
excluded = Matcher(self.config.exclude_patterns)
for extra_path in self.config.html_extra_path:
entry = path.join(self.confdir, extra_path)
if not path.exists(entry):
self.warn('html_extra_path entry %r does not exist' % entry)
continue
copy_extra_entry(entry, self.outdir, matchers)
copy_asset(entry, self.outdir, excluded)
self.info('done')
def write_buildinfo(self):

View File

@ -173,37 +173,6 @@ def copy_static_entry(source, targetdir, builder, context={},
builder, context, level=level+1,
exclude_matchers=exclude_matchers)
def copy_extra_entry(source, targetdir, exclude_matchers=()):
"""Copy a HTML builder extra_path entry from source to targetdir.
Handles all possible cases of files, directories and subdirectories.
"""
def excluded(path):
relpath = relative_path(os.path.dirname(source), path)
return any(matcher(relpath) for matcher in exclude_matchers)
def copy_extra_file(source_, targetdir_):
if not excluded(source_):
target = path.join(targetdir_, os.path.basename(source_))
copyfile(source_, target)
if os.path.isfile(source):
copy_extra_file(source, targetdir)
return
for root, dirs, files in os.walk(source):
reltargetdir = os.path.join(targetdir, relative_path(source, root))
for dir in dirs[:]:
if excluded(os.path.join(root, dir)):
dirs.remove(dir)
else:
target = os.path.join(reltargetdir, dir)
if not path.exists(target):
os.mkdir(target)
for file in files:
copy_extra_file(os.path.join(root, file), reltargetdir)
_DEBUG_HEADER = '''\
# Sphinx version: %s
# Python version: %s (%s)

View File

@ -62,6 +62,24 @@ def compile_matchers(patterns):
return [re.compile(_translate_pattern(pat)).match for pat in patterns]
class Matcher(object):
"""A pattern matcher for Multiple shell-style glob patterns.
Note: this modifies the patterns to work with copy_asset().
For example, "**/index.rst" matches with "index.rst"
"""
def __init__(self, patterns):
expanded = [pat[3:] for pat in patterns if pat.startswith('**/')]
self.patterns = compile_matchers(patterns + expanded)
def __call__(self, string):
return self.match(string)
def match(self, string):
return any(pat(string) for pat in self.patterns)
_pat_cache = {}

View File

@ -987,6 +987,8 @@ def test_html_extra_path(app, status, warning):
assert (app.outdir / 'rimg.png').exists()
assert not (app.outdir / '_build/index.html').exists()
assert (app.outdir / 'background.png').exists()
assert (app.outdir / 'subdir' / '.htaccess').exists()
assert not (app.outdir / 'subdir' / '.htpasswd').exists()
@with_app(buildername='html', confoverrides={'html_sourcelink_suffix': ''})

View File

@ -8,7 +8,7 @@
:copyright: Copyright 2007-2016 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from sphinx.util.matching import compile_matchers
from sphinx.util.matching import compile_matchers, Matcher
def test_compile_matchers():
@ -81,3 +81,11 @@ def test_compile_matchers():
pat = compile_matchers(['hello[!].py']).pop()
assert pat('hello[!].py')
assert not pat('hello.py')
def test_Matcher():
matcher = Matcher(['hello.py', '**/world.py'])
assert matcher('hello.py')
assert not matcher('subdir/hello.py')
assert matcher('world.py')
assert matcher('subdir/world.py')