Artificial-superintelligence commited on
Commit
5bc4351
·
verified ·
1 Parent(s): a172f1d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -33
app.py CHANGED
@@ -1,14 +1,12 @@
1
  from flask import Flask, request, jsonify, render_template
2
- import io
3
- import sys
4
  import subprocess
5
  import tempfile
6
  import shutil
7
- import os
8
 
9
  app = Flask(__name__)
10
 
11
- # Create a temporary directory for user-installed packages
12
  temp_dir = tempfile.mkdtemp()
13
 
14
  @app.route("/")
@@ -17,46 +15,46 @@ def index():
17
 
18
  @app.route("/execute", methods=["POST"])
19
  def execute_code():
20
- code = request.json.get("code", "").strip()
21
- output = io.StringIO()
22
- sys.stdout = output
23
- sys.stderr = output
24
 
25
  try:
26
- if code.startswith("!pip install"):
27
- # Handle pip install commands with temporary directory
28
- package = code.split("!pip install", 1)[1].strip()
29
- if package:
30
- result = subprocess.run(
31
- [sys.executable, "-m", "pip", "install", package, "--target", temp_dir],
32
- stdout=subprocess.PIPE,
33
- stderr=subprocess.PIPE,
34
- text=True,
35
- )
36
- return jsonify({"result": result.stdout + result.stderr})
37
- else:
38
- return jsonify({"result": "Error: No package specified for installation."})
 
39
  else:
40
- # Adjust sys.path to include temporary package directory
41
- sys.path.insert(0, temp_dir)
42
- exec(code)
43
- result = output.getvalue()
 
 
 
 
 
44
  except Exception as e:
45
- result = f"Error: {e}"
46
- finally:
47
- # Reset stdout and stderr
48
- sys.stdout = sys.__stdout__
49
- sys.stderr = sys.__stderr__
50
 
51
- return jsonify({"result": result})
52
 
53
  @app.route("/cleanup", methods=["POST"])
54
  def cleanup():
55
- # Delete the temporary directory
56
  global temp_dir
57
  if os.path.exists(temp_dir):
58
  shutil.rmtree(temp_dir)
59
- temp_dir = tempfile.mkdtemp() # Recreate for the next session
60
  return jsonify({"result": "Temporary files cleaned up."})
61
 
62
  if __name__ == "__main__":
 
1
  from flask import Flask, request, jsonify, render_template
2
+ import os
 
3
  import subprocess
4
  import tempfile
5
  import shutil
 
6
 
7
  app = Flask(__name__)
8
 
9
+ # Create a temporary directory for operations
10
  temp_dir = tempfile.mkdtemp()
11
 
12
  @app.route("/")
 
15
 
16
  @app.route("/execute", methods=["POST"])
17
  def execute_code():
18
+ command = request.json.get("code", "").strip()
19
+ response = ""
 
 
20
 
21
  try:
22
+ # Ensure all operations happen within the temporary directory
23
+ os.chdir(temp_dir)
24
+
25
+ if command.startswith("!"):
26
+ # Handle shell commands (e.g., git clone, cd, pip install)
27
+ shell_command = command[1:]
28
+ process = subprocess.run(
29
+ shell_command,
30
+ shell=True,
31
+ stdout=subprocess.PIPE,
32
+ stderr=subprocess.PIPE,
33
+ text=True,
34
+ )
35
+ response = process.stdout + process.stderr
36
  else:
37
+ # Handle Python code execution
38
+ process = subprocess.run(
39
+ ["python3", "-c", command],
40
+ stdout=subprocess.PIPE,
41
+ stderr=subprocess.PIPE,
42
+ text=True,
43
+ )
44
+ response = process.stdout + process.stderr
45
+
46
  except Exception as e:
47
+ response = f"Error: {e}"
 
 
 
 
48
 
49
+ return jsonify({"result": response})
50
 
51
  @app.route("/cleanup", methods=["POST"])
52
  def cleanup():
53
+ # Clean up the temporary directory and recreate it
54
  global temp_dir
55
  if os.path.exists(temp_dir):
56
  shutil.rmtree(temp_dir)
57
+ temp_dir = tempfile.mkdtemp()
58
  return jsonify({"result": "Temporary files cleaned up."})
59
 
60
  if __name__ == "__main__":