run_aws_listing.py 1.87 KB
Newer Older
1
import argparse
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
2
from collections import defaultdict
3
4
5
6
7
8
9
10
11
12

from boto3 import client

parser = argparse.ArgumentParser()
parser.add_argument("--access_key_id", type=str, required=True)
parser.add_argument("--secret_access_key", type=str, required=True)
parser.add_argument("--bucket", type=str, required=True)
parser.add_argument("--region", type=str, required=True)
args = parser.parse_args()

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
13
14
15
16
17
18
19
20
ROOT_URL = f"https://{args.bucket}.s3.{args.region}.amazonaws.com/whl"
html = "<!DOCTYPE html>\n<html>\n<body>\n{}\n</body>\n</html>"
href = '  <a href="{}">{}</a><br/>'
args = {
    "ContentType": "text/html",
    "CacheControl": "max-age=300",
    "ACL": "public-read",
}
21
22
23
24
25
26
27

s3 = client(
    "s3",
    aws_access_key_id=args.access_key_id,
    aws_secret_access_key=args.secret_access_key,
)

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
bucket = s3.Bucket(name="nerfacc-bucket")

wheels_dict = defaultdict(list)
for obj in bucket.objects.filter(Prefix="whl"):
    if obj.key[-3:] != "whl":
        continue
    torch_version, wheel = obj.key.split("/")[-2:]
    wheel = f"{torch_version}/{wheel}"
    wheels_dict[torch_version].append(wheel)

index_html = html.format(
    "\n".join(
        [
            href.format(f"{torch_version}.html".replace("+", "%2B"), version)
            for version in wheels_dict
        ]
    )
)

with open("index.html", "w") as f:
    f.write(index_html)
bucket.Object("whl/index.html").upload_file("index.html", args)

for torch_version, wheel_names in wheels_dict.items():
    torch_version_html = html.format(
        "\n".join(
            [
                href.format(
                    f"{ROOT_URL}/{wheel_name}".replace("+", "%2B"), wheel_name
                )
                for wheel_name in wheel_names
            ]
        )
    )

    with open(f"{torch_version}.html", "w") as f:
        f.write(torch_version_html)
    bucket.Object(f"whl/{torch_version}.html").upload_file(
        f"{torch_version}.html", args
67
    )