Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/npm/loader.py
Show First 20 Lines • Show All 135 Lines • ▼ Show 20 Lines | def get_metadata_authority(self): | ||||
) | ) | ||||
def get_package_info(self, version: str) -> Iterator[Tuple[str, NpmPackageInfo]]: | def get_package_info(self, version: str) -> Iterator[Tuple[str, NpmPackageInfo]]: | ||||
p_info = NpmPackageInfo.from_metadata( | p_info = NpmPackageInfo.from_metadata( | ||||
project_metadata=self.info(), version=version | project_metadata=self.info(), version=version | ||||
) | ) | ||||
yield release_name(version), p_info | yield release_name(version), p_info | ||||
@staticmethod | |||||
def known_artifact_to_extid(known_artifact: Dict) -> Optional[PartialExtID]: | |||||
extid_str = _artifact_to_sha1(known_artifact) | |||||
if extid_str is None: | |||||
return None | |||||
try: | |||||
return (EXTID_TYPE, hash_to_bytes(extid_str)) | |||||
except ValueError: | |||||
return None | |||||
def build_revision( | def build_revision( | ||||
self, p_info: NpmPackageInfo, uncompressed_path: str, directory: Sha1Git | self, p_info: NpmPackageInfo, uncompressed_path: str, directory: Sha1Git | ||||
) -> Optional[Revision]: | ) -> Optional[Revision]: | ||||
i_metadata = extract_intrinsic_metadata(uncompressed_path) | i_metadata = extract_intrinsic_metadata(uncompressed_path) | ||||
if not i_metadata: | if not i_metadata: | ||||
return None | return None | ||||
author = extract_npm_package_author(i_metadata) | author = extract_npm_package_author(i_metadata) | ||||
message = i_metadata["version"].encode("ascii") | message = i_metadata["version"].encode("ascii") | ||||
Show All 16 Lines | ) -> Optional[Revision]: | ||||
message=message, | message=message, | ||||
author=author, | author=author, | ||||
date=date, | date=date, | ||||
committer=author, | committer=author, | ||||
committer_date=date, | committer_date=date, | ||||
parents=(), | parents=(), | ||||
directory=directory, | directory=directory, | ||||
synthetic=True, | synthetic=True, | ||||
metadata={ | |||||
"intrinsic": {"tool": "package.json", "raw": i_metadata,}, | |||||
"extrinsic": { | |||||
"provider": self.provider_url, | |||||
"when": self.visit_date.isoformat(), | |||||
"raw": p_info.raw_info, | |||||
}, | |||||
}, | |||||
) | ) | ||||
return r | return r | ||||
def _artifact_to_sha1(known_artifact: Dict) -> Optional[str]: | |||||
"""Returns the sha1 from an NPM 'original_artifact' dict | |||||
The following code allows to deal with 2 metadata formats: | |||||
- old format sample:: | |||||
{ | |||||
'package_source': { | |||||
'sha1': '05181c12cd8c22035dd31155656826b85745da37', | |||||
} | |||||
} | |||||
- new format sample:: | |||||
{ | |||||
'original_artifact': [{ | |||||
'checksums': { | |||||
'sha256': '6975816f2c5ad4046acc676ba112f2fff945b01522d63948531f11f11e0892ec', # noqa | |||||
... | |||||
}, | |||||
}], | |||||
... | |||||
} | |||||
""" | |||||
known_original_artifact = known_artifact.get("original_artifact") | |||||
if not known_original_artifact: | |||||
# previous loader-npm version kept original artifact elsewhere | |||||
known_original_artifact = known_artifact.get("package_source") | |||||
if not known_original_artifact: | |||||
return None | |||||
return known_original_artifact["sha1"] | |||||
else: | |||||
assert isinstance(known_original_artifact, list) | |||||
return known_original_artifact[0]["checksums"]["sha1"] | |||||
def _author_str(author_data: Union[Dict, List, str]) -> str: | def _author_str(author_data: Union[Dict, List, str]) -> str: | ||||
"""Parse author from package.json author fields | """Parse author from package.json author fields | ||||
""" | """ | ||||
if isinstance(author_data, dict): | if isinstance(author_data, dict): | ||||
author_str = "" | author_str = "" | ||||
name = author_data.get("name") | name = author_data.get("name") | ||||
if name is not None: | if name is not None: | ||||
▲ Show 20 Lines • Show All 108 Lines • Show Last 20 Lines |