Skip to content

Commit

Permalink
WIP add pep 503 support for indices
Browse files Browse the repository at this point in the history
Signed-off-by: Eli Uriegas <[email protected]>
  • Loading branch information
seemethere committed Dec 21, 2020
1 parent 53a11b4 commit c4d0132
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions s3_management/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
S3 = boto3.resource('s3')
CLIENT = boto3.client('s3')
BUCKET = S3.Bucket('pytorch')
BASE_URL = "https://download.pytorch.org"

ACCEPTED_FILE_EXTENSIONS = ("whl", "zip")
ACCEPTED_SUBDIR_PATTERNS = [
Expand Down Expand Up @@ -85,6 +86,47 @@ def normalize_package_version(self: S3IndexType, obj: str) -> str:
"-".join(path.basename(obj).split("-")[:2])
)

def pep503_normalize(self, obj: str) -> str:
return sub(r"[-_.]+", "-", obj).lower()

def to_pep503_html(self) -> Dict[str, str]:
objects = (
self.nightly_packages_to_show() if self.prefix == 'whl/nightly'
else self.objects
)
links: Dict[str, List[str]] = defaultdict(list)
for obj in objects:
obj_base = path.basename(obj)
package_name = self.pep503_normalize(
self.normalize_package_version(obj).split("-")[0]
)
# TODO: Add hash function here
links[package_name].append(
f'<a href="{BASE_URL}/{obj}">{obj_base}</a><br/>'
)
out: Dict[str, str] = {
f"{package_name}/index.html": "\n".join(contents)
for package_name, contents in links.items()
}
out["index.html"] = "\n".join([
f'<a href="{package_name}/">{package_name}</a><br/>'
for package_name in links.keys()
])
return out

def upload_pep503_html(self) -> None:
html = self.to_pep503_html()
for index_name, content in html.items():
print(f"INFO Uploading {index_name}")
BUCKET.Object(
key=f"simple/{self.prefix}/{index_name}"
).put(
ACL='public-read',
CacheControl='no-cache,no-store,must-revalidate',
ContentType='text/html',
Body=content
)

def to_legacy_html(
self,
subdir: Optional[str]=None
Expand Down

0 comments on commit c4d0132

Please sign in to comment.