Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/utils.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-2018 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 | ||||
from collections import defaultdict | from collections import defaultdict | ||||
import magic | import magic | ||||
import math | import math | ||||
import pypandoc | import pypandoc | ||||
import stat | import stat | ||||
import textwrap | |||||
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 importlib import reload | from importlib import reload | ||||
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 | ||||
▲ Show 20 Lines • Show All 705 Lines • ▼ Show 20 Lines | def gen_snapshot_directory_link(snapshot_context, revision_id=None, | ||||
Returns: | Returns: | ||||
An HTML link in the form | An HTML link in the form | ||||
'<a href="origin_directory_view_url">origin_directory_view_url</a>' | '<a href="origin_directory_view_url">origin_directory_view_url</a>' | ||||
""" | """ | ||||
query_params = {'revision': revision_id} | query_params = {'revision': revision_id} | ||||
if snapshot_context['origin_info']: | if snapshot_context['origin_info']: | ||||
origin_info = snapshot_context['origin_info'] | origin_info = snapshot_context['origin_info'] | ||||
url_args = {'origin_type': origin_info['type'], | url_args = {'origin_url': origin_info['url']} | ||||
'origin_url': origin_info['url']} | |||||
if 'timestamp' in snapshot_context['url_args']: | if 'timestamp' in snapshot_context['url_args']: | ||||
url_args['timestamp'] = \ | url_args['timestamp'] = \ | ||||
snapshot_context['url_args']['timestamp'] | snapshot_context['url_args']['timestamp'] | ||||
if 'visit_id' in snapshot_context['query_params']: | if 'visit_id' in snapshot_context['query_params']: | ||||
query_params['visit_id'] = \ | query_params['visit_id'] = \ | ||||
snapshot_context['query_params']['visit_id'] | snapshot_context['query_params']['visit_id'] | ||||
directory_url = reverse('browse-origin-directory', | directory_url = reverse('browse-origin-directory', | ||||
url_args=url_args, | url_args=url_args, | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | Args: | ||||
snapshot_context (dict): if provided, generate snapshot-dependent | snapshot_context (dict): if provided, generate snapshot-dependent | ||||
browsing link | browsing link | ||||
Returns: | Returns: | ||||
The SWH revision log view URL | The SWH revision log view URL | ||||
""" | """ | ||||
query_params = {'revision': revision_id} | query_params = {'revision': revision_id} | ||||
if snapshot_context and snapshot_context['origin_info']: | if snapshot_context and snapshot_context['origin_info']: | ||||
origin_info = snapshot_context['origin_info'] | origin_info = snapshot_context['origin_info'] | ||||
url_args = {'origin_type': origin_info['type'], | url_args = {'origin_url': origin_info['url']} | ||||
'origin_url': origin_info['url']} | |||||
if 'timestamp' in snapshot_context['url_args']: | if 'timestamp' in snapshot_context['url_args']: | ||||
url_args['timestamp'] = \ | url_args['timestamp'] = \ | ||||
snapshot_context['url_args']['timestamp'] | snapshot_context['url_args']['timestamp'] | ||||
if 'visit_id' in snapshot_context['query_params']: | if 'visit_id' in snapshot_context['query_params']: | ||||
query_params['visit_id'] = \ | query_params['visit_id'] = \ | ||||
snapshot_context['query_params']['visit_id'] | snapshot_context['query_params']['visit_id'] | ||||
revision_log_url = reverse('browse-origin-log', | revision_log_url = reverse('browse-origin-log', | ||||
url_args=url_args, | url_args=url_args, | ||||
Show All 32 Lines | def gen_revision_log_link(revision_id, snapshot_context=None, link_text=None, | ||||
revision_log_url = get_revision_log_url(revision_id, snapshot_context) | revision_log_url = get_revision_log_url(revision_id, snapshot_context) | ||||
if not link_text: | if not link_text: | ||||
link_text = revision_log_url | link_text = revision_log_url | ||||
return gen_link(revision_log_url, link_text, link_attrs) | return gen_link(revision_log_url, link_text, link_attrs) | ||||
def _format_log_entries(revision_log, per_page, snapshot_context=None): | def format_log_entries(revision_log, per_page, snapshot_context=None): | ||||
revision_log_data = [] | |||||
for i, log in enumerate(revision_log): | |||||
if i == per_page: | |||||
break | |||||
author_name = 'None' | |||||
author_link = 'None' | |||||
if log['author']: | |||||
author_name = log['author']['name'] or log['author']['fullname'] | |||||
author_link = gen_person_link(log['author']['id'], author_name, | |||||
snapshot_context) | |||||
revision_log_data.append( | |||||
{'author': author_link, | |||||
'revision': gen_revision_link(log['id'], True, snapshot_context), | |||||
'message': log['message'], | |||||
'date': format_utc_iso_date(log['date']), | |||||
'directory': log['directory']}) | |||||
return revision_log_data | |||||
def prepare_revision_log_for_display(revision_log, per_page, revs_breadcrumb, | |||||
snapshot_context=None): | |||||
""" | """ | ||||
Utility functions that process raw revision log data for HTML display. | Utility functions that process raw revision log data for HTML display. | ||||
Its purpose is to: | Its purpose is to: | ||||
* add links to relevant SWH browse views | * add links to relevant SWH browse views | ||||
* format date in human readable format | * format date in human readable format | ||||
* truncate the message log | * truncate the message log | ||||
It also computes the data needed to generate the links for navigating back | |||||
and forth in the history log. | |||||
Args: | Args: | ||||
revision_log (list): raw revision log as returned by the SWH web api | revision_log (list): raw revision log as returned by the SWH web api | ||||
per_page (int): number of log entries per page | per_page (int): number of log entries per page | ||||
revs_breadcrumb (str): breadcrumbs of revisions navigated so far, | |||||
in the form 'rev1[/rev2/../revN]'. Each revision corresponds to | |||||
the first one displayed in the HTML view for history log. | |||||
snapshot_context (dict): if provided, generate snapshot-dependent | snapshot_context (dict): if provided, generate snapshot-dependent | ||||
browsing link | browsing link | ||||
""" | """ | ||||
current_rev = revision_log[0]['id'] | revision_log_data = [] | ||||
next_rev = None | for i, rev in enumerate(revision_log): | ||||
prev_rev = None | if i == per_page: | ||||
next_revs_breadcrumb = None | break | ||||
prev_revs_breadcrumb = None | author_name = 'None' | ||||
if len(revision_log) == per_page + 1: | author_fullname = 'None' | ||||
prev_rev = revision_log[-1]['id'] | committer_fullname = 'None' | ||||
if rev['author']: | |||||
prev_rev_bc = current_rev | author_name = rev['author']['name'] or rev['author']['fullname'] | ||||
if snapshot_context: | author_fullname = rev['author']['fullname'] | ||||
prev_rev_bc = prev_rev | if rev['committer']: | ||||
committer_fullname = rev['committer']['fullname'] | |||||
if revs_breadcrumb: | author_date = format_utc_iso_date(rev['date']) | ||||
revs = revs_breadcrumb.split('/') | committer_date = format_utc_iso_date(rev['committer_date']) | ||||
next_rev = revs[-1] | |||||
if len(revs) > 1: | tooltip = 'revision %s\n' % rev['id'] | ||||
next_revs_breadcrumb = '/'.join(revs[:-1]) | tooltip += 'author: %s\n' % author_fullname | ||||
if len(revision_log) == per_page + 1: | tooltip += 'author date: %s\n' % author_date | ||||
prev_revs_breadcrumb = revs_breadcrumb + '/' + prev_rev_bc | tooltip += 'committer: %s\n' % committer_fullname | ||||
else: | tooltip += 'committer date: %s\n\n' % committer_date | ||||
prev_revs_breadcrumb = prev_rev_bc | tooltip += textwrap.indent(rev['message'], ' '*4) | ||||
return {'revision_log_data': _format_log_entries(revision_log, per_page, | revision_log_data.append({ | ||||
snapshot_context), | 'author': author_name, | ||||
'prev_rev': prev_rev, | 'id': rev['id'][:7], | ||||
'prev_revs_breadcrumb': prev_revs_breadcrumb, | 'message': rev['message'], | ||||
'next_rev': next_rev, | 'date': author_date, | ||||
'next_revs_breadcrumb': next_revs_breadcrumb} | 'commit_date': committer_date, | ||||
'url': gen_revision_url(rev['id'], snapshot_context), | |||||
'tooltip': tooltip | |||||
}) | |||||
return revision_log_data | |||||
# list of origin types that can be found in the swh archive | # list of origin types that can be found in the swh archive | ||||
# TODO: retrieve it dynamically in an efficient way instead | # TODO: retrieve it dynamically in an efficient way instead | ||||
# of hardcoding it | # of hardcoding it | ||||
_swh_origin_types = ['git', 'svn', 'deb', 'hg', 'ftp', 'deposit', 'pypi'] | _swh_origin_types = ['git', 'svn', 'deb', 'hg', 'ftp', 'deposit', 'pypi'] | ||||
▲ Show 20 Lines • Show All 273 Lines • Show Last 20 Lines |