Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/cran/loader.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019-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 General Public License version 3, or any later version | # License: GNU 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 dateutil.parser | import dateutil.parser | ||||
import datetime | import datetime | ||||
import os | import os | ||||
import logging | import logging | ||||
Show All 16 Lines | |||||
DATE_PATTERN = re.compile(r'^(?P<year>\d{4})-(?P<month>\d{2})$') | DATE_PATTERN = re.compile(r'^(?P<year>\d{4})-(?P<month>\d{2})$') | ||||
class CRANLoader(PackageLoader): | class CRANLoader(PackageLoader): | ||||
visit_type = 'cran' | visit_type = 'cran' | ||||
def __init__(self, url: str, version: str): | def __init__(self, url: str, artifacts: List[Dict]): | ||||
"""Loader constructor. | """Loader constructor. | ||||
Args: | Args: | ||||
url: Origin url to retrieve cran artifact from | url: Origin url to retrieve cran artifact(s) from | ||||
version: version of the cran artifact | artifacts: List of associated artifact for the origin url | ||||
""" | """ | ||||
super().__init__(url=url) | super().__init__(url=url) | ||||
self.version = version | |||||
# explicit what we consider the artifact identity | # explicit what we consider the artifact identity | ||||
self.id_keys = ['url', 'version'] | self.id_keys = ['url', 'version'] | ||||
self.artifact = {'url': url, 'version': version} | self.artifacts = artifacts | ||||
def get_versions(self) -> List[str]: | def get_versions(self) -> List[str]: | ||||
# only 1 artifact | versions = [] | ||||
return [self.version] | for artifact in self.artifacts: | ||||
versions.append(artifact['version']) | |||||
return versions | |||||
def get_default_version(self) -> str: | def get_default_version(self) -> str: | ||||
return self.version | return self.artifacts[-1]['version'] | ||||
def get_package_info(self, version: str) -> Generator[ | def get_package_info(self, version: str) -> Generator[ | ||||
Tuple[str, Dict[str, Any]], None, None]: | Tuple[str, Dict[str, Any]], None, None]: | ||||
for a_metadata in self.artifacts: | |||||
url = a_metadata['url'] | |||||
package_version = a_metadata['version'] | |||||
if version == package_version: | |||||
p_info = { | p_info = { | ||||
'url': self.url, | 'url': url, | ||||
'filename': path.basename(self.url), | 'filename': path.basename(url), | ||||
'raw': self.artifact, | 'raw': a_metadata, | ||||
} | } | ||||
yield release_name(version), p_info | yield release_name(version), p_info | ||||
def resolve_revision_from( | def resolve_revision_from( | ||||
self, known_artifacts: Mapping[bytes, Mapping], | self, known_artifacts: Mapping[bytes, Mapping], | ||||
artifact_metadata: Mapping[str, Any]) \ | artifact_metadata: Mapping[str, Any]) \ | ||||
-> Optional[bytes]: | -> Optional[bytes]: | ||||
"""Given known_artifacts per revision, try to determine the revision for | """Given known_artifacts per revision, try to determine the revision for | ||||
artifact_metadata | artifact_metadata | ||||
Show All 9 Lines | class CRANLoader(PackageLoader): | ||||
def build_revision( | def build_revision( | ||||
self, a_metadata: Mapping[str, Any], | self, a_metadata: Mapping[str, Any], | ||||
uncompressed_path: str) -> Dict[str, Any]: | uncompressed_path: str) -> Dict[str, Any]: | ||||
# a_metadata is empty | # a_metadata is empty | ||||
metadata = extract_intrinsic_metadata(uncompressed_path) | metadata = extract_intrinsic_metadata(uncompressed_path) | ||||
normalized_date = normalize_timestamp(parse_date(metadata.get('Date'))) | normalized_date = normalize_timestamp(parse_date(metadata.get('Date'))) | ||||
author = swh_author(parse_author(metadata.get('Maintainer', {}))) | author = swh_author(parse_author(metadata.get('Maintainer', {}))) | ||||
version = metadata.get('Version', self.version) | version = metadata.get('Version', a_metadata['version']) | ||||
return { | return { | ||||
'message': version.encode('utf-8'), | 'message': version.encode('utf-8'), | ||||
'type': 'tar', | 'type': 'tar', | ||||
'date': normalized_date, | 'date': normalized_date, | ||||
'author': author, | 'author': author, | ||||
'committer': author, | 'committer': author, | ||||
'committer_date': normalized_date, | 'committer_date': normalized_date, | ||||
'parents': [], | 'parents': [], | ||||
▲ Show 20 Lines • Show All 85 Lines • Show Last 20 Lines |