fix: print debug in real time

This commit is contained in:
administrateur 2025-03-30 06:59:52 +02:00
parent 1948433764
commit 80796c20a0

View File

@ -1,7 +1,6 @@
from flask import Flask, request, jsonify
import os
import subprocess
import base64
import redis
from threading import Lock
@ -36,7 +35,6 @@ def obscure_password(password):
return result.stdout.strip()
def write_rclone_config():
"""Write secure rclone config file with obscured password"""
config_dir = "/tmp/rclone"
os.makedirs(config_dir, exist_ok=True)
config_path = os.path.join(config_dir, "rclone.conf")
@ -71,6 +69,14 @@ def mount_nextcloud(website, mount_path="/mnt"):
if result.returncode != 0:
raise RuntimeError(f"Failed to mount: {result.stderr}")
def stream_command(cmd):
log_debug(f"Executing: {cmd}")
process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
for line in process.stdout:
print(line.rstrip(), flush=True)
process.wait()
return process.returncode
@app.route("/build", methods=["POST"])
def build_mkdocs():
log_debug("POST request received at /build")
@ -108,18 +114,14 @@ def build_mkdocs():
return jsonify({"error": f"{src} not found after mount"}), 404
log_debug(f"Running MkDocs build: {src} -> {compile_path}")
cmd = f"mkdocs build --quiet --no-strict --config-file {src} --site-dir {compile_path}"
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
if result.returncode != 0:
build_error = base64.b64encode(result.stderr.encode()).decode()
json_payload = {"site": website, "error": build_error}
log_debug(f"MkDocs build failed: {result.stderr}")
# requests.post(error_callback, json=json_payload, headers={"Content-Type": "application/json"})
return jsonify({"status": "error", "message": "Build failed", "error": result.stderr}), 500
mkdocs_cmd = f"mkdocs build --no-strict --config-file {src} --site-dir {compile_path}"
if stream_command(mkdocs_cmd) != 0:
return jsonify({"status": "error", "message": "MkDocs build failed"}), 500
log_debug(f"Performing differential copy from {compile_path} to {final_path}")
subprocess.run(f"rsync -a --delete {compile_path}/ {final_path}/", shell=True, check=True)
rsync_cmd = f"rsync -a --delete {compile_path}/ {final_path}/"
if stream_command(rsync_cmd) != 0:
return jsonify({"status": "error", "message": "Rsync failed"}), 500
log_debug(f"MkDocs build and sync successful for website: {website}")
return jsonify({"status": "success", "message": "Build successful"}), 200