Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
[ghstack-poisoned]
  • Loading branch information
clee2000 committed Jan 3, 2025
2 parents 7f423f8 + 3cab629 commit 6ccbf03
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions s3_management/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from packaging.version import parse as _parse_version, Version, InvalidVersion

import boto3
import botocore


S3 = boto3.resource('s3')
Expand Down Expand Up @@ -217,6 +218,7 @@ class S3Object:
orig_key: str
checksum: Optional[str]
size: Optional[int]
pep658: Optional[str]

def __hash__(self):
return hash(self.key)
Expand Down Expand Up @@ -380,7 +382,16 @@ def to_simple_package_html(
out.append(' <h1>Links for {}</h1>'.format(package_name.lower().replace("_", "-")))
for obj in sorted(self.gen_file_list(subdir, package_name)):
maybe_fragment = f"#sha256={obj.checksum}" if obj.checksum else ""
out.append(f' <a href="/{obj.key}{maybe_fragment}">{path.basename(obj.key).replace("%2B","+")}</a><br/>')
pep658_attribute = ""
if obj.pep658:
pep658_sha = f"sha256={obj.pep658}"
# pep714 renames the attribute to data-core-metadata
pep658_attribute = (
f' data-dist-info-metadata="{pep658_sha}" data-core-metadata="{pep658_sha}"'
)
out.append(
f' <a href="/{obj.key}{maybe_fragment}"{pep658_attribute}>{path.basename(obj.key).replace("%2B","+")}</a><br/>'
)
# Adding html footer
out.append(' </body>')
out.append('</html>')
Expand Down Expand Up @@ -529,6 +540,32 @@ def fetch_metadata(self: S3IndexType) -> None:
if size := response.get("ContentLength"):
self.objects[idx].size = int(size)

def fetch_pep658(self: S3IndexType) -> None:
def _fetch_metadata(key: str) -> str:
try:
response = CLIENT.head_object(
Bucket=BUCKET.name, Key=f"{key}.metadata", ChecksumMode="Enabled"
)
sha256 = base64.b64decode(response.get("ChecksumSHA256")).hex()
return sha256
except botocore.exceptions.ClientError as e:
if e.response["Error"]["Code"] == "404":
return None
raise

with concurrent.futures.ThreadPoolExecutor(max_workers=6) as executor:
metadata_futures = {
idx: executor.submit(
_fetch_metadata,
obj.orig_key,
)
for (idx, obj) in enumerate(self.objects)
}
for idx, future in metadata_futures.items():
response = future.result()
if response is not None:
self.objects[idx].pep658 = response

@classmethod
def from_S3(cls: Type[S3IndexType], prefix: str, with_metadata: bool = True) -> S3IndexType:
prefix = prefix.rstrip("/")
Expand All @@ -540,11 +577,13 @@ def sanitize_key(key: str) -> str:
rc = cls([S3Object(key=sanitize_key(key),
orig_key=key,
checksum=None,
size=None) for key in obj_names], prefix)
size=None,
pep658=None) for key in obj_names], prefix)
if prefix == "whl/nightly":
rc.objects = rc.nightly_packages_to_show()
if with_metadata:
rc.fetch_metadata()
rc.fetch_pep658()
return rc

@classmethod
Expand Down

0 comments on commit 6ccbf03

Please sign in to comment.