Artificial-superintelligence commited on
Commit
c6858b8
·
verified ·
1 Parent(s): d0ecb70

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
+ import tempfile
4
+ import shutil
5
+ from flask import Flask, render_template, request, jsonify, send_from_directory
6
+ from werkzeug.utils import secure_filename
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Directory to store temporary files
11
+ TEMP_DIR = tempfile.mkdtemp()
12
+
13
+ @app.route('/')
14
+ def index():
15
+ return render_template('index.html')
16
+
17
+ @app.route('/run_command', methods=['POST'])
18
+ def run_command():
19
+ user_command = request.form['command']
20
+
21
+ # Handle Git Clone
22
+ if user_command.startswith('git clone'):
23
+ return handle_git_clone(user_command)
24
+
25
+ # Handle other commands (cd, pip, etc.)
26
+ try:
27
+ # Run the command on the server
28
+ result = subprocess.run(user_command, shell=True, text=True, capture_output=True, check=True, cwd=TEMP_DIR)
29
+ output = result.stdout + "\n" + result.stderr
30
+ except subprocess.CalledProcessError as e:
31
+ output = e.output + "\n" + e.stderr
32
+
33
+ return jsonify({'output': output})
34
+
35
+ @app.route('/upload', methods=['POST'])
36
+ def upload_file():
37
+ file = request.files['file']
38
+ if file:
39
+ filename = secure_filename(file.filename)
40
+ filepath = os.path.join(TEMP_DIR, filename)
41
+ file.save(filepath)
42
+ return jsonify({"message": f"File {filename} uploaded successfully!"})
43
+ return jsonify({"error": "No file selected"}), 400
44
+
45
+ @app.route('/download/<filename>', methods=['GET'])
46
+ def download_file(filename):
47
+ return send_from_directory(TEMP_DIR, filename)
48
+
49
+ @app.route('/cleanup', methods=['POST'])
50
+ def cleanup():
51
+ shutil.rmtree(TEMP_DIR)
52
+ os.makedirs(TEMP_DIR) # Recreate temp directory
53
+ return jsonify({'message': 'Temporary files cleaned up!'})
54
+
55
+ # Handle git clone operation
56
+ def handle_git_clone(command):
57
+ try:
58
+ # Extract the repository URL from the command
59
+ parts = command.split(' ')
60
+ if len(parts) != 3:
61
+ return jsonify({'output': 'Invalid git clone command. Usage: git clone <repo_url>'})
62
+
63
+ repo_url = parts[2]
64
+ repo_name = repo_url.split('/')[-1].replace('.git', '')
65
+ clone_path = os.path.join(TEMP_DIR, repo_name)
66
+
67
+ # Clone the repository into the temporary folder
68
+ result = subprocess.run(['git', 'clone', repo_url, clone_path], text=True, capture_output=True, check=True)
69
+
70
+ output = f"Repository {repo_name} cloned successfully!\n"
71
+ output += result.stdout + "\n" + result.stderr
72
+
73
+ return jsonify({'output': output})
74
+ except subprocess.CalledProcessError as e:
75
+ return jsonify({'output': e.output + "\n" + e.stderr})
76
+
77
+ if __name__ == '__main__':
78
+ app.run(host='0.0.0.0', port=7860)