支持多语言
parent
ce8aff93f0
commit
91766b89c4
@ -1,4 +0,0 @@
|
|||||||
TERM=xterm
|
|
||||||
VIRTUAL_ENV=/python_env
|
|
||||||
VIRTUAL_ENV_PROMPT=(python_env)
|
|
||||||
PATH=/python_env/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
|
||||||
@ -0,0 +1,6 @@
|
|||||||
|
TERM=xterm
|
||||||
|
HOME=/home/rocker
|
||||||
|
VIRTUAL_ENV=/home/rocker/pypy_env
|
||||||
|
VIRTUAL_ENV_PROMPT=(pypy_env)
|
||||||
|
|
||||||
|
PATH=/home/rocker/go/go/bin:/home/rocker/pypy_env/bin:/home/rocker/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
|
||||||
@ -1,23 +1,60 @@
|
|||||||
|
|
||||||
code = """
|
python_code = """
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
import http.client
|
import http.client
|
||||||
|
import random
|
||||||
|
import ctypes
|
||||||
|
size_in_bytes = 100 * 1024 * 1024
|
||||||
|
fixed_size_memory = (ctypes.c_ubyte * size_in_bytes)()
|
||||||
|
for i in range(100 * 1024):
|
||||||
|
fixed_size_memory[i * 1024] = 0
|
||||||
|
|
||||||
print("hello world")
|
print("hello world")
|
||||||
print(os.environ)
|
print(os.environ)
|
||||||
print(os.listdir())
|
print(os.listdir())
|
||||||
|
memory_list = []
|
||||||
|
# for _ in range(100_0000):
|
||||||
|
# memory_list.append(random.randint(0, 1000_0000))
|
||||||
|
exit()
|
||||||
|
|
||||||
conn = http.client.HTTPConnection("www.baidu.com")
|
conn = http.client.HTTPConnection("www.baidu.com")
|
||||||
conn.request("GET", "/")
|
conn.request("GET", "/")
|
||||||
print(conn.getresponse().read().decode('utf-8'))
|
print(conn.getresponse().read().decode('utf-8'))
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
cpp_code = """
|
||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
cout << "hello world~" << endl;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
go_code = """
|
||||||
|
package main
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
func main(){
|
||||||
|
fmt.Println1("hello world~")
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
rust_code = """
|
||||||
|
fn main(){
|
||||||
|
println!("hello world~");
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import requests
|
import requests
|
||||||
url = "http://code.quant.cm"
|
url = "http://127.0.0.1:8011"
|
||||||
resp = requests.post(url, json={"code": code})
|
# resp = requests.post(url, json={"code": python_code, "language": "python"})
|
||||||
|
# resp = requests.post(url, json={"code": cpp_code, "language": "cpp"})
|
||||||
|
# resp = requests.post(url, json={"code": go_code, "language": "go"})
|
||||||
|
resp = requests.post(url, json={"code": rust_code, "language": "rust"})
|
||||||
print(resp.json()["msg"])
|
print(resp.json()["msg"])
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,92 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import subprocess
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
TARGET_LANGUAGE = set(["python", "cpp", "go", "node", "rust"])
|
||||||
|
PROJECT_PATH = "/home/rocker/project"
|
||||||
|
|
||||||
|
def go_handler():
|
||||||
|
source_path = f"{PROJECT_PATH}/go_project/main.go"
|
||||||
|
subprocess.run(["cp", "/tmp/code", source_path], capture_output=True)
|
||||||
|
main_path = f"{PROJECT_PATH}/go_project/main"
|
||||||
|
ret = subprocess.run(["go", "build", "-o", main_path], cwd=f"{PROJECT_PATH}/go_project", capture_output=True)
|
||||||
|
std_err = ret.stderr.decode("utf-8")
|
||||||
|
if len(std_err) > 0:
|
||||||
|
return std_err, ""
|
||||||
|
return "", main_path
|
||||||
|
|
||||||
|
def cpp_handler():
|
||||||
|
source_path = f"{PROJECT_PATH}/cpp_project/main.cpp"
|
||||||
|
subprocess.run(["cp", "/tmp/code", source_path], capture_output=True)
|
||||||
|
main_path = f"{PROJECT_PATH}/cpp_project/main"
|
||||||
|
ret = subprocess.run(["g++", source_path, "-o", main_path], capture_output=True)
|
||||||
|
std_err = ret.stderr.decode("utf-8")
|
||||||
|
if len(std_err) > 0:
|
||||||
|
return std_err, ""
|
||||||
|
return "", main_path
|
||||||
|
|
||||||
|
def node_handler():
|
||||||
|
pass
|
||||||
|
|
||||||
|
def rust_handler():
|
||||||
|
source_path = f"{PROJECT_PATH}/rust_project/src/main.rs"
|
||||||
|
subprocess.run(["cp", "/tmp/code", source_path], capture_output=True)
|
||||||
|
ret = subprocess.run(["cargo", "build"], cwd=f"{PROJECT_PATH}/rust_project", capture_output=True)
|
||||||
|
std_err = ret.stderr.decode("utf-8")
|
||||||
|
if len(std_err) > 0 and ret.returncode != 0:
|
||||||
|
return std_err, ""
|
||||||
|
main_path = f"{PROJECT_PATH}/rust_project/target/debug/main"
|
||||||
|
subprocess.run(["mv", f"{PROJECT_PATH}/rust_project/target/debug/rust_project", main_path], capture_output=True)
|
||||||
|
return "", main_path
|
||||||
|
|
||||||
|
def python_handler():
|
||||||
|
main_path = f"{PROJECT_PATH}/python_project/main.py"
|
||||||
|
subprocess.run(["cp", "/tmp/code", main_path], capture_output=True)
|
||||||
|
return "", main_path
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
language = sys.argv[1]
|
||||||
|
if language not in TARGET_LANGUAGE:
|
||||||
|
print(f"不支持的编程语言: {language}")
|
||||||
|
return
|
||||||
|
|
||||||
|
# 编译和执行
|
||||||
|
cmd = ["/usr/bin/time", "-f", "'%M %U %S %e'"]
|
||||||
|
err = ""
|
||||||
|
if language == "python":
|
||||||
|
err, main_path = python_handler()
|
||||||
|
cmd.extend(["python", "-u", main_path])
|
||||||
|
elif language == "cpp":
|
||||||
|
err, main_path = cpp_handler()
|
||||||
|
cmd.extend([main_path])
|
||||||
|
elif language == "go":
|
||||||
|
err, main_path = go_handler()
|
||||||
|
cmd.extend([main_path])
|
||||||
|
elif language == "node":
|
||||||
|
pass
|
||||||
|
elif language == "rust":
|
||||||
|
err, main_path = rust_handler()
|
||||||
|
cmd.extend([main_path])
|
||||||
|
else:
|
||||||
|
...
|
||||||
|
|
||||||
|
# 阻塞执行
|
||||||
|
if err != "":
|
||||||
|
print(err)
|
||||||
|
else:
|
||||||
|
ret = subprocess.run(cmd, capture_output=True)
|
||||||
|
out_lines = f"{ret.stdout[:1_0000].decode('utf-8')} \n {ret.stderr[:1_0000].decode('utf-8')}".rstrip().split("\n")
|
||||||
|
try:
|
||||||
|
end_line_lis = out_lines[-1].replace("'", "").strip().split(" ")
|
||||||
|
end_line = f"MaxRSS: {int(end_line_lis[0])//1024}mb, User: {end_line_lis[1]}s, Sys: {end_line_lis[2]}s, Elapsed: {end_line_lis[3]}s"
|
||||||
|
out_lines[-1] = end_line
|
||||||
|
except Exception as e:
|
||||||
|
print(traceback.format_exc())
|
||||||
|
out = "\n".join(out_lines)
|
||||||
|
print(out)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in New Issue