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