Class: Ccup::Exec

Inherits:
Object
  • Object
show all
Defined in:
lib/ccup/exec.rb

Constant Summary collapse

<<-MSG

Usage
  ccup SUBMISSION_FILE INPUT_FOLDER OUTPUT_FILE ANSWER_KEY_FILE

Description
  Creates a temporary directory and copies SUBMISSION_FILE, the files inside
  INPUT_FOLDER, and ANSWER_KEY_FILE into it before compiling (if needed) and
  executing the SUBMISSION_FILE.

  Verification will be done by running "diff" on OUTPUT_FILE and ANSWER_KEY_FILE
  and putting the results into result.txt on the same folder. The running time
  will also be added to result.txt.

  The program ends with displaying the temporary folder.

MSG

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Exec

Returns a new instance of Exec.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ccup/exec.rb', line 25

def initialize(argv)
  if argv.size != 4
    $stderr.puts BANNER
    @error = true
    return
  end
  @submission = argv[0]
  @input_folder = argv[1]
  @output_file = argv[2]
  @answer_key = argv[3]
  unless @error
    @submission_file = File.basename(@submission)
    validate
    @pl = determine_pl(File.extname(@submission))
  end
end

Instance Attribute Details

#answer_keyObject (readonly)

Returns the value of attribute answer_key.



7
8
9
# File 'lib/ccup/exec.rb', line 7

def answer_key
  @answer_key
end

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/ccup/exec.rb', line 7

def error
  @error
end

#input_folderObject (readonly)

Returns the value of attribute input_folder.



7
8
9
# File 'lib/ccup/exec.rb', line 7

def input_folder
  @input_folder
end

#output_fileObject (readonly)

Returns the value of attribute output_file.



7
8
9
# File 'lib/ccup/exec.rb', line 7

def output_file
  @output_file
end

#submissionObject (readonly)

Returns the value of attribute submission.



7
8
9
# File 'lib/ccup/exec.rb', line 7

def submission
  @submission
end

#temp_folderObject (readonly)

Returns the value of attribute temp_folder.



7
8
9
# File 'lib/ccup/exec.rb', line 7

def temp_folder
  @temp_folder
end

Instance Method Details

#compareObject



172
173
174
175
176
177
178
# File 'lib/ccup/exec.rb', line 172

def compare
  @results_file.puts "Comparing output with answer key..."
  pid, stdin, stdout, stderr = Open4::popen4 "diff #{@output_file} #{File.basename @answer_key}"
  ignored, status = Process::waitpid2 pid

  @results_file.puts stdout.read.strip
end

#compileObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ccup/exec.rb', line 112

def compile
  command = case @pl
  when :c
    "gcc -Wall #{@submission_file} -o #{root_name}"
  when :cpp
    "g++ -Wall #{@submission_file} -o #{root_name}"
  when :csharp
    "gmcs #{@submission_file}"
  when :java
    "javac #{@submission_file}"
  when :vbnet
    "vbnc #{@submission_file}"
  end
  if command
    @results_file.puts "Compiling submission..."
    pid, stdin, stdout, stderr = Open4::popen4 command
    ignored, status = Process::waitpid2 pid
    unless status.exitstatus == 0
      @error = true
      @results_file.puts "ERROR ENCOUNTERED:"
      @results_file.puts stderr.read.strip
    end
  end
end

#determine_pl(ext) ⇒ Object



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
# File 'lib/ccup/exec.rb', line 57

def determine_pl(ext)
  case ext
  when ".c"
    :c
  when ".cpp"
    :cpp
  when ".cs"
    :csharp
  when ".java"
    :java
  when ".php"
    :php
  when ".vb"
    :vbnet
  when ".rb"
    :ruby
  when ".py"
    :python
  when ".js"
    :js
  else
    @error = true
    $stderr.puts "Invalid submission"
  end
end

#prepare_temp_folderObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/ccup/exec.rb', line 101

def prepare_temp_folder
  temp_folder = Dir.mktmpdir
  FileUtils.cp @submission, temp_folder
  Dir.chdir @input_folder do
    FileUtils.cp Dir.entries(".").reject { |x| File.directory? x }, temp_folder
  end
  FileUtils.cp @answer_key, temp_folder
  `touch #{File.join temp_folder, @output_file}`
  temp_folder
end

#processObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ccup/exec.rb', line 83

def process
  return if @error
  @temp_folder = prepare_temp_folder
  Dir.chdir @temp_folder do 
    @results_file = File.open("results.txt", "w")
    compile
    unless @error
      run
    end
    unless @error
      compare
    end
    @results_file.puts "Done."
    @results_file.close
  end
  return self
end

#root_nameObject



168
169
170
# File 'lib/ccup/exec.rb', line 168

def root_name
  File.basename(@submission_file, File.extname(@submission_file))
end

#runObject



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
# File 'lib/ccup/exec.rb', line 137

def run
  command = case @pl
  when :c, :cpp
    "./#{root_name}"
  when :csharp, :vbnet
    "mono #{root_name}.exe"
  when :java
    "java #{root_name}"
  when :ruby
    "ruby #{@submission_file}"
  when :python
    "python #{@submission_file}"
  when :php
    "php #{@submission_file}"
  when :js
    "node #{@submission_file}"
  end
  @results_file.puts "Running submission..."
  start_at = Time.now
  pid, stdin, stdout, stderr = Open4::popen4 command
  ignored, status = Process::waitpid2 pid
  
  unless status.exitstatus == 0
    @error = true
    @results_file.puts "ERROR ENCOUNTERED:"
    @results_file.puts stderr.read.strip
  end
  #TODO check status
  @results_file.puts "Execution time: #{ Time.now - start_at } seconds"
end

#validateObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ccup/exec.rb', line 42

def validate
  unless File.exist? @submission
    @error = true
    $stderr.puts "Can't find #{@submission}"
  end
  unless Dir.exist? @input_folder
    @error = true
    $stderr.puts "Can't find #{@input_folder}"
  end
  unless File.exist? @answer_key
    @error = true
    $stderr.puts "Can't find #{@answer_key}"
  end
end