-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathexport_submissions
executable file
·45 lines (37 loc) · 1.01 KB
/
export_submissions
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
#!/usr/bin/env ruby
# frozen_string_literal: true
root_dir = File.expand_path('../', File.dirname(__FILE__))
require root_dir + '/config/environment'
require 'fileutils'
if ![1, 2].include?(ARGV.length) || ['-h', '--help'].include?(ARGV[0])
puts 'Usage: script/export_submissions dest_dir [course_name]'
puts
exit(false)
end
dest_dir = ARGV[0]
def write_file(filename, data)
File.open(filename, 'wb') { |f| f.write(data) }
end
query = Submission.order(:id)
if ARGV[1]
begin
course = Course.find_by!(name: ARGV[1])
rescue StandardError
puts "Couldn't find course: #{ARGV[1]}"
exit(false)
end
query = query.where(course_id: course.id)
end
total = query.count
if total == 0
puts '0 submissions found'
exit(true)
end
i = 1
query.each do |sub|
puts "Exporting submission id=#{sub.id} for #{sub.exercise_name} (#{i}/#{total})"
FileUtils.mkdir_p("#{dest_dir}/#{sub.exercise_name}")
dest_file = "#{dest_dir}/#{sub.exercise_name}/#{sub.id}.zip"
write_file(dest_file, sub.return_file)
i += 1
end