-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Search indexing with storage #5854
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
Merged
ericholscher
merged 11 commits into
master
from
davidfischer/search-indexing-with-storage
Aug 8, 2019
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
596bed4
Initial pass at search indexing with storage
davidfischer a909b36
Merge branch 'master' into davidfischer/search-indexing-with-storage
davidfischer ea54d0f
Handle imported files correctly whether a CDN is enabled
davidfischer a29540e
Add tests for the build media storage
davidfischer 62863bf
Update docs and comments on BuildMediaStorage
davidfischer 731f8e6
Updates based on feedback
davidfischer bdc84bd
Merge branch 'master' into davidfischer/search-indexing-with-storage
davidfischer a3ef36e
Ignore result order in walk
davidfischer 207899e
Merge branch 'master' into davidfischer/search-indexing-with-storage
ericholscher 7441b9f
fix test file
ericholscher f344eeb
Fixes for storage/importing
davidfischer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1268,7 +1268,6 @@ def fileify(version_pk, commit, build): | |
) | ||
return | ||
|
||
path = project.rtd_build_path(version.slug) | ||
log.info( | ||
LOG_TEMPLATE, | ||
{ | ||
|
@@ -1278,40 +1277,47 @@ def fileify(version_pk, commit, build): | |
} | ||
) | ||
try: | ||
_manage_imported_files(version, path, commit, build) | ||
_manage_imported_files(version, commit, build) | ||
except Exception: | ||
log.exception('Failed during ImportedFile creation') | ||
|
||
try: | ||
_update_intersphinx_data(version, path, commit, build) | ||
_update_intersphinx_data(version, commit, build) | ||
except Exception: | ||
log.exception('Failed during SphinxDomain creation') | ||
|
||
|
||
def _update_intersphinx_data(version, path, commit, build): | ||
def _update_intersphinx_data(version, commit, build): | ||
""" | ||
Update intersphinx data for this version. | ||
|
||
:param version: Version instance | ||
:param path: Path to search | ||
:param commit: Commit that updated path | ||
:param build: Build id | ||
""" | ||
|
||
object_file = os.path.join(path, 'objects.inv') | ||
if not os.path.exists(object_file): | ||
log.debug('No objects.inv, skipping intersphinx indexing.') | ||
if not settings.RTD_BUILD_MEDIA_STORAGE: | ||
return | ||
|
||
full_json_path = version.project.get_production_media_path( | ||
storage = get_storage_class(settings.RTD_BUILD_MEDIA_STORAGE)() | ||
|
||
html_storage_path = version.project.get_storage_path( | ||
type_='html', version_slug=version.slug, include_file=False | ||
) | ||
json_storage_path = version.project.get_storage_path( | ||
type_='json', version_slug=version.slug, include_file=False | ||
) | ||
type_file = os.path.join(full_json_path, 'readthedocs-sphinx-domain-names.json') | ||
|
||
object_file = storage.join(html_storage_path, 'objects.inv') | ||
if not storage.exists(object_file): | ||
log.debug('No objects.inv, skipping intersphinx indexing.') | ||
return | ||
|
||
type_file = storage.join(json_storage_path, 'readthedocs-sphinx-domain-names.json') | ||
types = {} | ||
titles = {} | ||
if os.path.exists(type_file): | ||
if storage.exists(type_file): | ||
try: | ||
data = json.load(open(type_file)) | ||
data = json.load(storage.open(type_file)) | ||
types = data['types'] | ||
titles = data['titles'] | ||
except Exception: | ||
|
@@ -1331,7 +1337,13 @@ def warn(self, msg): | |
log.warning('Sphinx MockApp: %s', msg) | ||
|
||
# Re-create all objects from the new build of the version | ||
invdata = intersphinx.fetch_inventory(MockApp(), '', object_file) | ||
object_file_url = storage.url(object_file) | ||
if object_file_url.startswith('/'): | ||
# Filesystem backed storage simply prepends MEDIA_URL to the path to get the URL | ||
# This can cause an issue if MEDIA_URL is not fully qualified | ||
object_file_url = 'http://' + settings.PRODUCTION_DOMAIN + object_file_url | ||
|
||
invdata = intersphinx.fetch_inventory(MockApp(), '', object_file_url) | ||
for key, value in sorted(invdata.items() or {}): | ||
domain, _type = key.split(':') | ||
for name, einfo in sorted(value.items()): | ||
|
@@ -1424,29 +1436,40 @@ def clean_build(version_pk): | |
return True | ||
|
||
|
||
def _manage_imported_files(version, path, commit, build): | ||
def _manage_imported_files(version, commit, build): | ||
""" | ||
Update imported files for version. | ||
|
||
:param version: Version instance | ||
:param path: Path to search | ||
:param commit: Commit that updated path | ||
:param build: Build id | ||
""" | ||
|
||
if not settings.RTD_BUILD_MEDIA_STORAGE: | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should likely log something here, so we don't get confused if indexing isn't working. |
||
|
||
storage = get_storage_class(settings.RTD_BUILD_MEDIA_STORAGE)() | ||
|
||
changed_files = set() | ||
|
||
# Re-create all objects from the new build of the version | ||
for root, __, filenames in os.walk(path): | ||
storage_path = version.project.get_storage_path( | ||
type_='html', version_slug=version.slug, include_file=False | ||
) | ||
for root, __, filenames in storage.walk(storage_path): | ||
for filename in filenames: | ||
if filename.endswith('.html'): | ||
model_class = HTMLFile | ||
else: | ||
elif version.project.cdn_enabled: | ||
# We need to track all files for CDN enabled projects so the files can be purged | ||
model_class = ImportedFile | ||
else: | ||
# For projects not behind a CDN, we don't care about non-HTML | ||
continue | ||
|
||
full_path = os.path.join(root, filename) | ||
relpath = os.path.relpath(full_path, path) | ||
relpath = storage.join(root, filename) | ||
try: | ||
md5 = hashlib.md5(open(full_path, 'rb').read()).hexdigest() | ||
md5 = hashlib.md5(storage.open(relpath, 'rb').read()).hexdigest() | ||
except Exception: | ||
log.exception( | ||
'Error while generating md5 for %s:%s:%s. Don\'t stop.', | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import os | ||
import shutil | ||
import tempfile | ||
|
||
from django.test import TestCase | ||
|
||
from readthedocs.builds.storage import BuildMediaFileSystemStorage | ||
|
||
|
||
files_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'files') | ||
|
||
|
||
class TestBuildMediaStorage(TestCase): | ||
def setUp(self): | ||
self.test_media_dir = tempfile.mkdtemp() | ||
self.storage = BuildMediaFileSystemStorage(location=self.test_media_dir) | ||
|
||
def tearDown(self): | ||
shutil.rmtree(self.test_media_dir, ignore_errors=True) | ||
|
||
def test_copy_directory(self): | ||
self.assertFalse(self.storage.exists('files/test.html')) | ||
|
||
self.storage.copy_directory(files_dir, 'files') | ||
self.assertTrue(self.storage.exists('files/test.html')) | ||
self.assertTrue(self.storage.exists('files/conf.py')) | ||
self.assertTrue(self.storage.exists('files/api.fjson')) | ||
|
||
def test_delete_directory(self): | ||
self.storage.copy_directory(files_dir, 'files') | ||
dirs, files = self.storage.listdir('files') | ||
self.assertEqual(dirs, []) | ||
self.assertEqual(files, ['api.fjson', 'conf.py', 'test.html']) | ||
|
||
self.storage.delete_directory('files/') | ||
dirs, files = self.storage.listdir('files') | ||
self.assertEqual(dirs, []) | ||
self.assertEqual(files, []) | ||
|
||
def test_walk(self): | ||
self.storage.copy_directory(files_dir, 'files') | ||
self.storage.copy_directory(files_dir, 'files/subfiles') | ||
|
||
output = list(self.storage.walk('files')) | ||
self.assertEqual(len(output), 2) | ||
self.assertEqual( | ||
output, | ||
[ | ||
('files', ['subfiles'], ['api.fjson', 'conf.py', 'test.html']), | ||
('files/subfiles', [], ['api.fjson', 'conf.py', 'test.html']), | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we should be able to get away from this soon. We should be starting to store the proper path of a file after readthedocs/readthedocs-sphinx-ext#62 is merged.