vsj0702 commited on
Commit
68625cd
·
verified ·
1 Parent(s): 66d64a1

Adding support for java, c# and js

Browse files
Files changed (1) hide show
  1. utils.py +29 -30
utils.py CHANGED
@@ -4,6 +4,7 @@ import subprocess
4
  from io import StringIO
5
  import contextlib
6
  import tempfile
 
7
  from datetime import datetime
8
  from typing import Tuple
9
 
@@ -18,6 +19,31 @@ def capture_output():
18
  finally:
19
  sys.stdout, sys.stderr = old_out, old_err
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def execute_code(code: str, stdin: str = "", language: str = "cpp") -> Tuple[str, str, str]:
22
  """
23
  Execute code in the specified language and return (stdout, stderr, exception)
@@ -103,40 +129,13 @@ def _execute_cpp(code: str, stdin: str):
103
 
104
 
105
  def _execute_java(code: str, stdin: str):
106
- with tempfile.TemporaryDirectory() as tmp:
107
- class_name = "Main"
108
- source_path = os.path.join(tmp, f"{class_name}.java")
109
- with open(source_path, "w") as f:
110
- f.write(code)
111
- compile_cmd = ["javac", source_path]
112
- compile_out, compile_err, exc = _run_subprocess(compile_cmd)
113
- if exc or compile_err:
114
- return compile_out, compile_err, exc
115
- return _run_subprocess(["java", "-cp", tmp, class_name], stdin)
116
-
117
 
118
  def _execute_javascript(code: str, stdin: str):
119
- with tempfile.NamedTemporaryFile(suffix=".js", delete=False, mode="w") as f:
120
- f.write(code)
121
- temp_path = f.name
122
- try:
123
- return _run_subprocess(["node", temp_path], stdin)
124
- finally:
125
- os.remove(temp_path)
126
-
127
 
128
  def _execute_csharp(code: str, stdin: str):
129
- with tempfile.TemporaryDirectory() as tmp:
130
- source_path = os.path.join(tmp, "main.cs")
131
- binary_path = os.path.join(tmp, "main.exe")
132
- with open(source_path, "w") as f:
133
- f.write(code)
134
- compile_cmd = ["mcs", source_path, "-out:" + binary_path]
135
- compile_out, compile_err, exc = _run_subprocess(compile_cmd)
136
- if exc or compile_err:
137
- return compile_out, compile_err, exc
138
- return _run_subprocess(["mono", binary_path], stdin)
139
-
140
 
141
  def export_session(code: str, output: str, error: str) -> dict:
142
  return {
 
4
  from io import StringIO
5
  import contextlib
6
  import tempfile
7
+ import requests
8
  from datetime import datetime
9
  from typing import Tuple
10
 
 
19
  finally:
20
  sys.stdout, sys.stderr = old_out, old_err
21
 
22
+ def _execute_with_jdoodle(code: str, stdin: str, language: str, version_index="0"):
23
+ url = "https://api.jdoodle.com/v1/execute"
24
+ payload = {
25
+ "clientId": os.environ["JDOODLE_CLIENT_ID"],
26
+ "clientSecret": os.environ["JDOODLE_CLIENT_SECRET"],
27
+ "script": code,
28
+ "stdin": stdin,
29
+ "language": language.lower(),
30
+ "versionIndex": version_index
31
+ }
32
+
33
+ try:
34
+ response = requests.post(url, json=payload, timeout=10)
35
+ data = response.json()
36
+ output = data.get("output", "").strip()
37
+ error = data.get("error", "")
38
+ statusCode = data.get("statusCode")
39
+
40
+ if statusCode != 200:
41
+ return "", error or output, f"JDoodle Error: {statusCode}"
42
+ return output, "", None
43
+ except Exception as e:
44
+ return "", "", str(e)
45
+
46
+
47
  def execute_code(code: str, stdin: str = "", language: str = "cpp") -> Tuple[str, str, str]:
48
  """
49
  Execute code in the specified language and return (stdout, stderr, exception)
 
129
 
130
 
131
  def _execute_java(code: str, stdin: str):
132
+ return _execute_with_jdoodle(code, stdin, language="java", version_index="4")
 
 
 
 
 
 
 
 
 
 
133
 
134
  def _execute_javascript(code: str, stdin: str):
135
+ return _execute_with_jdoodle(code, stdin, language="nodejs", version_index="4")
 
 
 
 
 
 
 
136
 
137
  def _execute_csharp(code: str, stdin: str):
138
+ return _execute_with_jdoodle(code, stdin, language="csharp", version_index="4")
 
 
 
 
 
 
 
 
 
 
139
 
140
  def export_session(code: str, output: str, error: str) -> dict:
141
  return {