Created
May 21, 2014 05:33
-
-
Save mufid/b12c5d3fb05f30da3d0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
# MIT License | |
# Debug purpose | |
first = true; | |
# Some dummy class | |
class Student | |
attr_accessor :id # Retrieved from myself.txt | |
attr_accessor :full_name # Retrieved from myself.txt | |
attr_accessor :user_name # Retrieved from regex in catat-Lab08.txt | |
attr_accessor :chain | |
attr_accessor :run_client # Did he/she run ./client? | |
attr_accessor :run_server # Did he/she run ./server? | |
def init | |
@chain = [] | |
end | |
end | |
# Initial variable | |
students = [] | |
puts "--- PHASE 1: Retrieving all initial data ---" | |
# Iterate within current directory | |
Dir.foreach '.' do |x| | |
# Skip: current directory link, | |
# up one level directory link, | |
# non-NPM folder | |
next unless x.match /^[0-9]/ | |
next if %{. ..}.include? x | |
next unless File.directory? x | |
# Debug purpose. Testing for first data | |
next if not first | |
# Show status to user | |
puts "Analyzing #{x}" | |
catat_path = File.join('.', x, 'catat-Lab08.txt') | |
catat = File.read(catat_path) | |
user_name_scan = catat.scan /([.a-zA-z0-9]+)@box:/ | |
user_name = user_name_scan.first.first | |
client_ran = | |
catat.scan(/\.\/client/).length > 0 | |
server_ran = | |
catat.scan(/\.\/server/).length > 0 | |
myself_path = File.join('.', x, 'myself.txt') | |
myself = File.read(myself_path) | |
myself_datas = myself.split(',') | |
full_name = myself_datas[1] | |
id = myself_datas[0] | |
# Create new student object with their respective id | |
student = Student.new | |
student.id = id | |
student.full_name = full_name | |
student.user_name = user_name | |
student.run_client = client_ran | |
student.run_server = server_ran | |
# Add identified student into the list of student | |
students << student | |
end | |
puts | |
puts '--- PHASE 2: Analyzing chain' | |
puts | |
students.each do |student| | |
puts "Chain analysis for: #{student.full_name}" | |
catat_path = File.join('.', student.id, 'catat-Lab08.txt') | |
catat = File.read(catat_path) | |
log_scan = catat.scan /(From .+ to .+)/ | |
log_text = log_scan.last.last.gsub /(From|: Hello)/, '' | |
chain = log_text.split(' to ').map {|x| x.strip} | |
student.chain = chain | |
end | |
puts | |
puts '--- PHASE 3: Saving to file ---' | |
puts | |
students.each do |student| | |
puts "Writing log for #{student.full_name}" | |
log_path = File.join('.', 'log', "#{student.id}.txt") | |
File.open(log_path, 'w') do |file| | |
file.puts 'Log for:' | |
file.puts '' | |
file.puts "#{student.full_name}" | |
file.puts "#{student.id}" | |
file.puts "Identified as: #{student.user_name}" | |
file.puts '' | |
file.puts 'Client ran? #{student.run_client}' | |
file.puts 'Server ran? #{student.run_server}' | |
file.puts '' | |
file.puts 'Chain:' | |
student.chain.each do |u| | |
file.puts u | |
end | |
end | |
end | |
puts | |
puts 'Done! See ./log folder' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment