-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathsubmissions_handler.rb
More file actions
223 lines (180 loc) · 7.17 KB
/
submissions_handler.rb
File metadata and controls
223 lines (180 loc) · 7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
=begin
This file is part of SSID.
SSID is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SSID is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with SSID. If not, see <http://www.gnu.org/licenses/>.
=end
require 'zip'
require 'open3'
class ReorgBot
attr_accessor :path
attr_accessor :dir
def initialize(path)
raise ArgumentError, "path '#{path}' does not exist!" unless File.exist?(path)
raise ArgumentError, "path '#{path} is not a directory you dolt!" unless File.directory?(path)
@path = path
@dir = Dir.new(path)
end
def dirs
@dir.select { |f| File.directory?(File.join(@path, f)) }
end
def empty_dirs
dirs.select { |d| Dir[File.join(@path, d, '*')].empty? }
end
def non_empty_dirs
dirs - empty_dirs
end
def remove_empty_dirs
log = [];
empty_dirs.each do |d|
FileUtils.rm_rf(File.join(@path, d))
log << %Q{[#{Time.now.in_time_zone}] Deleting directory: #{File.join(@path, d)}}
end
log
end
end
module SubmissionsHandler
# Params for data truncation
MAX_DATA_CHAR_SIZE = 64000
DATA_TRUNCATE_MSG = "... (Data got truncated)"
def self.process_upload(file, isMapEnabled, mapfile, assignment)
upload_dir = File.join(".", "upload", assignment.id.to_s)
# Clear upload dir if exists
FileUtils.remove_dir upload_dir if File.exist? upload_dir
# Create upload dir
FileUtils.mkdir_p(upload_dir)
# Keep log
upload_log = []
upload_log << assignment.upload_log.truncate(MAX_DATA_CHAR_SIZE, separator: ' ', omission: DATA_TRUNCATE_MSG) if assignment.upload_log
upload_log << %Q{[#{Time.now.in_time_zone}] Received file: #{file.original_filename}}
# Rename upload to original file name
upload_file = File.join(upload_dir, file.original_filename)
# Move upload into dir
FileUtils.copy_entry(file.path, upload_file)
# Add filters for file types
accepted_formats = [".py",".java", ".cpp", ".c", ".h", ".scala", ".m", ".ml", ".mli", ".r"]
# Extract submissions into dir
Zip::File.open(upload_file) { |zip_file|
zip_file.each { |f|
# isdirectory or filter by accepted file extension
if File.directory?(f.name) or accepted_formats.include? File.extname(f.name)
upload_log << %Q{[#{Time.now.in_time_zone}] Extracting #{f.name}}
# Obtain File Path
f_path = File.join(upload_dir, f.name)
# Create Directory
FileUtils.mkdir_p(File.dirname(f_path))
# Extract files into the file path
zip_file.extract(f, f_path) unless File.exist?(f_path)
# Reject files that passed the extension test but might be a binary file in disguise
# if f.file? filepath
# upload_log << %Q{[#{Time.now.in_time_zone}] Detected binary file, deleting #{f.name}}
# FileUtils.rm filepath
# end
else
upload_log << %Q{[#{Time.now.in_time_zone}] Invalid file type, Ignoring #{f.name} with extension #{File.extname(f.name)}}
end
}
}
upload_log << %Q{[#{Time.now.in_time_zone}] Checking for empty directories}
bot = ReorgBot.new(upload_dir)
upload_log << bot.remove_empty_dirs
# Move map file (if uploaded by user) into dir
if (isMapEnabled)
upload_map_file = File.join(upload_dir, "mapfile.csv")
FileUtils.copy_entry(mapfile.path, upload_map_file)
end
# Save log
upload_log << %Q{[#{Time.now.in_time_zone}] Unzip complete}
assignment.upload_log = upload_log.join("\n")
assignment.save
# Remove zip file
FileUtils.rm upload_file, force: true
# Return path to dir
upload_dir
end
def self.process_submissions(compare_dir, assignment, isMapEnabled)
# Read database configuration
config = Rails.configuration.database_configuration
host = config[Rails.env]["host"]
database = config[Rails.env]["database"]
username = config[Rails.env]["username"]
password = config[Rails.env]["password"]
# Run the java program and get its pid
command = %Q{java -Xmx2048M -Dlog4j2.configurationFile="#{Rails.application.config.plagiarism_detection_log_configuration_path}" -jar "#{Rails.application.config.plagiarism_detection_path}" } +
%Q{#{assignment.id} #{compare_dir} #{assignment.language.downcase} } +
%Q{#{assignment.min_match_length} #{assignment.ngram_size} } +
%Q{#{host} #{database} #{username} #{password} #{isMapEnabled}}
# Fork to run java program in background
ruby_pid = Process.fork do
java_log = ""
java_status = nil
Open3.popen2e({ "LD_LIBRARY_PATH" => Rails.application.config.ld_library_path }, command) { |i,o,t|
java_log << o.gets until o.eof?
java_status = t.value
}
# Update log
upload_log = []
upload_log << assignment.upload_log if assignment.upload_log
upload_log << java_log.truncate(MAX_DATA_CHAR_SIZE, separator: ' ', omission: DATA_TRUNCATE_MSG)
assignment.upload_log = upload_log.join("\n")
# Update status
process = assignment.submission_similarity_process
if java_status.exitstatus == 0
process.status = SubmissionSimilarityProcess::STATUS_COMPLETED
else
process.status = SubmissionSimilarityProcess::STATUS_ERRONEOUS
end
# Save
assignment.transaction do
assignment.save
process.save
end
end
# Create process with pid
SubmissionSimilarityProcess.create do |p|
p.assignment_id = assignment.id
p.pid = ruby_pid
p.status = SubmissionSimilarityProcess::STATUS_RUNNING
end
Process.detach(ruby_pid) # Parent will not wait
end
def self.process_cluster_group(cluster_group)
# Get assignment
assignment = cluster_group.assignment
# Read database configuration
config = Rails.configuration.database_configuration
host = config[Rails.env]["host"]
database = config[Rails.env]["database"]
username = config[Rails.env]["username"]
password = config[Rails.env]["password"]
# Run the java program and get its pid
command = %Q{java -Xmx1024M -jar "#{Rails.application.config.submissions_clustering_path}" } +
%Q{#{assignment.id} #{cluster_group.id} #{cluster_group.cut_off_criterion} } +
%Q{#{host} #{database} #{username} #{password} 2>&1}
java_log = ""
IO.popen(command) { |pipe|
java_log << pipe.gets until pipe.eof?
}
java_status = $?
raise "Submissions clustering error: #{java_log}" unless java_status.exitstatus == 0
end
private
def self.string_from_combined_files(path)
strings = []
if File.directory? path
Dir.glob(File.join(path, "*")).sort.each { |subpath|
strings << string_from_combined_files(subpath)
}
else
strings << File.open(path).readlines.join
end
strings.join("\n")
end
end