Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/utils.py
# Copyright (C) 2017-2020 The Software Heritage developers | # Copyright (C) 2017-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import base64 | import base64 | ||||
import magic | import magic | ||||
import pypandoc | |||||
import stat | import stat | ||||
import textwrap | import textwrap | ||||
from collections import defaultdict | from collections import defaultdict | ||||
from threading import Lock | from threading import Lock | ||||
from django.core.cache import cache | from django.core.cache import cache | ||||
from django.utils.safestring import mark_safe | from django.utils.safestring import mark_safe | ||||
from django.utils.html import escape | from django.utils.html import escape | ||||
import sentry_sdk | import sentry_sdk | ||||
from swh.model.identifiers import persistent_identifier | from swh.model.identifiers import persistent_identifier | ||||
from swh.web.common import highlightjs, service | from swh.web.common import highlightjs, service | ||||
from swh.web.common.exc import NotFoundExc, http_status_code_message | from swh.web.common.exc import NotFoundExc, http_status_code_message | ||||
from swh.web.common.origin_visits import get_origin_visit | from swh.web.common.origin_visits import get_origin_visit | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
reverse, format_utc_iso_date, get_swh_persistent_id, | reverse, format_utc_iso_date, get_swh_persistent_id, | ||||
swh_object_icons | swh_object_icons, rst_to_html | ||||
) | ) | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
def get_directory_entries(sha1_git): | def get_directory_entries(sha1_git): | ||||
"""Function that retrieves the content of a directory | """Function that retrieves the content of a directory | ||||
from the archive. | from the archive. | ||||
▲ Show 20 Lines • Show All 1,009 Lines • ▼ Show 20 Lines | if readme_name and readme_name.endswith('.rst'): | ||||
cache_entry_id = 'readme_%s' % readme_sha1 | cache_entry_id = 'readme_%s' % readme_sha1 | ||||
cache_entry = cache.get(cache_entry_id) | cache_entry = cache.get(cache_entry_id) | ||||
if cache_entry: | if cache_entry: | ||||
readme_html = cache_entry | readme_html = cache_entry | ||||
else: | else: | ||||
try: | try: | ||||
rst_doc = request_content(readme_sha1) | rst_doc = request_content(readme_sha1) | ||||
readme_html = pypandoc.convert_text(rst_doc['raw_data'], | readme_html = rst_to_html(rst_doc['raw_data']) | ||||
'html', format='rst') | |||||
cache.set(cache_entry_id, readme_html) | cache.set(cache_entry_id, readme_html) | ||||
except Exception as exc: | except Exception as exc: | ||||
sentry_sdk.capture_exception(exc) | sentry_sdk.capture_exception(exc) | ||||
readme_html = 'Readme bytes are not available' | readme_html = 'Readme bytes are not available' | ||||
return readme_name, readme_url, readme_html | return readme_name, readme_url, readme_html | ||||
▲ Show 20 Lines • Show All 45 Lines • Show Last 20 Lines |