Description:
removed calls to 'pwd', other uses of back quotes
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r102:a3aedb7e3776 - - 4 files changed: 7 inserted, 5 deleted
@@ -1,26 +1,28 | |||
|
1 | 1 | #!/usr/bin/ruby |
|
2 | 2 | |
|
3 | + require 'fileutils' | |
|
4 | + | |
|
3 | 5 | def talk(str) |
|
4 | 6 | if TALKATIVE |
|
5 | 7 | puts str |
|
6 | 8 | end |
|
7 | 9 | end |
|
8 | 10 | |
|
9 | 11 | def save_source(submission,dir,fname) |
|
10 | 12 | f = File.open("#{dir}/#{fname}","w") |
|
11 | 13 | f.write(submission.source) |
|
12 | 14 | f.close |
|
13 | 15 | end |
|
14 | 16 | |
|
15 | 17 | def call_judge(problem_home,language,problem_out_dir,fname) |
|
16 | 18 | ENV['PROBLEM_HOME'] = problem_home |
|
17 | 19 | Dir.chdir problem_out_dir |
|
18 | 20 | cmd = "#{problem_home}/script/judge #{language} #{fname}" |
|
19 | 21 | # puts "CMD: #{cmd}" |
|
20 | 22 | system(cmd) |
|
21 | 23 | end |
|
22 | 24 | |
|
23 | 25 | def read_result(test_result_dir) |
|
24 | 26 | cmp_msg_fname = "#{test_result_dir}/compiler_message" |
|
25 | 27 | cmp_msg = File.open(cmp_msg_fname).read |
|
26 | 28 | |
@@ -64,26 +66,26 | |||
|
64 | 66 | Dir.mkdir(user_dir) if !FileTest.exist?(user_dir) |
|
65 | 67 | |
|
66 | 68 | problem_out_dir = "#{user_dir}/#{problem.name}" |
|
67 | 69 | Dir.mkdir(problem_out_dir) if !FileTest.exist?(problem_out_dir) |
|
68 | 70 | |
|
69 | 71 | problem_home = "#{PROBLEMS_DIR}/#{problem.name}" |
|
70 | 72 | source_name = "#{problem.name}.#{lang_ext}" |
|
71 | 73 | |
|
72 | 74 | save_source(sub,problem_out_dir,source_name) |
|
73 | 75 | call_judge(problem_home,language,problem_out_dir,source_name) |
|
74 | 76 | save_result(sub,read_result("#{problem_out_dir}/test-result")) |
|
75 | 77 | end |
|
76 | 78 | |
|
77 | 79 | # reading environment and options |
|
78 | 80 | GRADER_ENV = 'exam' |
|
79 | 81 | puts "environment: #{GRADER_ENV}" |
|
80 | 82 | require File.dirname(__FILE__) + "/environment.rb" |
|
81 | 83 | |
|
82 | 84 | #main program |
|
83 | 85 | talk 'Reading rails environment' |
|
84 | 86 | |
|
85 | 87 | RAILS_ENV = 'development' |
|
86 | 88 | require RAILS_APP_DIR + '/config/environment' |
|
87 | 89 | |
|
88 |
- current_dir = |
|
|
90 | + current_dir = FileUtils.pwd | |
|
89 | 91 | grade(ARGV[0].to_i) |
@@ -10,49 +10,49 | |||
|
10 | 10 | # |
|
11 | 11 | class Engine |
|
12 | 12 | |
|
13 | 13 | attr_writer :room_maker |
|
14 | 14 | attr_writer :reporter |
|
15 | 15 | |
|
16 | 16 | def initialize(options={}) |
|
17 | 17 | # default options |
|
18 | 18 | if not options.include? :room_maker |
|
19 | 19 | options[:room_maker] = Grader::SubmissionRoomMaker.new |
|
20 | 20 | end |
|
21 | 21 | if not options.include? :reporter |
|
22 | 22 | options[:reporter] = Grader::SubmissionReporter.new |
|
23 | 23 | end |
|
24 | 24 | |
|
25 | 25 | @config = Grader::Configuration.get_instance |
|
26 | 26 | |
|
27 | 27 | @room_maker = options[:room_maker] |
|
28 | 28 | @reporter = options[:reporter] |
|
29 | 29 | end |
|
30 | 30 | |
|
31 | 31 | # takes a submission, asks room_maker to produce grading directories, |
|
32 | 32 | # calls grader scripts, and asks reporter to save the result |
|
33 | 33 | def grade(submission) |
|
34 |
- current_dir = |
|
|
34 | + current_dir = FileUtils.pwd | |
|
35 | 35 | |
|
36 | 36 | user = submission.user |
|
37 | 37 | problem = submission.problem |
|
38 | 38 | |
|
39 | 39 | # TODO: will have to create real exception for this |
|
40 | 40 | if user==nil or problem == nil |
|
41 | 41 | @reporter.report_error(submission,"Grading error: problem with submission") |
|
42 | 42 | #raise "engine: user or problem is nil" |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | # TODO: this is another hack so that output only task can be judged |
|
46 | 46 | if submission.language!=nil |
|
47 | 47 | language = submission.language.name |
|
48 | 48 | lang_ext = submission.language.ext |
|
49 | 49 | else |
|
50 | 50 | language = 'c' |
|
51 | 51 | lang_ext = 'c' |
|
52 | 52 | end |
|
53 | 53 | |
|
54 | 54 | # FIX THIS |
|
55 | 55 | talk 'some hack on language' |
|
56 | 56 | if language == 'cpp' |
|
57 | 57 | language = 'c++' |
|
58 | 58 | end |
@@ -45,82 +45,82 | |||
|
45 | 45 | # ARGV[3] --- sandbox directory |
|
46 | 46 | |
|
47 | 47 | if ARGV.length < 2 || ARGV.length > 4 |
|
48 | 48 | puts "Usage: judge <language> <program-source> [<test-result-directory>] [<sandbox-directory>]" |
|
49 | 49 | puts " <sandbox-directory> is defaulted to ./sandbox" |
|
50 | 50 | puts " <test-result-directory> is defaulted to ./test-result" |
|
51 | 51 | puts "WARNING: The judge script will forcefully create the (implicitly and explicitly) specified directories and remove anything inside it." |
|
52 | 52 | exit(127) |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | language = ARGV[0] |
|
56 | 56 | if language != "c" && language != "c++" && language != "pas" |
|
57 | 57 | log "You specified a language that is not supported: #{language}." |
|
58 | 58 | exit(127) |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | source_file = ARGV[1] |
|
62 | 62 | if File.exist?(source_file) == false |
|
63 | 63 | log "The source file does not exist." |
|
64 | 64 | exit(127) |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | log "Making test result and sandbox directories..." |
|
68 | 68 | |
|
69 |
- current_dir = |
|
|
69 | + current_dir = FileUtils.pwd | |
|
70 | 70 | current_dir.strip! |
|
71 | 71 | |
|
72 | 72 | if ARGV.length >= 3 |
|
73 | 73 | test_result_dir = ARGV[2] |
|
74 | 74 | else |
|
75 | 75 | test_result_dir = "#{current_dir}/test-result" |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | log "Test result directory: #{test_result_dir}" |
|
79 | 79 | clear_and_create_empty_dir(test_result_dir) |
|
80 | 80 | |
|
81 | 81 | if ARGV.length >= 4 |
|
82 | 82 | sandbox_dir = ARGV[3] |
|
83 | 83 | else |
|
84 | 84 | sandbox_dir = "#{current_dir}/sandbox" |
|
85 | 85 | end |
|
86 | 86 | log "Sandbox directory: #{sandbox_dir}" |
|
87 | 87 | clear_and_create_empty_dir(sandbox_dir) |
|
88 | 88 | |
|
89 | 89 | # Compile |
|
90 | 90 | log |
|
91 | 91 | log "Compiling..." |
|
92 | 92 | call_and_log("Cannot copy the source file to #{sandbox_dir}") { |
|
93 | 93 | FileUtils.cp(source_file, sandbox_dir) |
|
94 | 94 | } |
|
95 | 95 | begin |
|
96 | 96 | Dir.chdir sandbox_dir |
|
97 | 97 | rescue |
|
98 | 98 | log "ERROR: Cannot change directory to #{sandbox_dir}." |
|
99 | 99 | exit(127) |
|
100 | 100 | end |
|
101 | 101 | execute("#{problem_home}/script/compile #{language} #{source_file}", "Compilation error!") |
|
102 |
- compile_message = |
|
|
102 | + compile_message = open("compiler_message").read | |
|
103 | 103 | compile_message.strip! |
|
104 | 104 | call_and_log("Cannot move the compiler message to #{test_result_dir}.") { |
|
105 | 105 | FileUtils.mv("compiler_message", test_result_dir) |
|
106 | 106 | } |
|
107 | 107 | if !FileTest.exist?("a.out") |
|
108 | 108 | log "Cannot compile the source code. See message in #{test_result_dir}/compile_message" |
|
109 | 109 | exit(127) |
|
110 | 110 | else |
|
111 | 111 | call_and_log("Cannot move the compiled program to #{test_result_dir}") { |
|
112 | 112 | FileUtils.mv("a.out",test_result_dir) |
|
113 | 113 | } |
|
114 | 114 | FileUtils.rm_rf("#{sandbox_dir}/.") |
|
115 | 115 | end |
|
116 | 116 | |
|
117 | 117 | require "#{problem_home}/script/test_dsl.rb" |
|
118 | 118 | load "#{problem_home}/test_cases/all_tests.cfg" |
|
119 | 119 | problem = Problem.get_instance |
|
120 | 120 | |
|
121 | 121 | if problem.well_formed? == false |
|
122 | 122 | log "The problem specification is not well formed." |
|
123 | 123 | exit(127) |
|
124 | 124 | end |
|
125 | 125 | |
|
126 | 126 | # Doing the testing. |
@@ -90,49 +90,49 | |||
|
90 | 90 | |
|
91 | 91 | # Restore PROBLEM_HOME |
|
92 | 92 | ENV['PROBLEM_HOME'] = problem_home |
|
93 | 93 | |
|
94 | 94 | # Create the result file. |
|
95 | 95 | result_file = File.new("result", "w") |
|
96 | 96 | comment_file = File.new("comment", "w") |
|
97 | 97 | |
|
98 | 98 | # Check if the program actually produced any output. |
|
99 | 99 | run_result_file = File.new("run_result", "r") |
|
100 | 100 | run_result = run_result_file.readlines |
|
101 | 101 | run_result_file.close |
|
102 | 102 | |
|
103 | 103 | run_stat = run_result[run_result.length-1] |
|
104 | 104 | running_time = extract_time(run_stat) |
|
105 | 105 | |
|
106 | 106 | report = lambda{ |status, points, comment| |
|
107 | 107 | result_file.write status.strip |
|
108 | 108 | result_file.write "\n" |
|
109 | 109 | result_file.write points.to_s.strip |
|
110 | 110 | result_file.write "\n" |
|
111 | 111 | result_file.write run_stat.strip |
|
112 | 112 | result_file.write "\n" |
|
113 | 113 | result_file.close |
|
114 |
- |
|
|
114 | + FileUtils.rm "run_result" | |
|
115 | 115 | # `rm output.txt` --- keep the output |
|
116 | 116 | |
|
117 | 117 | comment_file.write comment |
|
118 | 118 | |
|
119 | 119 | # added for debuggin --- jittat |
|
120 | 120 | comment_file.write "--run-result--\n" |
|
121 | 121 | run_result.each do |l| |
|
122 | 122 | comment_file.write l |
|
123 | 123 | end |
|
124 | 124 | |
|
125 | 125 | comment_file.close |
|
126 | 126 | |
|
127 | 127 | log "Done!" |
|
128 | 128 | exit(0) |
|
129 | 129 | } |
|
130 | 130 | |
|
131 | 131 | if run_result[0][0,2] != "OK" |
|
132 | 132 | log "There was a runtime error." |
|
133 | 133 | report.call(run_result[0], 0, "No comment.\n") |
|
134 | 134 | end |
|
135 | 135 | |
|
136 | 136 | if running_time[:user].to_f + running_time[:sys].to_f > time_limit |
|
137 | 137 | log "Time limit exceeded." |
|
138 | 138 | report.call("Time limit exceeded", 0, "No comment.\n") |
You need to be logged in to leave comments.
Login now