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,99 +1,99 | |||||
|
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 |
You need to be logged in to leave comments.
Login now