Moved back to sync and added logging

This commit is contained in:
alexander
2026-01-24 12:55:47 +01:00
parent e93e743f65
commit d81cd88cab
10 changed files with 217 additions and 77 deletions

View File

@@ -1,58 +1,57 @@
import asyncio
from jinja2 import Environment, FileSystemLoader, select_autoescape
import shutil
import datetime
import time
import os
import datetime
from sources import CSItem
from inventory import load_cheatsheet_inventory, prepare_cheatsheets
from config import get_settings
from logger import get_worker_thread_logger
INVENTORY_FILE = "cheatsheet_inventory.json"
STATIC_DIR = "static"
TEMPLATES_DIR = "templates"
OUTPUT_DIR = "out"
PROD_DIR = "prod"
def build(trigger_list: list[str] | None = None):
start_time = time.time()
settings = get_settings()
async def build(trigger_list: list[str] | None = None):
inv_raw = load_cheatsheet_inventory(INVENTORY_FILE)
inv_raw = load_cheatsheet_inventory(settings.paths.inventory_file)
# Clear output directory
shutil.rmtree(OUTPUT_DIR, ignore_errors=True)
shutil.copytree(STATIC_DIR, OUTPUT_DIR)
shutil.rmtree(settings.paths.output, ignore_errors=True)
shutil.copytree(settings.paths.static, settings.paths.output)
inv: list[CSItem] = prepare_cheatsheets(inv_raw, settings.paths.output)
inv: list[CSItem] = await prepare_cheatsheets(inv_raw, OUTPUT_DIR)
if not os.path.exists(PROD_DIR):
os.mkdir(PROD_DIR)
if not os.path.exists(settings.paths.prod):
os.mkdir(settings.paths.prod)
env = Environment(
loader=FileSystemLoader(TEMPLATES_DIR),
loader=FileSystemLoader(settings.paths.templates),
autoescape=select_autoescape()
)
index = env.get_template("index.html.j2")
print(f"{len(inv)} Cheatsheets")
logger = get_worker_thread_logger()
logger.info("Generated cheatsheets:")
for i in inv:
print("-", i)
logger.info(f"- {i}")
thisYear = datetime.datetime.now().year
with open(f"{OUTPUT_DIR}/index.html", "w", encoding="utf-8") as f:
with open(f"{settings.paths.output}/index.html", "w", encoding="utf-8") as f:
f.write(index.render(items=inv, thisYear=thisYear))
with open(f"{OUTPUT_DIR}/impressum.html", "w", encoding="utf-8") as f:
with open(f"{settings.paths.output}/impressum.html", "w", encoding="utf-8") as f:
f.write(env.get_template("impressum.html.j2").render(thisYear=thisYear))
with open(f"{OUTPUT_DIR}/license.html", "w", encoding="utf-8") as f:
with open(f"{settings.paths.output}/license.html", "w", encoding="utf-8") as f:
f.write(env.get_template("license.html.j2").render(thisYear=thisYear))
# Copy to prod
print("Copying to prod directory...")
shutil.copytree(OUTPUT_DIR, PROD_DIR, dirs_exist_ok=True)
print("Done.")
logger.info("Copying output to production directory")
shutil.copytree(settings.paths.output, settings.paths.prod, dirs_exist_ok=True)
logger.info("Done after {:.2f}s".format(time.time() - start_time))
if __name__ == "__main__":
asyncio.run(build())
build()