-
Notifications
You must be signed in to change notification settings - Fork 1
/
cdnify.py
executable file
·70 lines (55 loc) · 2.11 KB
/
cdnify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
67
68
69
70
#!/usr/bin/env python3
"""Upload assets to S3 and produce algobowl/lib/algocdn.py.
This gets called prior to building a wheel, that way we can remove assets from
the wheel and source distributions on PyPI.
"""
import hashlib
from pathlib import Path
import click
import s3fs
HERE = Path(__file__).resolve().parent
def get_assets_hash(public_dir: Path) -> str:
hasher = hashlib.md5(usedforsecurity=False)
for path in sorted(public_dir.glob("**/*")):
if not path.is_file():
continue
hasher.update(path.read_bytes())
return hasher.hexdigest()
def write_py_out(f, url_map):
print("# Auto-generated by cdnify.py", file=f)
print(f"url_map = {url_map!r}", file=f)
@click.command()
@click.option("--access-key", envvar="S3_ACCESS_KEY", required=True)
@click.option("--secret-key", envvar="S3_SECRET_KEY", required=True)
@click.option("--bucket", envvar="S3_BUCKET", default="algocdn")
@click.option(
"--endpoint-url",
envvar="S3_ENDPOINT_URL",
default="https://s3.us-west-004.backblazeb2.com",
)
@click.option(
"--public-url-prefix",
default="https://assets.algobowl.org/file/algocdn",
)
@click.option("--public-dir", type=Path, default=HERE / "algobowl" / "public")
@click.option("--py-out", type=Path, default=HERE / "algobowl" / "lib" / "algocdn.py")
def main(
access_key, secret_key, bucket, endpoint_url, public_dir, public_url_prefix, py_out
):
s3 = s3fs.S3FileSystem(endpoint_url=endpoint_url, key=access_key, secret=secret_key)
url_map = {}
assets_hash = get_assets_hash(public_dir)
for path in public_dir.glob("**/*"):
if not path.is_file():
continue
relative_url = f"/{path.relative_to(public_dir)}"
remote_path = f"{assets_hash}{relative_url}"
s3_uri = f"s3://{bucket}/{remote_path}"
if not s3.exists(s3_uri):
click.echo(f"Upload {path} to {s3_uri}", err=True)
s3.put_file(path, s3_uri)
url_map[relative_url] = f"{public_url_prefix}/{remote_path}"
with open(py_out, "w", encoding="utf-8") as f:
write_py_out(f, url_map)
if __name__ == "__main__":
main()