Files
aperisolve/aperisolve/analyzers/template_analyzer.py
T
2025-06-12 22:55:04 +02:00

75 lines
2.0 KiB
Python

"""Custom Analyzer for Image Submissions."""
import shutil
import subprocess
from pathlib import Path
from .utils import update_data
def analyze_myanalyzer(input_img: Path, output_dir: Path) -> None:
"""Analyze an image submission using myanalyzer."""
image_name = input_img.name
# Dir generated by the tool
extracted_dir = output_dir / "myanalyzer_output_dir"
try:
stderr, stdout = "", ""
# Run myanalyzer
data = subprocess.run(
["myanalyzer", "-e", "../" + str(image_name)],
cwd=output_dir,
capture_output=True,
text=True,
check=False,
)
stderr += data.stderr
stdout += data.stdout
if len(stderr) > 0: # Return error if error has been raised
err = {
"myanalyzer": {
"status": "error",
"error": stderr,
}
}
update_data(output_dir, err)
return None
zip_exist = False
if extracted_dir.exists(): # If result file(s) exist, zip them
subprocess.run(
["7z", "a", "../myanalyzer.7z", "*"],
cwd=extracted_dir,
capture_output=True,
text=True,
check=False,
)
zip_exist = True
if extracted_dir.exists(): # Remove the extracted directory
shutil.rmtree(extracted_dir)
output = data.stdout.split("\n") if data else []
output_data = {
"myanalyzer": {
"status": "ok",
"output": output,
}
}
if zip_exist:
output_data["myanalyzer"][
"download"
] = f"/download/{output_dir.name}/myanalyzer"
update_data(output_dir, output_data)
except Exception as e:
update_data(output_dir, {"myanalyzer": {"status": "error", "error": str(e)}})
return None