-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpc.rb
executable file
·306 lines (258 loc) · 8.66 KB
/
cpc.rb
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env ruby
#change to #!/home/pfaiman/ruby/bin/ruby to prevent tampering with runtime
require 'getoptlong'
#require 'sqlite3'
require 'date'
#require './src/db.rb'
require '/home/csc-cplug/cpc/config.rb'
if __FILE__ != $0
puts 'Do not include or require this file.'
exit!
end
$commands_summary =
"The following commands are available:\n"\
"help View help text on a command\n"\
"submit Add a new submission\n"\
"problems List problems\n"\
"grade Grade submissions (internal only)"
# "submissions View your graded submissions\n"\
# "scoreboard View the scoreboard\n"\
# "contests List running contests\n"\
$commands = {
# :submissions =>
# "Usage: cpc submissions [-c contest] [-u user]\n"\
# "Lists all graded submissions made by the specified user, or the current user if none is specified.\n"\
# "The -c flag filters submissions to include only those made in the specified contest.",
# :scoreboard =>
# "Usage: cpc scoreboard [-c contest] [-a]\n"\
# "Gives the scoreboard for the specified contest, or the default contest if none is specified.\n"\
# "The -a flag will cause this command to factor in results from submissions made after the contest has ended.\n",
:grade =>
"Usage: cpc grade\n"\
"Runs the grader on all submissions in the queue.\n"\
"For internal use only.",
:submit =>
"Usage: cpc submit <problem> <file...>\n"\
"Submits files to the specified problem.\n",
# :contests =>
# "Usage: cpc contests [-a]\n"\
# "By default, lists all active contests. The default contest will be followed by an asterisk (*).\n"\
# "The default contest is the contest that has last started, and has not yet finished.\n"\
# "The -a flag will cause this command to display all contests, including those that have already ended and those that haven't started yet.\n",
:problems =>
"Usage: cpc problems <problem>\n"\
"Lists all problems for the default contest.\n"\
"Or, if a problem name is given, print the text of that problem.\n",
:help =>
"Usage: cpc help [command]\n" + $commands_summary
}
def contest_dir(contest=default_contest)
File.join($problem_loc, contest)
end
def problem_dir(problem)
File.join($problem_loc, $default_contest, problem)
end
def problem_list(contest=$default_contest)
contests_paths = File.join(contest_dir(contest), "*", "/")
Dir[contests_paths].map do |contest_abs_path|
File.basename(contest_abs_path)
end
end
def submission_dir(problem, user="")
File.join(problem_dir(problem), "submissions", "queued", user)
end
def submit(user, args)
problem = args.shift
if !problem
puts 'Must specify problem.'
return false
end
problem = File.basename(problem)
problem_dir = problem_dir(problem)
if !File.directory?(problem_dir)
puts "Invalid problem: #{problem}"
false
elsif args.length == 0
puts 'No files specified'
false
else
args.each do |file|
# How paranoid do we want to be for now...
puts "Submitting #{file}... "
system("cat #{file} | #{$fancycat_loc} #{$default_contest} #{problem} #{file}")
end
true
end
end
def error_string(error_id)
return 'ID10T_ERROR'
end
def submissions(user, args)
contest = $default_contest
sql = 'SELECT problem.name, time, status, errorId, score
FROM submission
JOIN problem ON problem.id = submission.problem
JOIN contest ON contest.id = problem.contest
WHERE user = ? AND contest.alias = ?'
opts = GetoptLong.new(
['--user', '-u', GetoptLong::REQUIRED_ARGUMENT],
['--contest', '-c', GetoptLong::REQUIRED_ARGUMENT]
)
opts.each do |opt, arg|
case opt
when '--user'
user = arg
when '--contest'
contest = arg
end
end
db = SQLite3::Database.new 'cpc.db'
db.execute(sql, user, contest) do |row|
printf("%-15s %-50s %s %s", row[0], DateTime.strptime(row[1],"%s").strftime("%H:%M %Y/%m/%d"), status, status == "SUCCESS" ? row[2] : error_string(row[3]));
end
true
end
def scoreboard(user, args)
false
end
def compile(file_path)
dirname = File.dirname(file_path)
filename = File.basename(file_path)
fileext = File.extname(file_path)
command = {
".c" => "gcc #{filename}",
".cpp" => "g++ #{filename}",
".java" => "javac #{filename}",
".sh" => "chmod +x #{filename}",
}[fileext]
if command
system("cd #{dirname} && #{command}")
end
true
end
def run(file_path, in_file_path)
dirname = File.dirname(file_path)
fileext = File.extname(file_path)
basename = File.basename(file_path, fileext)
in_file_dirname = File.dirname(in_file_path)
in_file_basename = File.basename(in_file_path, ".in")
out_file_path = File.join(dirname, in_file_basename + ".out")
expected_out_path = File.join(in_file_dirname, in_file_basename + ".out")
command = {
".c" => "./a.out",
".cpp" => "./a.out",
".java" => "java #{basename}",
".js" => "~grade_cstaley/bin/js #{basename}",
".pl" => "perl #{file_path}",
".py" => "python #{file_path}",
".rb" => "ruby #{file_path}",
".sh" => "./#{file_path}",
}[fileext]
if !command
puts "Your language is not supported, or unknown file extension"
return false
end
system("cd #{dirname} && #{command} < #{in_file_path} > #{out_file_path}")
if !File.exists?(out_file_path)
return false
end
system("diff -b #{expected_out_path} #{out_file_path} > /dev/null")
end
def move_submission(old_file, graded_dir)
user = File.basename(old_file)
index = (1..1.0/0).find{|e| !File.exist?("#{graded_dir}/#{user}.#{e}")}
File.rename(old_file,"#{graded_dir}/#{user}.#{index}")
end
def grade(user, args)
if !$admin_users.include?(user)
return false
end
problems = args.any? ? [args.shift] : problem_list
submitter = args.any? ? args.shift : nil
problems.each do |problem|
in_files_path = File.join(problem_dir(problem), "tests", "*.in")
in_files = Dir[in_files_path]
submissions_paths = File.join(submission_dir(problem), "*")
submissions = Dir[submissions_paths]
submissions.each do |submission|
user = File.basename(submission)
next if !submitter.nil? and user != submitter
puts "\nGrading #{user} - #{problem}"
files_paths = File.join(submission_dir(problem, user), "*")
files = Dir[files_paths]
if files.size != 1
puts "#{files.size} files submitted, expected 1"
next
end
file = files.first
compile(file)
success = true
in_files.each do |in_file_path|
r = run(file, in_file_path)
success &= r
puts "\t#{File.basename in_file_path}: #{r ? "Passed" : "Failed"}"
end
puts "Result: #{success ? "Passed" : "Failed"}"
move_submission(submission, "#{problem_dir(problem)}/submissions/graded")
end
end
true
end
def contests(user, args)
cons = nil
DB::connect do |db|
if args.length != 1 || args[0] != '-a'
cons = db.active_contests
else
cons = db.all_contests
end
end
cons.each do |contest|
if contest == $default_contest
puts contest + ' *'
else
puts contest
end
end
true
end
def problems(user, args)
in_name = args[0]
prob_names = {}
problem_list.each do |problem_name|
prob_names[problem_name] = File.join(problem_dir(problem_name), "problem.txt")
end
if prob_names.keys.include? in_name
File.open(prob_names[in_name]) do |prob_file|
while line = prob_file.gets do
puts line
end
end
else
puts 'Available problems: ' + prob_names.keys.join(', ')
end
true
end
def help(user, args)
if args.length == 1
cmd = args[0].strip.downcase.to_sym
puts $commands[cmd]
true
else
puts $commands[:help]
false
end
end
if ARGV[0] == nil
puts "Usage: cpc <command>\n\n#{$commands_summary}"
else
cmd = ARGV[0].strip.downcase.to_sym
user = `/usr/bin/whoami`.strip
if $commands.include? cmd
cmd_f = method cmd
exit(cmd_f.call(user, ARGV[1..-1]))
else
puts "No such command.\n\n#{$commands_summary}"
exit(false)
end
end