Spaces:
Sleeping
Sleeping
from flask import Flask, request, send_from_directory, render_template_string | |
import os | |
import subprocess | |
app = Flask(__name__) | |
# Directory to save uploaded NSI files and generated EXEs | |
UPLOAD_FOLDER = 'uploads' | |
COMPILE_FOLDER = 'compiled' | |
os.makedirs(UPLOAD_FOLDER, exist_ok=True) | |
os.makedirs(COMPILE_FOLDER, exist_ok=True) | |
# HTML template for the interface | |
html_template = """ | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>NSIS Compiler</title> | |
</head> | |
<body> | |
<h1>Upload your .nsi file to compile</h1> | |
<form action="/upload" method="post" enctype="multipart/form-data"> | |
<input type="file" name="nsi_file" accept=".nsi" required> | |
<button type="submit">Upload and Compile</button> | |
</form> | |
</body> | |
</html> | |
""" | |
def index(): | |
return render_template_string(html_template) | |
def upload_nsi(): | |
if 'nsi_file' not in request.files: | |
return "No file part" | |
file = request.files['nsi_file'] | |
if file.filename == '': | |
return "No selected file" | |
# Save the NSI file | |
nsi_path = os.path.join(UPLOAD_FOLDER, file.filename) | |
file.save(nsi_path) | |
# Compile the NSI file using makensis | |
exe_name = file.filename.replace('.nsi', '.exe') | |
exe_path = os.path.join(COMPILE_FOLDER, exe_name) | |
# Command to run NSIS compiler | |
compile_command = f'makensis {nsi_path}' | |
result = subprocess.run(compile_command, shell=True, capture_output=True) | |
# Log output | |
output = result.stdout.decode() + '\n' + result.stderr.decode() | |
if result.returncode == 0: | |
return f'Compilation successful! Download <a href="/download/{exe_name}">here</a><br><pre>{output}</pre>' | |
else: | |
return f'Compilation failed: <pre>{output}</pre>' | |
def download_file(filename): | |
return send_from_directory(COMPILE_FOLDER, filename) | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port=7860, debug=True) | |