Artificial-superintelligence commited on
Commit
9d64598
·
verified ·
1 Parent(s): 44d0c88

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -0
app.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify, send_file
2
+ import subprocess
3
+ import tempfile
4
+ import os
5
+ import shutil
6
+ import requests
7
+
8
+ app = Flask(__name__)
9
+
10
+ # Temporary directory to store downloaded files
11
+ TEMP_DIR = tempfile.mkdtemp()
12
+
13
+ @app.route('/')
14
+ def index():
15
+ return render_template('index.html')
16
+
17
+ @app.route('/execute', methods=['POST'])
18
+ def execute_command():
19
+ command = request.json['command']
20
+
21
+ if command.startswith('pip install'):
22
+ return execute_pip_install(command)
23
+ elif command.startswith('git clone'):
24
+ return execute_git_clone(command)
25
+ elif command.startswith('python'):
26
+ return execute_python_script(command)
27
+ elif command.startswith('download'):
28
+ return download_file(command)
29
+ elif command.startswith('!'):
30
+ return execute_shell_command(command[1:])
31
+ else:
32
+ return jsonify({'output': 'Unknown command. Try pip install, git clone, python, download, or !<shell command>.'})
33
+
34
+ def execute_pip_install(command):
35
+ try:
36
+ result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
37
+ return jsonify({'output': result.stdout})
38
+ except subprocess.CalledProcessError as e:
39
+ return jsonify({'output': f'Error: {e.stderr}'})
40
+
41
+ def execute_git_clone(command):
42
+ try:
43
+ result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True, cwd=TEMP_DIR)
44
+ return jsonify({'output': f'Repository cloned successfully to {TEMP_DIR}'})
45
+ except subprocess.CalledProcessError as e:
46
+ return jsonify({'output': f'Error: {e.stderr}'})
47
+
48
+ def execute_python_script(command):
49
+ try:
50
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, dir=TEMP_DIR) as temp_file:
51
+ temp_file.write(command[7:]) # Remove 'python ' from the beginning
52
+ temp_file_path = temp_file.name
53
+
54
+ result = subprocess.run(f'python {temp_file_path}', shell=True, check=True, capture_output=True, text=True)
55
+ os.unlink(temp_file_path)
56
+ return jsonify({'output': result.stdout})
57
+ except subprocess.CalledProcessError as e:
58
+ return jsonify({'output': f'Error: {e.stderr}'})
59
+
60
+ def download_file(command):
61
+ try:
62
+ _, url = command.split(maxsplit=1)
63
+ response = requests.get(url)
64
+ if response.status_code == 200:
65
+ filename = os.path.join(TEMP_DIR, url.split('/')[-1])
66
+ with open(filename, 'wb') as f:
67
+ f.write(response.content)
68
+ return jsonify({'output': f'File downloaded successfully: {filename}'})
69
+ else:
70
+ return jsonify({'output': f'Error downloading file: HTTP {response.status_code}'})
71
+ except Exception as e:
72
+ return jsonify({'output': f'Error downloading file: {str(e)}'})
73
+
74
+ def execute_shell_command(command):
75
+ try:
76
+ result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True, cwd=TEMP_DIR)
77
+ return jsonify({'output': result.stdout})
78
+ except subprocess.CalledProcessError as e:
79
+ return jsonify({'output': f'Error: {e.stderr}'})
80
+
81
+ @app.teardown_appcontext
82
+ def cleanup(error):
83
+ shutil.rmtree(TEMP_DIR, ignore_errors=True)
84
+
85
+ if __name__ == '__main__':
86
+ app.run(debug=True)