From 66670d6751dd28df4fa3605c84d3d561566df023 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 22 Jan 2025 08:34:57 -0800 Subject: [PATCH] Fix publish? --- .github/workflows/ci.yml | 5 +- tools/build/publish.py | 67 ----------------------- tools/{build/main.py => scripts/build.py} | 12 ++-- tools/scripts/publish.py | 67 +++++++++++++++++++++++ tools/{build => scripts}/ruff.toml | 0 5 files changed, 74 insertions(+), 77 deletions(-) delete mode 100644 tools/build/publish.py rename tools/{build/main.py => scripts/build.py} (96%) create mode 100644 tools/scripts/publish.py rename tools/{build => scripts}/ruff.toml (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 334f767..51f13c4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -3,6 +3,7 @@ name: CI on: push: pull_request: + workflow_dispatch: jobs: typos: @@ -67,7 +68,7 @@ jobs: # Builds all handouts, LaTeX and Typst - name: "Build handouts" - run: TYPST_PATH="$(pwd)/typst" python tools/build/main.py + run: TYPST_PATH="$(pwd)/typst" python tools/scripts/build.py # Upload logs, even if build fails. # LaTeX stdout/stderr isn't always helpful. @@ -94,4 +95,4 @@ jobs: PUBLISH_KEY="${{ secrets.PUBLISH_KEY }}" \ VERSION="${{ github.sha }}" \ PACKAGE="${{ vars.PACKAGE }}" \ - python tools/build/publish.py + python tools/scripts/publish.py diff --git a/tools/build/publish.py b/tools/build/publish.py deleted file mode 100644 index 02cf077..0000000 --- a/tools/build/publish.py +++ /dev/null @@ -1,67 +0,0 @@ -from pathlib import Path -import requests -import json -import os - -URL = "https://git.betalupi.com" -USER = os.environ["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") diff --git a/tools/build/main.py b/tools/scripts/build.py similarity index 96% rename from tools/build/main.py rename to tools/scripts/build.py index 04ea0af..536f48d 100644 --- a/tools/build/main.py +++ b/tools/scripts/build.py @@ -179,11 +179,9 @@ def build_typst(source_dir: Path, out_subdir: Path) -> IndexEntry | None: return { "title": meta["title"], "group": str(out_subdir), - "handout_file": str(out/handout_file), + "handout_file": str(out / handout_file), "solutions_file": ( - str(out/solutions_file) - if meta["publish_solutions"] - else None + str(out / solutions_file) if meta["publish_solutions"] else None ), } @@ -258,11 +256,9 @@ def build_xetex(source_dir: Path, out_subdir: Path) -> IndexEntry | None: return { "title": meta["title"], "group": str(out_subdir), - "handout_file": str(out/handout_file), + "handout_file": str(out / handout_file), "solutions_file": ( - str(out/solutions_file) - if meta["publish_solutions"] - else None + str(out / solutions_file) if meta["publish_solutions"] else None ), } diff --git a/tools/scripts/publish.py b/tools/scripts/publish.py new file mode 100644 index 0000000..95f1fa8 --- /dev/null +++ b/tools/scripts/publish.py @@ -0,0 +1,67 @@ +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") diff --git a/tools/build/ruff.toml b/tools/scripts/ruff.toml similarity index 100% rename from tools/build/ruff.toml rename to tools/scripts/ruff.toml