-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.py
More file actions
73 lines (54 loc) · 1.99 KB
/
upload.py
File metadata and controls
73 lines (54 loc) · 1.99 KB
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
71
72
73
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "httpx",
# "tqdm",
# ]
# ///
import argparse
import asyncio
import os
from pathlib import Path
import httpx
from tqdm.asyncio import tqdm_asyncio
BUNNY_BASE_URL = os.getenv("BUNNY_BASE_URL")
STORAGE_ZONE = os.getenv("BUNNY_STORAGE_ZONE")
ACCESS_KEY = os.getenv("BUNNY_API_KEY")
MAX_CONCURRENT = 100
def get_files(subdir: str | None = None):
base_path = Path("site/benchmarks")
search_path = base_path / subdir if subdir else base_path
files = []
for f in search_path.rglob("*"):
if f.is_file() and not f.name.startswith("."):
rel_path = str(f.relative_to(base_path)).replace("\\", "/")
files.append((f, rel_path))
return files
async def upload_file(client, sem, local_path, remote_path):
async with sem:
url = f"https://{BUNNY_BASE_URL}/{STORAGE_ZONE}/benchmarks/{remote_path}"
headers = {"AccessKey": ACCESS_KEY, "Content-Type": "application/octet-stream"}
with open(local_path, "rb") as f:
content = f.read()
response = await client.put(url, content=content, headers=headers)
return response.status_code == 201
async def main():
parser = argparse.ArgumentParser(description="Upload benchmark files to Bunny CDN")
parser.add_argument(
"--subdir",
help="Subdirectory within site/benchmarks/ to upload (e.g., 'strategies')",
)
args = parser.parse_args()
files = get_files(args.subdir)
if not files:
print("No files to upload")
return
print(f"Uploading {len(files)} files...")
async with httpx.AsyncClient(timeout=60.0) as client:
sem = asyncio.Semaphore(MAX_CONCURRENT)
tasks = [upload_file(client, sem, local, remote) for local, remote in files]
results = await tqdm_asyncio.gather(*tasks, desc="Uploading", unit="file")
success = sum(results)
print(f"Done: {success}/{len(files)} uploaded")
if __name__ == "__main__":
asyncio.run(main())