-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.py
180 lines (158 loc) · 6.28 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import socket, os, os.path
from subprocess import Popen, PIPE
import Websheet
import json
import utils
# we already checked for this error in php land
try:
# a little convoluted to account for possibility this module's loaded from another place
config_jo = json.loads(open(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ws-config.json')).read())
db_enabled = "db-enabled" in config_jo and config_jo["db-enabled"] == True
except:
config_jo = []
db_enabled = False
# if you are using safeexec, securely running java should be something like this:
def java_prefix():
if (config_jo == []): return "error: ws-config.json doesn't exist"
jail = config_jo["java_jail-abspath"]
safeexec = config_jo["safeexec-executable-abspath"]
java = ["/java/bin/java", "-cp", ".:javax.json-1.0.jar", "-Xmx128M"] # java within jail, using default java_jail config
safeexec_args = ["--chroot_dir", jail, "--exec_dir", "/cp", "--env_vars", "",
"--nproc", "100", "--mem", "10000000", "--nfile", "100", "--gid", "1000", "--clock", "5", "--exec"]
return [safeexec] + safeexec_args + java
# at princeton, they use "sandbox" instead
if socket.gethostname().endswith("princeton.edu"):
def java_prefix():
cos126 = "/n/fs/htdocs/cos126/" # wapps directory
java = ["/usr/bin/java", "-cp", "java_jail/cp:java_jail/cp/javax.json-1.0.jar", "-Xmx512M"]
return ["sandbox", "-M", "-i", cos126+"java_jail/cp"] + java
# in either case "java_prefix" is like the 'java' binary,
# ready to accept the class name and cmd line args
def execute(command, the_stdin, input_encoding="UTF-8", output_encoding="UTF-8", flag_badchars = False):
proc = Popen(command, stdin=PIPE, stdout=PIPE, stderr=PIPE)
result = proc.communicate(input = the_stdin.encode(input_encoding))
result = Websheet.record(command = command,
pwd = os.getcwd(),
stdin = the_stdin,
stdout = result[0].decode(output_encoding),
stderr = result[1].decode(output_encoding),
returncode = proc.returncode)
if flag_badchars:
result.stdout = utils.expose_badchars(result.stdout)
return result
def run_java(command, the_stdin = ""):
return execute(java_prefix() + command, the_stdin)
tempdirs = []
# return new location for temp dir, relative to jail
# e.g. creates and returns "scratch/1237684/"
def create_tempdir():
import random
loc = "scratch/" + str(random.randint(100000000000, 999999999999))
os.mkdir(config_jo["java_jail-abspath"] + loc)
global tempdirs
tempdirs += [config_jo["java_jail-abspath"] + loc]
return loc + "/"
def uncreate_tempdirs():
import shutil
for d in tempdirs:
shutil.rmtree(d)
# database stuff
def connect():
if not db_enabled: return
import mysql.connector
return mysql.connector.connect(host=config_jo["db-host"],
user=config_jo["db-user"],
password=config_jo["db-password"],
db=config_jo["db-database"])
# don't run if not configured correctly,
# but run if configured correctly & not logged in
def save_submission(student, problem, user_state, result_column, passed):
if not db_enabled: return
db = connect()
cursor = db.cursor()
cursor.execute(
"insert into ws_history (user, problem, submission, result, passed, meta)" +
" VALUES (%s, %s, %s, %s, %s, %s)",
(student,
problem,
json.dumps(user_state),
json.dumps(result_column),
passed,
json.dumps(meta)))
db.commit()
cursor.close()
db.close()
# the rest don't run if not logged in
# returns a json list of code fragments, or False
def load_submission(student, problem, onlyPassed = False, maxId = None):
if student=="anonymous": return False
db = connect()
cursor = db.cursor()
passed_clause = " AND passed = 1 " if onlyPassed else ""
maxid_clause = " AND id <= " + str(maxId) if maxId is not None else ""
cursor.execute(
"select submission from ws_history " +
"WHERE user = %s AND problem = %s "
+ passed_clause + maxid_clause +
" ORDER BY ID DESC LIMIT 1;",
(student,
problem))
result = "false"
for row in cursor:
result = row[0]
cursor.close()
db.close()
return json.loads(result)
# returns a boolean
def ever_passed(student, problem):
if student=="anonymous": return False
db = connect()
cursor = db.cursor()
cursor.execute(
"select passed from ws_history WHERE user = %s AND problem = %s AND passed = 1 LIMIT 1;",
(student,
problem))
result = False
for row in cursor: # if any results, they've passed it
result = True
cursor.close()
db.close()
return result
# returns an integer
def num_submissions(student, problem):
if student=="anonymous": return 0
db = connect()
cursor = db.cursor()
cursor.execute(
"select count(1) from ws_history WHERE user = %s AND problem = %s;",
(student,
problem))
result = -1
for row in cursor: # if any results, they've passed it
result = row[0]
cursor.close()
db.close()
return result
def get_row(query, multiple=False, escapeme=[]):
db = connect()
cursor = db.cursor()
cursor.execute(query, escapeme)
result = [] if multiple else None
for row in cursor:
if multiple:
result.append([x for x in row])
else:
result = [x for x in row]
cursor.close()
db.close()
return result
def get_rows(query, escapeme=[]):
return get_row(query, True, escapeme)
# get instructor (to check permisions of student code review)
def get_instructor(student):
result = get_row(
"select value from ws_settings " +
"WHERE user = %s AND keyname = 'instructor';",
escapeme=(student, )
)
return result[0] if result else ""