handouts/tools/build/publish.py

79 lines
1.5 KiB
Python
Raw Normal View History

2025-01-21 22:43:56 -08:00
from typing import TypedDict
from pathlib import Path
import requests
import shutil
import json
import os
from requests.auth import HTTPBasicAuth
URL="https://git.betalupi.com"
USER = os.environ['USER']
2025-01-21 22:53:45 -08:00
PACKAGE = os.environ['PACKAGE']
2025-01-21 22:43:56 -08:00
VERSION = os.environ['VERSION']
AUTH = HTTPBasicAuth(USER, os.environ['PUBLISH_KEY'])
ROOT: Path = Path(os.getcwd())
SRC_DIR: Path = ROOT / "output"
def log(msg):
print(f"[PUBLISH.PY] {msg}")
2025-01-21 22:58:00 -08:00
log(f"Version is {VERSION}")
log(f"Package is is {PACKAGE}")
2025-01-21 22:43:56 -08:00
log(f"Running in {ROOT}")
if not ROOT.is_dir():
log("Root is not a directory, cannot continue")
exit(1)
2025-01-21 22:58:00 -08:00
log(f"Source dir is {SRC_DIR}")
2025-01-21 22:43:56 -08:00
if not SRC_DIR.exists():
2025-01-21 22:58:00 -08:00
log("Source dir doesn't exist, cannot continue")
2025-01-21 22:43:56 -08:00
exit(1)
2025-01-21 23:09:28 -08:00
def upload(data, target: str):
2025-01-21 22:58:00 -08:00
requests.put(
f"{URL}/api/packages/{USER}/generic/{PACKAGE}/{VERSION}/{target}",
auth=AUTH,
data=data
)
return f"{URL}/api/packages/{USER}/generic/ormc-handouts/{VERSION}/{target}"
2025-01-21 22:43:56 -08:00
index = SRC_DIR / "index.json"
with index.open("r") as f:
index = json.load(f)
2025-01-21 22:48:32 -08:00
new_index = []
2025-01-21 22:43:56 -08:00
for handout in index:
title = handout["title"]
group = handout["group"]
h_file = SRC_DIR/handout["handout_file"]
s_file = SRC_DIR/handout["handout_file"]
2025-01-21 22:48:32 -08:00
h_url = None
s_url = None
2025-01-21 22:58:00 -08:00
h_url = upload(
h_file.open('rb').read(),
f"{group} - {title}.pdf"
2025-01-21 22:48:32 -08:00
)
2025-01-21 22:43:56 -08:00
if s_file is not None:
2025-01-21 22:58:00 -08:00
s_url = upload(
s_file.open('rb').read(),
f"{group} - {title}.sols.pdf"
2025-01-21 22:48:32 -08:00
)
log(f"Published {title}")
new_index.append({
"title": title,
"group": group,
"handout": h_url,
"solutions": s_url
})
2025-01-21 22:43:56 -08:00
2025-01-21 22:58:00 -08:00
upload(
json.dumps(new_index),
"index.json"
2025-01-21 22:48:32 -08:00
)