|
import os |
|
import subprocess |
|
import tempfile |
|
import shutil |
|
from flask import Flask, render_template, request, jsonify, send_from_directory |
|
from werkzeug.utils import secure_filename |
|
|
|
app = Flask(__name__) |
|
|
|
|
|
TEMP_DIR = tempfile.mkdtemp() |
|
|
|
@app.route('/') |
|
def index(): |
|
return render_template('index.html') |
|
|
|
@app.route('/run_command', methods=['POST']) |
|
def run_command(): |
|
user_command = request.form['command'] |
|
|
|
|
|
if user_command.startswith('git clone'): |
|
return handle_git_clone(user_command) |
|
|
|
|
|
try: |
|
|
|
result = subprocess.run(user_command, shell=True, text=True, capture_output=True, check=True, cwd=TEMP_DIR) |
|
output = result.stdout + "\n" + result.stderr |
|
except subprocess.CalledProcessError as e: |
|
output = e.output + "\n" + e.stderr |
|
|
|
return jsonify({'output': output}) |
|
|
|
@app.route('/upload', methods=['POST']) |
|
def upload_file(): |
|
file = request.files['file'] |
|
if file: |
|
filename = secure_filename(file.filename) |
|
filepath = os.path.join(TEMP_DIR, filename) |
|
file.save(filepath) |
|
return jsonify({"message": f"File {filename} uploaded successfully!"}) |
|
return jsonify({"error": "No file selected"}), 400 |
|
|
|
@app.route('/download/<filename>', methods=['GET']) |
|
def download_file(filename): |
|
return send_from_directory(TEMP_DIR, filename) |
|
|
|
@app.route('/cleanup', methods=['POST']) |
|
def cleanup(): |
|
shutil.rmtree(TEMP_DIR) |
|
os.makedirs(TEMP_DIR) |
|
return jsonify({'message': 'Temporary files cleaned up!'}) |
|
|
|
|
|
def handle_git_clone(command): |
|
try: |
|
|
|
parts = command.split(' ') |
|
if len(parts) != 3: |
|
return jsonify({'output': 'Invalid git clone command. Usage: git clone <repo_url>'}) |
|
|
|
repo_url = parts[2] |
|
repo_name = repo_url.split('/')[-1].replace('.git', '') |
|
clone_path = os.path.join(TEMP_DIR, repo_name) |
|
|
|
|
|
result = subprocess.run(['git', 'clone', repo_url, clone_path], text=True, capture_output=True, check=True) |
|
|
|
output = f"Repository {repo_name} cloned successfully!\n" |
|
output += result.stdout + "\n" + result.stderr |
|
|
|
return jsonify({'output': output}) |
|
except subprocess.CalledProcessError as e: |
|
return jsonify({'output': e.output + "\n" + e.stderr}) |
|
|
|
if __name__ == '__main__': |
|
app.run(host='0.0.0.0', port=7860) |
|
|