Description:
added rename_problem script, fixed require error for dir_init git-svn-id: http://theory.cpe.ku.ac.th/grader/judge/trunk/scripts@420 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Comments:
0 Commit comments 0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
Add another comment

r78:3df292e1c18d - - 2 files changed: 101 inserted, 1 deleted

@@ -0,0 +1,100
1 + #!/usr/bin/env ruby
2 +
3 + ENVIRONMENT_DIRS = ['ev', 'ev-exam']
4 +
5 + def config
6 + Grader::Configuration.get_instance
7 + end
8 +
9 + def rename_problem(old_problem_name, new_problem_name)
10 +
11 + if valid_problem_name(new_problem_name)
12 + puts "Bad new problem name: #{new_problem_name}"
13 + return
14 + end
15 +
16 + problem = Problem.find_by_name(old_problem_name)
17 + if problem==nil
18 + puts "Problem #{old_problem_name} does not exist."
19 + return
20 + end
21 +
22 + puts "Problem: #{old_problem_name} -> #{new_problem_name}"
23 +
24 + ENVIRONMENT_DIRS.each do |dir|
25 + problem_dir = File.join(GRADER_ROOT,'..',dir,old_problem_name)
26 + new_problem_dir = File.join(GRADER_ROOT,'..',dir,new_problem_name)
27 +
28 + if FileTest.exists? problem_dir
29 + puts "Moving #{problem_dir} to #{new_problem_dir}."
30 + File.rename(problem_dir, new_problem_dir)
31 +
32 + tr_problem_dir = File.join(GRADER_ROOT,'..',dir,
33 + 'test_request',old_problem_name)
34 + new_tr_problem_dir = File.join(GRADER_ROOT,'..',dir,
35 + 'test_request',new_problem_name)
36 + File.rename(tr_problem_dir, new_tr_problem_dir)
37 + end
38 + end
39 +
40 + problem.name = new_problem_name
41 + problem.save
42 + end
43 +
44 + def usage
45 + puts <<USAGE
46 + Usage:
47 + rename_problem [old_name] [new_name]
48 + or
49 + rename_problem -f [filename]
50 +
51 + When using with -f, that file should contain, for each line, the old
52 + problem name and its new name.
53 +
54 + This script should be called at the judge root dir where dirs 'ev' and
55 + 'ev-exam' are.
56 + USAGE
57 + end
58 +
59 + def valid_problem_name(name)
60 + if name.length==0:
61 + return false
62 + else
63 + return !(/^[a-zA-Z0-9_\-]+$/ === name)
64 + end
65 + end
66 +
67 + if (ARGV.length!=2)
68 + usage
69 + exit(0)
70 + end
71 +
72 + if ARGV[0]=='-f' and !FileTest.exists?(ARGV[1])
73 + puts "File #{ARGV[1]} does not exist."
74 + usage
75 + exit(0)
76 + end
77 +
78 + # load grader environment
79 + GRADER_ENV = 'grading'
80 + require File.join(File.dirname(__FILE__),'config/environment')
81 +
82 + # boot rails, to be able to rename the problem
83 + RAILS_ENV = config.rails_env
84 + require RAILS_ROOT + '/config/environment'
85 +
86 + if ARGV[0]!='-f'
87 + old_problem_name = ARGV[0]
88 + new_problem_name = ARGV[1]
89 +
90 + rename_problem(old_problem_name, new_problem_name)
91 + else
92 + lines = IO.readlines(ARGV[1])
93 + lines.each do |line|
94 + items = line.split
95 + if items.length==2
96 + old_name, new_name = items
97 + rename_problem(old_name, new_name)
98 + end
99 + end
100 + end
@@ -1,175 +1,175
1 require 'fileutils'
1 require 'fileutils'
2 require 'ftools'
2 require 'ftools'
3 - require 'lib/dir_init'
3 + require File.join(File.dirname(__FILE__),'dir_init')
4
4
5 module Grader
5 module Grader
6
6
7 #
7 #
8 # A grader engine grades a submission, against anything: a test
8 # A grader engine grades a submission, against anything: a test
9 # data, or a user submitted test data. It uses two helpers objects:
9 # data, or a user submitted test data. It uses two helpers objects:
10 # room_maker and reporter.
10 # room_maker and reporter.
11 #
11 #
12 class Engine
12 class Engine
13
13
14 attr_writer :room_maker
14 attr_writer :room_maker
15 attr_writer :reporter
15 attr_writer :reporter
16
16
17 def initialize(room_maker=nil, reporter=nil)
17 def initialize(room_maker=nil, reporter=nil)
18 @config = Grader::Configuration.get_instance
18 @config = Grader::Configuration.get_instance
19
19
20 @room_maker = room_maker || Grader::SubmissionRoomMaker.new
20 @room_maker = room_maker || Grader::SubmissionRoomMaker.new
21 @reporter = reporter || Grader::SubmissionReporter.new
21 @reporter = reporter || Grader::SubmissionReporter.new
22 end
22 end
23
23
24 # takes a submission, asks room_maker to produce grading directories,
24 # takes a submission, asks room_maker to produce grading directories,
25 # calls grader scripts, and asks reporter to save the result
25 # calls grader scripts, and asks reporter to save the result
26 def grade(submission)
26 def grade(submission)
27 current_dir = `pwd`.chomp
27 current_dir = `pwd`.chomp
28
28
29 user = submission.user
29 user = submission.user
30 problem = submission.problem
30 problem = submission.problem
31
31
32 # TODO: will have to create real exception for this
32 # TODO: will have to create real exception for this
33 if user==nil or problem == nil
33 if user==nil or problem == nil
34 @reporter.report_error(submission,"Grading error: problem with submission")
34 @reporter.report_error(submission,"Grading error: problem with submission")
35 #raise "engine: user or problem is nil"
35 #raise "engine: user or problem is nil"
36 end
36 end
37
37
38 # TODO: this is another hack so that output only task can be judged
38 # TODO: this is another hack so that output only task can be judged
39 if submission.language!=nil
39 if submission.language!=nil
40 language = submission.language.name
40 language = submission.language.name
41 lang_ext = submission.language.ext
41 lang_ext = submission.language.ext
42 else
42 else
43 language = 'c'
43 language = 'c'
44 lang_ext = 'c'
44 lang_ext = 'c'
45 end
45 end
46
46
47 # FIX THIS
47 # FIX THIS
48 talk 'some hack on language'
48 talk 'some hack on language'
49 if language == 'cpp'
49 if language == 'cpp'
50 language = 'c++'
50 language = 'c++'
51 end
51 end
52
52
53 # COMMENT: should it be only source.ext?
53 # COMMENT: should it be only source.ext?
54 if problem!=nil
54 if problem!=nil
55 source_name = "#{problem.name}.#{lang_ext}"
55 source_name = "#{problem.name}.#{lang_ext}"
56 else
56 else
57 source_name = "source.#{lang_ext}"
57 source_name = "source.#{lang_ext}"
58 end
58 end
59
59
60 begin
60 begin
61 grading_dir = @room_maker.produce_grading_room(submission)
61 grading_dir = @room_maker.produce_grading_room(submission)
62 @room_maker.save_source(submission,source_name)
62 @room_maker.save_source(submission,source_name)
63 problem_home = @room_maker.find_problem_home(submission)
63 problem_home = @room_maker.find_problem_home(submission)
64
64
65 # puts "GRADING DIR: #{grading_dir}"
65 # puts "GRADING DIR: #{grading_dir}"
66 # puts "PROBLEM DIR: #{problem_home}"
66 # puts "PROBLEM DIR: #{problem_home}"
67
67
68 dinit = DirInit::Manager.new(problem_home)
68 dinit = DirInit::Manager.new(problem_home)
69
69
70 dinit.setup do
70 dinit.setup do
71 copy_log = copy_script(problem_home)
71 copy_log = copy_script(problem_home)
72 save_copy_log(problem_home,copy_log)
72 save_copy_log(problem_home,copy_log)
73 end
73 end
74
74
75 call_judge(problem_home,language,grading_dir,source_name)
75 call_judge(problem_home,language,grading_dir,source_name)
76
76
77 @reporter.report(submission,"#{grading_dir}/test-result")
77 @reporter.report(submission,"#{grading_dir}/test-result")
78
78
79 dinit.teardown do
79 dinit.teardown do
80 copy_log = load_copy_log(problem_home)
80 copy_log = load_copy_log(problem_home)
81 clear_copy_log(problem_home)
81 clear_copy_log(problem_home)
82 clear_script(copy_log,problem_home)
82 clear_script(copy_log,problem_home)
83 end
83 end
84
84
85 rescue RuntimeError => msg
85 rescue RuntimeError => msg
86 @reporter.report_error(submission,"Grading error: #{msg}")
86 @reporter.report_error(submission,"Grading error: #{msg}")
87
87
88 ensure
88 ensure
89 @room_maker.clean_up(submission)
89 @room_maker.clean_up(submission)
90 Dir.chdir(current_dir) # this is really important
90 Dir.chdir(current_dir) # this is really important
91 end
91 end
92 end
92 end
93
93
94 protected
94 protected
95
95
96 def talk(str)
96 def talk(str)
97 if @config.talkative
97 if @config.talkative
98 puts str
98 puts str
99 end
99 end
100 end
100 end
101
101
102 def call_judge(problem_home,language,grading_dir,fname)
102 def call_judge(problem_home,language,grading_dir,fname)
103 ENV['PROBLEM_HOME'] = problem_home
103 ENV['PROBLEM_HOME'] = problem_home
104
104
105 talk grading_dir
105 talk grading_dir
106 Dir.chdir grading_dir
106 Dir.chdir grading_dir
107 cmd = "#{problem_home}/script/judge #{language} #{fname}"
107 cmd = "#{problem_home}/script/judge #{language} #{fname}"
108 talk "CMD: #{cmd}"
108 talk "CMD: #{cmd}"
109 system(cmd)
109 system(cmd)
110 end
110 end
111
111
112 def get_std_script_dir
112 def get_std_script_dir
113 GRADER_ROOT + '/std-script'
113 GRADER_ROOT + '/std-script'
114 end
114 end
115
115
116 def copy_script(problem_home)
116 def copy_script(problem_home)
117 script_dir = "#{problem_home}/script"
117 script_dir = "#{problem_home}/script"
118 std_script_dir = get_std_script_dir
118 std_script_dir = get_std_script_dir
119
119
120 raise "std-script directory not found" if !FileTest.exist?(std_script_dir)
120 raise "std-script directory not found" if !FileTest.exist?(std_script_dir)
121
121
122 scripts = Dir[std_script_dir + '/*']
122 scripts = Dir[std_script_dir + '/*']
123
123
124 copied = []
124 copied = []
125
125
126 scripts.each do |s|
126 scripts.each do |s|
127 fname = File.basename(s)
127 fname = File.basename(s)
128 if !FileTest.exist?("#{script_dir}/#{fname}")
128 if !FileTest.exist?("#{script_dir}/#{fname}")
129 copied << fname
129 copied << fname
130 system("cp #{s} #{script_dir}")
130 system("cp #{s} #{script_dir}")
131 end
131 end
132 end
132 end
133
133
134 return copied
134 return copied
135 end
135 end
136
136
137 def copy_log_filename(problem_home)
137 def copy_log_filename(problem_home)
138 return File.join(problem_home, '.scripts_copied')
138 return File.join(problem_home, '.scripts_copied')
139 end
139 end
140
140
141 def save_copy_log(problem_home, log)
141 def save_copy_log(problem_home, log)
142 f = File.new(copy_log_filename(problem_home),"w")
142 f = File.new(copy_log_filename(problem_home),"w")
143 log.each do |fname|
143 log.each do |fname|
144 f.write("#{fname}\n")
144 f.write("#{fname}\n")
145 end
145 end
146 f.close
146 f.close
147 end
147 end
148
148
149 def load_copy_log(problem_home)
149 def load_copy_log(problem_home)
150 f = File.new(copy_log_filename(problem_home),"r")
150 f = File.new(copy_log_filename(problem_home),"r")
151 log = []
151 log = []
152 f.readlines.each do |line|
152 f.readlines.each do |line|
153 log << line.strip
153 log << line.strip
154 end
154 end
155 f.close
155 f.close
156 log
156 log
157 end
157 end
158
158
159 def clear_copy_log(problem_home)
159 def clear_copy_log(problem_home)
160 File.delete(copy_log_filename(problem_home))
160 File.delete(copy_log_filename(problem_home))
161 end
161 end
162
162
163 def clear_script(log,problem_home)
163 def clear_script(log,problem_home)
164 log.each do |s|
164 log.each do |s|
165 system("rm #{problem_home}/script/#{s}")
165 system("rm #{problem_home}/script/#{s}")
166 end
166 end
167 end
167 end
168
168
169 def mkdir_if_does_not_exist(dirname)
169 def mkdir_if_does_not_exist(dirname)
170 Dir.mkdir(dirname) if !FileTest.exist?(dirname)
170 Dir.mkdir(dirname) if !FileTest.exist?(dirname)
171 end
171 end
172
172
173 end
173 end
174
174
175 end
175 end
You need to be logged in to leave comments. Login now