mirror of
https://github.com/blw1138/Zordon.git
synced 2026-06-09 13:39:24 -05:00
Windows path fixes (#129)
* Change uses of os.path to use Pathlib * Add return types and type hints * Add more docstrings * Add missing import to api_server
This commit is contained in:
+54
-46
@@ -2,14 +2,15 @@
|
||||
import concurrent.futures
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
import shutil
|
||||
import socket
|
||||
import ssl
|
||||
import tempfile
|
||||
import time
|
||||
import traceback
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Dict, Any, Optional
|
||||
|
||||
import cpuinfo
|
||||
import psutil
|
||||
@@ -23,7 +24,7 @@ from src.distributed_job_manager import DistributedJobManager
|
||||
from src.engines.engine_manager import EngineManager
|
||||
from src.render_queue import RenderQueue, JobNotFoundError
|
||||
from src.utilities.config import Config
|
||||
from src.utilities.misc_helper import system_safe_path, current_system_os, current_system_cpu, \
|
||||
from src.utilities.misc_helper import current_system_os, current_system_cpu, \
|
||||
current_system_os_version, num_to_alphanumeric, get_gpu_info
|
||||
from src.utilities.status_utils import string_to_status
|
||||
from src.version import APP_VERSION
|
||||
@@ -34,7 +35,7 @@ ssl._create_default_https_context = ssl._create_unverified_context # disable SS
|
||||
|
||||
API_VERSION = "0.1"
|
||||
|
||||
def start_api_server(hostname=None):
|
||||
def start_api_server(hostname: Optional[str] = None) -> None:
|
||||
|
||||
# get hostname
|
||||
if not hostname:
|
||||
@@ -44,7 +45,7 @@ def start_api_server(hostname=None):
|
||||
# load flask settings
|
||||
server.config['HOSTNAME'] = hostname
|
||||
server.config['PORT'] = int(Config.port_number)
|
||||
server.config['UPLOAD_FOLDER'] = system_safe_path(os.path.expanduser(Config.upload_folder))
|
||||
server.config['UPLOAD_FOLDER'] = str(Path(Config.upload_folder).expanduser())
|
||||
server.config['MAX_CONTENT_PATH'] = Config.max_content_path
|
||||
server.config['enable_split_jobs'] = Config.enable_split_jobs
|
||||
|
||||
@@ -65,7 +66,7 @@ def start_api_server(hostname=None):
|
||||
# --------------------------------------------
|
||||
|
||||
@server.get('/api/jobs')
|
||||
def jobs_json():
|
||||
def jobs_json() -> Dict[str, Any]:
|
||||
"""Retrieves all jobs from the render queue in JSON format.
|
||||
|
||||
This endpoint fetches all jobs currently in the render queue, converts them to JSON format,
|
||||
@@ -134,9 +135,9 @@ def get_job_logs(job_id):
|
||||
Response: The log file's content as plain text, or an empty response if the log file is not found.
|
||||
"""
|
||||
found_job = RenderQueue.job_with_id(job_id)
|
||||
log_path = system_safe_path(found_job.log_path())
|
||||
log_path = Path(found_job.log_path())
|
||||
log_data = None
|
||||
if log_path and os.path.exists(log_path):
|
||||
if log_path and log_path.exists():
|
||||
with open(log_path) as file:
|
||||
log_data = file.read()
|
||||
return Response(log_data, mimetype='text/plain')
|
||||
@@ -144,20 +145,21 @@ def get_job_logs(job_id):
|
||||
|
||||
@server.get('/api/job/<job_id>/file_list')
|
||||
def get_file_list(job_id):
|
||||
return [os.path.basename(x) for x in RenderQueue.job_with_id(job_id).file_list()]
|
||||
return [Path(p).name for p in RenderQueue.job_with_id(job_id).file_list()]
|
||||
|
||||
|
||||
@server.route('/api/job/<job_id>/download')
|
||||
def download_requested_file(job_id):
|
||||
|
||||
requested_filename = request.args.get('filename')
|
||||
requested_filename = request.args.get("filename")
|
||||
if not requested_filename:
|
||||
return 'Filename required', 400
|
||||
return "Filename required", 400
|
||||
|
||||
found_job = RenderQueue.job_with_id(job_id)
|
||||
for job_filename in found_job.file_list():
|
||||
if os.path.basename(job_filename).lower() == requested_filename.lower():
|
||||
return send_file(job_filename, as_attachment=True, )
|
||||
|
||||
for job_file in found_job.file_list():
|
||||
p = Path(job_file)
|
||||
if p.name.lower() == requested_filename.lower():
|
||||
return send_file(str(p), as_attachment=True)
|
||||
|
||||
return f"File '{requested_filename}' not found", 404
|
||||
|
||||
@@ -168,26 +170,27 @@ def download_all_files(job_id):
|
||||
|
||||
@after_this_request
|
||||
def clear_zip(response):
|
||||
if zip_filename and os.path.exists(zip_filename):
|
||||
if zip_filename and zip_filename.exists():
|
||||
try:
|
||||
os.remove(zip_filename)
|
||||
zip_filename.unlink()
|
||||
except Exception as e:
|
||||
logger.warning(f"Error removing zip file '{zip_filename}': {e}")
|
||||
return response
|
||||
|
||||
found_job = RenderQueue.job_with_id(job_id)
|
||||
output_dir = os.path.dirname(found_job.output_path)
|
||||
if os.path.exists(output_dir):
|
||||
from zipfile import ZipFile
|
||||
zip_filename = system_safe_path(os.path.join(tempfile.gettempdir(),
|
||||
pathlib.Path(found_job.input_path).stem + '.zip'))
|
||||
with ZipFile(zip_filename, 'w') as zipObj:
|
||||
for f in os.listdir(output_dir):
|
||||
zipObj.write(filename=system_safe_path(os.path.join(output_dir, f)),
|
||||
arcname=os.path.basename(f))
|
||||
return send_file(zip_filename, mimetype="zip", as_attachment=True, )
|
||||
else:
|
||||
return f'Cannot find project files for job {job_id}', 500
|
||||
|
||||
output_dir = Path(found_job.output_path).parent
|
||||
if not output_dir.exists():
|
||||
return f"Cannot find project files for job {job_id}", 500
|
||||
|
||||
zip_filename = Path(tempfile.gettempdir()) / f"{Path(found_job.input_path).stem}.zip"
|
||||
from zipfile import ZipFile
|
||||
with ZipFile(zip_filename, "w") as zipObj:
|
||||
for f in output_dir.iterdir():
|
||||
if f.is_file():
|
||||
zipObj.write(f, arcname=f.name)
|
||||
|
||||
return send_file(str(zip_filename), mimetype="zip", as_attachment=True)
|
||||
|
||||
|
||||
# --------------------------------------------
|
||||
@@ -195,8 +198,8 @@ def download_all_files(job_id):
|
||||
# --------------------------------------------
|
||||
|
||||
@server.get('/api/presets')
|
||||
def presets():
|
||||
presets_path = system_safe_path('config/presets.yaml')
|
||||
def presets() -> Dict[str, Any]:
|
||||
presets_path = Path('config/presets.yaml')
|
||||
with open(presets_path) as f:
|
||||
loaded_presets = yaml.load(f, Loader=yaml.FullLoader)
|
||||
return loaded_presets
|
||||
@@ -292,6 +295,7 @@ def add_job_handler():
|
||||
err_msg = f"Error processing job data: {e}"
|
||||
return err_msg, 400
|
||||
except Exception as e:
|
||||
traceback.print_exception(e)
|
||||
err_msg = f"Unknown error processing data: {e}"
|
||||
return err_msg, 500
|
||||
|
||||
@@ -311,9 +315,9 @@ def add_job_handler():
|
||||
|
||||
# Save notes to .txt
|
||||
if processed_job_data.get("notes"):
|
||||
parent_dir = os.path.dirname(os.path.dirname(loaded_project_local_path))
|
||||
parent_dir = Path(loaded_project_local_path).parent.parent
|
||||
notes_name = processed_job_data['name'] + "-notes.txt"
|
||||
with open(os.path.join(parent_dir, notes_name), "w") as f:
|
||||
with (Path(parent_dir) / notes_name).open("w") as f:
|
||||
f.write(processed_job_data["notes"])
|
||||
return [x.json() for x in created_jobs]
|
||||
except Exception as e:
|
||||
@@ -338,13 +342,18 @@ def cancel_job(job_id):
|
||||
@server.route('/api/job/<job_id>/delete', methods=['POST', 'GET'])
|
||||
def delete_job(job_id):
|
||||
try:
|
||||
if not request.args.get('confirm', False):
|
||||
return 'Confirmation required to delete job', 400
|
||||
if not request.args.get("confirm", False):
|
||||
return "Confirmation required to delete job", 400
|
||||
|
||||
# Check if we can remove the 'output' directory
|
||||
found_job = RenderQueue.job_with_id(job_id)
|
||||
project_dir = os.path.dirname(os.path.dirname(found_job.input_path))
|
||||
output_dir = os.path.dirname(found_job.output_path)
|
||||
|
||||
input_path = Path(found_job.input_path)
|
||||
output_path = Path(found_job.output_path)
|
||||
upload_root = Path(server.config["UPLOAD_FOLDER"])
|
||||
|
||||
project_dir = input_path.parent.parent
|
||||
output_dir = output_path.parent
|
||||
|
||||
found_job.stop()
|
||||
|
||||
try:
|
||||
@@ -352,25 +361,24 @@ def delete_job(job_id):
|
||||
except Exception as e:
|
||||
logger.error(f"Error deleting previews for {found_job}: {e}")
|
||||
|
||||
# finally delete the job
|
||||
RenderQueue.delete_job(found_job)
|
||||
|
||||
# delete the output_dir
|
||||
if server.config['UPLOAD_FOLDER'] in output_dir and os.path.exists(output_dir):
|
||||
# Delete output directory if we own it
|
||||
if output_dir.exists() and output_dir.is_relative_to(upload_root):
|
||||
shutil.rmtree(output_dir)
|
||||
|
||||
# See if we own the project_dir (i.e. was it uploaded) - if so delete the directory
|
||||
# Delete project directory if we own it and it's unused
|
||||
try:
|
||||
if server.config['UPLOAD_FOLDER'] in project_dir and os.path.exists(project_dir):
|
||||
# check to see if any other projects are sharing the same project file
|
||||
project_dir_files = [f for f in os.listdir(project_dir) if not f.startswith('.')]
|
||||
if len(project_dir_files) == 0 or (len(project_dir_files) == 1 and 'source' in project_dir_files[0]):
|
||||
if project_dir.exists() and project_dir.is_relative_to(upload_root):
|
||||
project_dir_files = [p for p in project_dir.iterdir() if not p.name.startswith(".")]
|
||||
if not project_dir_files or (len(project_dir_files) == 1 and "source" in project_dir_files[0].name):
|
||||
logger.info(f"Removing project directory: {project_dir}")
|
||||
shutil.rmtree(project_dir)
|
||||
except Exception as e:
|
||||
logger.error(f"Error removing project files: {e}")
|
||||
|
||||
return "Job deleted", 200
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Error deleting job: {e}")
|
||||
return f"Error deleting job: {e}", 500
|
||||
|
||||
Reference in New Issue
Block a user