handouts/tools/scripts/publish.py
Mark 66670d6751
Some checks failed
CI / Typst formatting (push) Successful in 12s
CI / Typos (push) Successful in 15s
CI / Build (push) Has been cancelled
Fix publish?
2025-01-22 08:34:57 -08:00

68 lines
1.6 KiB
Python

from pathlib import Path
import requests
import json
import os
URL = "https://git.betalupi.com"
USER = os.environ["PUBLISH_USER"]
PACKAGE = os.environ["PACKAGE"]
VERSION = os.environ["VERSION"]
AUTH = requests.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}")
log(f"Version is {VERSION}")
log(f"Package is {PACKAGE}")
log(f"Running in {ROOT}")
if not ROOT.is_dir():
log("Root is not a directory, cannot continue")
exit(1)
log(f"Source dir is {SRC_DIR}")
if not SRC_DIR.exists():
log("Source dir doesn't exist, cannot continue")
exit(1)
def upload(data, target: str):
requests.put(
f"{URL}/api/packages/{USER}/generic/{PACKAGE}/{VERSION}/{target}",
auth=AUTH,
data=data,
)
return f"{URL}/api/packages/{USER}/generic/{PACKAGE}/{VERSION}/{target}"
index_file = SRC_DIR / "index.json"
with index_file.open("r") as f:
index = json.load(f)
new_index = []
for handout in index:
title = handout["title"]
group = handout["group"]
h_file = SRC_DIR / handout["handout_file"]
s_file = SRC_DIR / handout["handout_file"]
h_url = None
s_url = None
h_url = upload(h_file.open("rb").read(), f"{group} - {title}.pdf")
if s_file is not None:
s_url = upload(s_file.open("rb").read(), f"{group} - {title}.sols.pdf")
log(f"Published {title} to {h_url}")
new_index.append(
{"title": title, "group": group, "handout": h_url, "solutions": s_url}
)
upload(json.dumps(new_index), "index.json")