Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/npm/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 json | import json | ||||
import logging | import logging | ||||
import os | import os | ||||
from codecs import BOM_UTF8 | from codecs import BOM_UTF8 | ||||
from typing import Any, Dict, Generator, Mapping, Sequence, Tuple, Optional | from typing import Any, Dict, Generator, List, Mapping, Optional, Sequence, Tuple, Union | ||||
import attr | import attr | ||||
import chardet | import chardet | ||||
from urllib.parse import quote | from urllib.parse import quote | ||||
from swh.model.model import ( | from swh.model.model import ( | ||||
Person, | Person, | ||||
RevisionType, | RevisionType, | ||||
▲ Show 20 Lines • Show All 156 Lines • ▼ Show 20 Lines | for rev_id, known_artifact in known_artifacts.items(): | ||||
else: | else: | ||||
assert isinstance(known_original_artifact, list) | assert isinstance(known_original_artifact, list) | ||||
original_hash = known_original_artifact[0]["checksums"]["sha1"] | original_hash = known_original_artifact[0]["checksums"]["sha1"] | ||||
if shasum == original_hash: | if shasum == original_hash: | ||||
return rev_id | return rev_id | ||||
return None | return None | ||||
def extract_npm_package_author(package_json) -> Person: | def _author_str(author_data: Union[Dict, List, str]) -> str: | ||||
"""Parse author from package.json author fields | |||||
""" | |||||
if isinstance(author_data, dict): | |||||
author_str = "" | |||||
ardumont: yeah... that won't work ;) | |||||
Done Inline Actionswas "email" instead of "name", fixed. ardumont: was "email" instead of "name", fixed. | |||||
name = author_data.get("name") | |||||
if name is not None: | |||||
author_str += name | |||||
email = author_data.get("email") | |||||
if email is not None: | |||||
author_str += f" <{email}>" | |||||
result = author_str | |||||
elif isinstance(author_data, list): | |||||
result = _author_str(author_data[0]) if len(author_data) > 0 else "" | |||||
else: | |||||
result = author_data | |||||
return result | |||||
def extract_npm_package_author(package_json: Dict[str, Any]) -> Person: | |||||
""" | """ | ||||
Extract package author from a ``package.json`` file content and | Extract package author from a ``package.json`` file content and | ||||
return it in swh format. | return it in swh format. | ||||
Args: | Args: | ||||
package_json (dict): Dict holding the content of parsed | package_json (dict): Dict holding the content of parsed | ||||
``package.json`` file | ``package.json`` file | ||||
Returns: | Returns: | ||||
Person | Person | ||||
""" | """ | ||||
def _author_str(author_data): | |||||
if type(author_data) is dict: | |||||
author_str = "" | |||||
if "name" in author_data: | |||||
author_str += author_data["name"] | |||||
if "email" in author_data: | |||||
author_str += " <%s>" % author_data["email"] | |||||
return author_str | |||||
elif type(author_data) is list: | |||||
return _author_str(author_data[0]) if len(author_data) > 0 else "" | |||||
else: | |||||
return author_data | |||||
for author_key in ("author", "authors"): | for author_key in ("author", "authors"): | ||||
if author_key in package_json: | if author_key in package_json: | ||||
author_str = _author_str(package_json[author_key]) | author_str = _author_str(package_json[author_key]) | ||||
return Person.from_fullname(author_str.encode()) | return Person.from_fullname(author_str.encode()) | ||||
return Person(fullname=b"", name=None, email=None) | return Person(fullname=b"", name=None, email=None) | ||||
▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines |
yeah... that won't work ;)