File size: 2,614 Bytes
c6858b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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__)

# Directory to store temporary files
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']

    # Handle Git Clone
    if user_command.startswith('git clone'):
        return handle_git_clone(user_command)

    # Handle other commands (cd, pip, etc.)
    try:
        # Run the command on the server
        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)  # Recreate temp directory
    return jsonify({'message': 'Temporary files cleaned up!'})

# Handle git clone operation
def handle_git_clone(command):
    try:
        # Extract the repository URL from the command
        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)

        # Clone the repository into the temporary folder
        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)