Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/svn/converters.py
# Copyright (C) 2015-2020 The Software Heritage developers | # Copyright (C) 2015-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 | ||||
from typing import Any, Dict, List, Optional, Union | from typing import Any, Dict, List, Optional | ||||
from email import utils | |||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Person, Revision, RevisionType, TimestampWithTimezone | Person, Revision, RevisionType, TimestampWithTimezone | ||||
) | ) | ||||
from swh.loader.package.utils import EMPTY_AUTHOR | |||||
from .utils import strdate_to_timestamp | from .utils import strdate_to_timestamp | ||||
def svn_date_to_swh_date( | def svn_date_to_swh_date( | ||||
strdate: Optional[str]) -> TimestampWithTimezone: | strdate: Optional[str]) -> TimestampWithTimezone: | ||||
"""Convert a string date to an swh one. | """Convert a string date to an swh one. | ||||
Args: | Args: | ||||
strdate: A string formatted for .utils.strdate_to_timestamp | strdate: A string formatted for .utils.strdate_to_timestamp | ||||
to do its jobs | to do its jobs | ||||
Returns: | Returns: | ||||
An swh date format | An swh date format | ||||
""" | """ | ||||
return TimestampWithTimezone( | return TimestampWithTimezone( | ||||
timestamp=strdate_to_timestamp(strdate), | timestamp=strdate_to_timestamp(strdate), | ||||
offset=0, | offset=0, | ||||
negative_utc=False, | negative_utc=False, | ||||
) | ) | ||||
def svn_author_to_swh_person(author: Union[str, bytes]) -> Person: | def svn_author_to_swh_person(author: Optional[bytes]) -> Person: | ||||
"""Convert an svn author to an swh person. | """Convert an svn author to an swh person. | ||||
Default policy: No information is added. | Default policy: No information is added. | ||||
Args: | Args: | ||||
author (string): the svn author (in bytes) | author: the svn author (in bytes) | ||||
Returns: | Returns: | ||||
a Person | a Person | ||||
""" | """ | ||||
# TODO: Align this function and move it up as library helper function | return Person.from_fullname(author or b'') | ||||
if not author: | |||||
return EMPTY_AUTHOR | |||||
if isinstance(author, str): | |||||
author = author.encode('utf-8') | |||||
if b'<' in author and b'>' in author: | |||||
name, email = utils.parseaddr(author.decode('utf-8')) | |||||
return Person( | |||||
fullname=author, | |||||
name=name.encode('utf-8'), | |||||
email=email.encode('utf-8') | |||||
) | |||||
return Person(fullname=author, name=author, email=None) | |||||
def build_swh_revision( | def build_swh_revision( | ||||
rev: int, commit: Dict, repo_uuid: str, dir_id: bytes, | rev: int, commit: Dict, repo_uuid: str, dir_id: bytes, | ||||
parents: List[bytes]) -> Revision: | parents: List[bytes]) -> Revision: | ||||
"""Given a svn revision, build a swh revision. | """Given a svn revision, build a swh revision. | ||||
This adds an ['metadata']['extra-headers'] entry with the | This adds an ['metadata']['extra-headers'] entry with the | ||||
Show All 36 Lines |