Publish package
This commit is contained in:
@ -126,6 +126,9 @@ def build_typst(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
return None
|
||||
meta = read_meta_toml(meta_path)
|
||||
|
||||
handout_file = sanitize_file_name(f"{meta['title']}.pdf")
|
||||
solutions_file = sanitize_file_name(f"{meta['title']}.sols.pdf")
|
||||
|
||||
# Do nothing if not published
|
||||
if not meta["publish_handout"]:
|
||||
return None
|
||||
@ -136,7 +139,6 @@ def build_typst(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
out = OUT_DIR / out_subdir
|
||||
out.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
file_name = sanitize_file_name(f"{meta['title']}.pdf")
|
||||
res = subprocess.run(
|
||||
[
|
||||
TYPST_PATH,
|
||||
@ -145,7 +147,7 @@ def build_typst(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
"main.typ",
|
||||
"--input",
|
||||
"show_solutions=false",
|
||||
f"{out}/{file_name}",
|
||||
f"{out}/{handout_file}",
|
||||
],
|
||||
cwd=source_dir,
|
||||
stdout=subprocess.PIPE,
|
||||
@ -158,14 +160,13 @@ def build_typst(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
# Build solutions
|
||||
if meta["publish_solutions"]:
|
||||
log(f"Building typst (solutions): {source_dir}")
|
||||
file_name = sanitize_file_name(f"{meta['title']}.sols.pdf")
|
||||
res = subprocess.run(
|
||||
[
|
||||
TYPST_PATH,
|
||||
"compile",
|
||||
"--ignore-system-fonts",
|
||||
"main.typ",
|
||||
f"{out}/{file_name}",
|
||||
f"{out}/{solutions_file}",
|
||||
],
|
||||
cwd=source_dir,
|
||||
stdout=subprocess.PIPE,
|
||||
@ -178,11 +179,9 @@ def build_typst(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
return {
|
||||
"title": meta["title"],
|
||||
"group": str(out_subdir),
|
||||
"handout_file": f"{out_subdir}/{meta['title']}.pdf",
|
||||
"handout_file": str(out / handout_file),
|
||||
"solutions_file": (
|
||||
f"{out_subdir}/{meta['title']}.sols.pdf"
|
||||
if meta["publish_solutions"]
|
||||
else None
|
||||
str(out / solutions_file) if meta["publish_solutions"] else None
|
||||
),
|
||||
}
|
||||
|
||||
@ -198,6 +197,9 @@ def build_xetex(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
return None
|
||||
meta = read_meta_toml(meta_path)
|
||||
|
||||
handout_file = sanitize_file_name(f"{meta['title']}.pdf")
|
||||
solutions_file = sanitize_file_name(f"{meta['title']}.sols.pdf")
|
||||
|
||||
# Do nothing if not published
|
||||
if not meta["publish_handout"]:
|
||||
return None
|
||||
@ -219,9 +221,8 @@ def build_xetex(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
stderr=subprocess.PIPE,
|
||||
)
|
||||
|
||||
file_name = sanitize_file_name(f"{meta['title']}.pdf")
|
||||
try:
|
||||
shutil.copy(source_dir / "main.pdf", f"{out}/{file_name}")
|
||||
shutil.copy(source_dir / "main.pdf", f"{out}/{handout_file}")
|
||||
except Exception as e:
|
||||
log(f"Error: {e}")
|
||||
log_error(res)
|
||||
@ -243,9 +244,8 @@ def build_xetex(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
stderr=subprocess.PIPE,
|
||||
)
|
||||
|
||||
file_name = sanitize_file_name(f"{meta['title']}.sols.pdf")
|
||||
try:
|
||||
shutil.copy(source_dir / "main.pdf", f"{out}/{file_name}")
|
||||
shutil.copy(source_dir / "main.pdf", f"{out}/{solutions_file}")
|
||||
except Exception as e:
|
||||
log(f"Error: {e}")
|
||||
log_error(res)
|
||||
@ -256,11 +256,9 @@ def build_xetex(source_dir: Path, out_subdir: Path) -> IndexEntry | None:
|
||||
return {
|
||||
"title": meta["title"],
|
||||
"group": str(out_subdir),
|
||||
"handout_file": f"{out_subdir}/{meta['title']}.pdf",
|
||||
"handout_file": str(out / handout_file),
|
||||
"solutions_file": (
|
||||
f"{out_subdir}/{meta['title']}.sols.pdf"
|
||||
if meta["publish_solutions"]
|
||||
else None
|
||||
str(out / solutions_file) if meta["publish_solutions"] else None
|
||||
),
|
||||
}
|
||||
|
78
tools/scripts/publish.py
Normal file
78
tools/scripts/publish.py
Normal file
@ -0,0 +1,78 @@
|
||||
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)
|
||||
|
||||
log(f"Deleting existing package {SRC_DIR}")
|
||||
res = requests.delete(
|
||||
f"{URL}/api/packages/{USER}/generic/{PACKAGE}/{VERSION}",
|
||||
auth=AUTH,
|
||||
)
|
||||
if res.status_code != 204:
|
||||
log(f"Deletion failed with code {res.status_code}, this is ok")
|
||||
|
||||
|
||||
def upload(data, target: str):
|
||||
res = requests.put(
|
||||
f"{URL}/api/packages/{USER}/generic/{PACKAGE}/{VERSION}/{target}",
|
||||
auth=AUTH,
|
||||
data=data,
|
||||
)
|
||||
|
||||
if res.status_code != 201:
|
||||
log(f"Upload failed with code {res.status_code}")
|
||||
exit(1)
|
||||
|
||||
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"]
|
||||
log(f"Uploading {title}")
|
||||
|
||||
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")
|
||||
|
||||
new_index.append(
|
||||
{"title": title, "group": group, "handout": h_url, "solutions": s_url}
|
||||
)
|
||||
|
||||
upload(json.dumps(new_index), "index.json")
|
@ -2,6 +2,7 @@ exclude = ["venv"]
|
||||
line-length = 88
|
||||
indent-width = 4
|
||||
target-version = "py39"
|
||||
include = ["scripts/**/*.py"]
|
||||
|
||||
[lint]
|
||||
select = ["E4", "E7", "E9", "F"]
|
Reference in New Issue
Block a user