Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/pypi/loader.py
Show All 35 Lines | |||||
EXTID_TYPE = "pypi-archive-sha256" | EXTID_TYPE = "pypi-archive-sha256" | ||||
@attr.s | @attr.s | ||||
class PyPIPackageInfo(BasePackageInfo): | class PyPIPackageInfo(BasePackageInfo): | ||||
raw_info = attr.ib(type=Dict[str, Any]) | raw_info = attr.ib(type=Dict[str, Any]) | ||||
name = attr.ib(type=str) | |||||
comment_text = attr.ib(type=Optional[str]) | comment_text = attr.ib(type=Optional[str]) | ||||
sha256 = attr.ib(type=str) | sha256 = attr.ib(type=str) | ||||
upload_time = attr.ib(type=str) | upload_time = attr.ib(type=str) | ||||
@classmethod | @classmethod | ||||
def from_metadata(cls, metadata: Dict[str, Any], version: str) -> "PyPIPackageInfo": | def from_metadata( | ||||
cls, metadata: Dict[str, Any], name: str, version: str | |||||
) -> "PyPIPackageInfo": | |||||
return cls( | return cls( | ||||
url=metadata["url"], | url=metadata["url"], | ||||
filename=metadata["filename"], | filename=metadata["filename"], | ||||
version=version, | version=version, | ||||
raw_info=metadata, | raw_info=metadata, | ||||
name=name, | |||||
comment_text=metadata.get("comment_text"), | comment_text=metadata.get("comment_text"), | ||||
sha256=metadata["digests"]["sha256"], | sha256=metadata["digests"]["sha256"], | ||||
upload_time=metadata["upload_time"], | upload_time=metadata["upload_time"], | ||||
directory_extrinsic_metadata=[ | directory_extrinsic_metadata=[ | ||||
RawExtrinsicMetadataCore( | RawExtrinsicMetadataCore( | ||||
format="pypi-project-json", metadata=json.dumps(metadata).encode(), | format="pypi-project-json", metadata=json.dumps(metadata).encode(), | ||||
) | ) | ||||
], | ], | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | def get_package_info(self, version: str) -> Iterator[Tuple[str, PyPIPackageInfo]]: | ||||
res = [] | res = [] | ||||
for meta in self.info()["releases"][version]: | for meta in self.info()["releases"][version]: | ||||
# process only standard sdist archives | # process only standard sdist archives | ||||
if meta["packagetype"] != "sdist" or meta["filename"].lower().endswith( | if meta["packagetype"] != "sdist" or meta["filename"].lower().endswith( | ||||
(".deb", ".egg", ".rpm", ".whl") | (".deb", ".egg", ".rpm", ".whl") | ||||
): | ): | ||||
continue | continue | ||||
p_info = PyPIPackageInfo.from_metadata(meta, version=version) | p_info = PyPIPackageInfo.from_metadata( | ||||
meta, name=self.info()["info"]["name"], version=version | |||||
) | |||||
res.append((version, p_info)) | res.append((version, p_info)) | ||||
if len(res) == 1: | if len(res) == 1: | ||||
version, p_info = res[0] | version, p_info = res[0] | ||||
yield release_name(version), p_info | yield release_name(version), p_info | ||||
else: | else: | ||||
for version, p_info in res: | for version, p_info in res: | ||||
yield release_name(version, p_info.filename), p_info | yield release_name(version, p_info.filename), p_info | ||||
def build_release( | def build_release( | ||||
self, p_info: PyPIPackageInfo, uncompressed_path: str, directory: Sha1Git | self, p_info: PyPIPackageInfo, uncompressed_path: str, directory: Sha1Git | ||||
) -> Optional[Release]: | ) -> Optional[Release]: | ||||
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 | ||||
# from intrinsic metadata | # from intrinsic metadata | ||||
version_ = i_metadata.get("version", "") | version_ = i_metadata.get("version", p_info.version) | ||||
author_ = author(i_metadata) | author_ = author(i_metadata) | ||||
# from extrinsic metadata | if p_info.comment_text: | ||||
message = p_info.comment_text or "" | msg = p_info.comment_text | ||||
message = "%s: %s" % (version_, message) if message else version_ | else: | ||||
msg = ( | |||||
f"Synthetic release for PyPI source package {p_info.name} " | |||||
f"version {version_}" | |||||
) | |||||
date = TimestampWithTimezone.from_iso8601(p_info.upload_time) | date = TimestampWithTimezone.from_iso8601(p_info.upload_time) | ||||
return Release( | return Release( | ||||
name=p_info.version.encode(), | name=p_info.version.encode(), | ||||
message=message.encode(), | message=msg.encode(), | ||||
author=author_, | author=author_, | ||||
date=date, | date=date, | ||||
target=directory, | target=directory, | ||||
target_type=ObjectType.DIRECTORY, | target_type=ObjectType.DIRECTORY, | ||||
synthetic=True, | synthetic=True, | ||||
) | ) | ||||
▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines |