diff --git a/readthedocs/doc_builder/backends/sphinx.py b/readthedocs/doc_builder/backends/sphinx.py index 66154f68dea..5ed06c8f831 100644 --- a/readthedocs/doc_builder/backends/sphinx.py +++ b/readthedocs/doc_builder/backends/sphinx.py @@ -237,6 +237,7 @@ class SearchBuilder(BaseSphinx): type = 'sphinx_search' sphinx_builder = 'json' sphinx_build_dir = '_build/json' + ignore_patterns = ['_static'] class LocalMediaBuilder(BaseSphinx): diff --git a/readthedocs/doc_builder/base.py b/readthedocs/doc_builder/base.py index d81565d9c7c..fcb70964709 100644 --- a/readthedocs/doc_builder/base.py +++ b/readthedocs/doc_builder/base.py @@ -37,6 +37,8 @@ class BaseBuilder(object): _force = False + ignore_patterns = [] + # old_artifact_path = .. def __init__(self, build_env, python_env, force=False): @@ -63,7 +65,12 @@ def move(self, **__): if os.path.exists(self.target): shutil.rmtree(self.target) log.info('Copying %s on the local filesystem', self.type) - shutil.copytree(self.old_artifact_path, self.target) + log.info('Ignoring patterns %s', self.ignore_patterns) + shutil.copytree( + self.old_artifact_path, + self.target, + ignore=shutil.ignore_patterns(*self.ignore_patterns) + ) else: log.warning('Not moving docs, because the build dir is unknown.') diff --git a/readthedocs/rtd_tests/tests/test_doc_builder.py b/readthedocs/rtd_tests/tests/test_doc_builder.py index 5713ba00961..8ac0a30c8e3 100644 --- a/readthedocs/rtd_tests/tests/test_doc_builder.py +++ b/readthedocs/rtd_tests/tests/test_doc_builder.py @@ -10,7 +10,7 @@ from mock import patch, Mock import pytest -from readthedocs.doc_builder.backends.sphinx import BaseSphinx +from readthedocs.doc_builder.backends.sphinx import BaseSphinx, SearchBuilder from readthedocs.projects.exceptions import ProjectConfigurationError from readthedocs.projects.models import Project @@ -69,3 +69,42 @@ def test_create_conf_py(self, conf_file, get_conf_py_path, _, get_config_params, expected_conf_py = os.path.join(os.path.dirname(__file__), '..', 'files', 'conf.py') with open(generated_conf_py) as gf, open(expected_conf_py) as ef: self.assertEqual(gf.read(), ef.read()) + + +class SphinxSearchBuilderTest(TestCase): + + fixtures = ['test_data'] + + def setUp(self): + self.project = Project.objects.get(slug='pip') + self.version = self.project.versions.first() + + build_env = namedtuple('project', 'version') + build_env.project = self.project + build_env.version = self.version + + self.searchbuilder = SearchBuilder(build_env=build_env, python_env=None) + + def test_ignore_patterns(self): + src = tempfile.mkdtemp() + src_static = os.path.join(src, '_static/') + src_other = os.path.join(src, 'other/') + os.mkdir(src_static) + os.mkdir(src_other) + + dest = tempfile.mkdtemp() + dest_static = os.path.join(dest, '_static/') + dest_other = os.path.join(dest, 'other/') + + self.searchbuilder.old_artifact_path = src + self.searchbuilder.target = dest + + # There is a _static/ dir in src/ but not in dest/ + self.assertTrue(os.path.exists(src_static)) + self.assertFalse(os.path.exists(dest_static)) + + self.searchbuilder.move() + + # There is a dest/other/ but not a dest/_static + self.assertFalse(os.path.exists(dest_static)) + self.assertTrue(os.path.exists(dest_other))