Changeset View
Changeset View
Standalone View
Standalone View
swh/web/browse/utils.py
Show First 20 Lines • Show All 846 Lines • ▼ Show 20 Lines | for i, rev in enumerate(revision_log): | ||||
'date': author_date, | 'date': author_date, | ||||
'commit_date': committer_date, | 'commit_date': committer_date, | ||||
'url': gen_revision_url(rev['id'], snapshot_context), | 'url': gen_revision_url(rev['id'], snapshot_context), | ||||
'tooltip': tooltip | 'tooltip': tooltip | ||||
}) | }) | ||||
return revision_log_data | return revision_log_data | ||||
# list of origin types that can be found in the swh archive | def get_snapshot_context(snapshot_id=None, origin_url=None, | ||||
# TODO: retrieve it dynamically in an efficient way instead | |||||
# of hardcoding it | |||||
_swh_origin_types = ['git', 'svn', 'deb', 'hg', 'ftp', 'deposit', | |||||
'pypi', 'npm'] | |||||
def get_origin_info(origin_url, origin_type=None): | |||||
""" | |||||
Get info about a software origin. | |||||
Its main purpose is to automatically find an origin type | |||||
when it is not provided as parameter. | |||||
Args: | |||||
origin_url (str): complete url of a software origin | |||||
origin_type (str): optional origin type | |||||
Returns: | |||||
A dict with the following entries: | |||||
* type: the origin type | |||||
* url: the origin url | |||||
* id: the internal id of the origin | |||||
""" | |||||
if origin_type: | |||||
return service.lookup_origin({'type': origin_type, | |||||
'url': origin_url}) | |||||
else: | |||||
for origin_type in _swh_origin_types: | |||||
try: | |||||
origin_info = service.lookup_origin({'type': origin_type, | |||||
'url': origin_url}) | |||||
return origin_info | |||||
except Exception: | |||||
pass | |||||
raise NotFoundExc('Origin with url %s not found!' % escape(origin_url)) | |||||
def get_snapshot_context(snapshot_id=None, origin_type=None, origin_url=None, | |||||
timestamp=None, visit_id=None): | timestamp=None, visit_id=None): | ||||
""" | """ | ||||
Utility function to compute relevant information when navigating | Utility function to compute relevant information when navigating | ||||
the archive in a snapshot context. The snapshot is either | the archive in a snapshot context. The snapshot is either | ||||
referenced by its id or it will be retrieved from an origin visit. | referenced by its id or it will be retrieved from an origin visit. | ||||
Args: | Args: | ||||
snapshot_id (str): hexadecimal representation of a snapshot identifier, | snapshot_id (str): hexadecimal representation of a snapshot identifier, | ||||
all other parameters will be ignored if it is provided | all other parameters will be ignored if it is provided | ||||
origin_type (str): the origin type (git, svn, deposit, ...) | |||||
origin_url (str): the origin_url | origin_url (str): the origin_url | ||||
(e.g. https://github.com/(user)/(repo)/) | (e.g. https://github.com/(user)/(repo)/) | ||||
timestamp (str): a datetime string for retrieving the closest | timestamp (str): a datetime string for retrieving the closest | ||||
visit of the origin | visit of the origin | ||||
visit_id (int): optional visit id for disambiguation in case | visit_id (int): optional visit id for disambiguation in case | ||||
of several visits with the same timestamp | of several visits with the same timestamp | ||||
Returns: | Returns: | ||||
Show All 23 Lines | def get_snapshot_context(snapshot_id=None, origin_url=None, | ||||
releases = [] | releases = [] | ||||
browse_url = None | browse_url = None | ||||
visit_url = None | visit_url = None | ||||
branches_url = None | branches_url = None | ||||
releases_url = None | releases_url = None | ||||
swh_type = 'snapshot' | swh_type = 'snapshot' | ||||
if origin_url: | if origin_url: | ||||
swh_type = 'origin' | swh_type = 'origin' | ||||
origin_info = get_origin_info(origin_url, origin_type) | origin_info = service.lookup_origin({'url': origin_url}) | ||||
visit_info = get_origin_visit(origin_info, timestamp, visit_id, | visit_info = get_origin_visit(origin_info, timestamp, visit_id, | ||||
snapshot_id) | snapshot_id) | ||||
fmt_date = format_utc_iso_date(visit_info['date']) | fmt_date = format_utc_iso_date(visit_info['date']) | ||||
visit_info['fmt_date'] = fmt_date | visit_info['fmt_date'] = fmt_date | ||||
snapshot_id = visit_info['snapshot'] | snapshot_id = visit_info['snapshot'] | ||||
if not snapshot_id: | if not snapshot_id: | ||||
raise NotFoundExc('No snapshot associated to the visit of origin ' | raise NotFoundExc('No snapshot associated to the visit of origin ' | ||||
'%s on %s' % (escape(origin_url), fmt_date)) | '%s on %s' % (escape(origin_url), fmt_date)) | ||||
# provided timestamp is not necessarily equals to the one | # provided timestamp is not necessarily equals to the one | ||||
# of the retrieved visit, so get the exact one in order | # of the retrieved visit, so get the exact one in order | ||||
# use it in the urls generated below | # use it in the urls generated below | ||||
if timestamp: | if timestamp: | ||||
timestamp = visit_info['date'] | timestamp = visit_info['date'] | ||||
branches, releases = \ | branches, releases = \ | ||||
get_origin_visit_snapshot(origin_info, timestamp, visit_id, | get_origin_visit_snapshot(origin_info, timestamp, visit_id, | ||||
snapshot_id) | snapshot_id) | ||||
url_args = {'origin_type': origin_type, | url_args = {'origin_url': origin_info['url']} | ||||
'origin_url': origin_info['url']} | |||||
query_params = {'visit_id': visit_id} | query_params = {'visit_id': visit_id} | ||||
browse_url = reverse('browse-origin-visits', | browse_url = reverse('browse-origin-visits', | ||||
url_args=url_args) | url_args=url_args) | ||||
if timestamp: | if timestamp: | ||||
url_args['timestamp'] = format_utc_iso_date(timestamp, | url_args['timestamp'] = format_utc_iso_date(timestamp, | ||||
Show All 31 Lines | def get_snapshot_context(snapshot_id=None, origin_url=None, | ||||
return { | return { | ||||
'swh_type': swh_type, | 'swh_type': swh_type, | ||||
'swh_object_id': swh_snp_id, | 'swh_object_id': swh_snp_id, | ||||
'snapshot_id': snapshot_id, | 'snapshot_id': snapshot_id, | ||||
'snapshot_size': snapshot_size, | 'snapshot_size': snapshot_size, | ||||
'is_empty': is_empty, | 'is_empty': is_empty, | ||||
'origin_info': origin_info, | 'origin_info': origin_info, | ||||
# keep track if the origin type was provided as url argument | |||||
'origin_type': origin_type, | |||||
'visit_info': visit_info, | 'visit_info': visit_info, | ||||
'branches': branches, | 'branches': branches, | ||||
'releases': releases, | 'releases': releases, | ||||
'branch': None, | 'branch': None, | ||||
'release': None, | 'release': None, | ||||
'browse_url': browse_url, | 'browse_url': browse_url, | ||||
'branches_url': branches_url, | 'branches_url': branches_url, | ||||
'releases_url': releases_url, | 'releases_url': releases_url, | ||||
▲ Show 20 Lines • Show All 117 Lines • Show Last 20 Lines |