-
Notifications
You must be signed in to change notification settings - Fork 0
/
partThree.rb
executable file
·137 lines (95 loc) · 3.33 KB
/
partThree.rb
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
# Author: Peter O'Reilly
# DT340A - Linux System Administartion - Assignment
# Ruby Script that groups all log files by yyyy-mm-dd
# and places them into an archive file with their respective name.
# If multiple archives on same day will time stamp each subsequent archive file
# to prevent over-writing
module FileManager
class FileZip
require 'fileutils'
attr_accessor :logFiles, :trimmedLogFiles, :fileOccurences, :filesAlreadyLocated, :application_log_dir
def initialize()
workingDirectory = Dir.pwd
@application_log_dir = workingDirectory<<'/application-logs'
Dir.chdir(@application_log_dir)
@logFiles =Dir.entries(Dir.pwd).select {|file| !File.directory?(file) && file=~/\d/ && !file.include?('tar')}
@fileOccurences = Array.new
@filesAlreadyLocated = Array.new
@trimmedLogFiles = Array.new
end
def trimHoursFromLogFiles()
@logFiles.each do |file|
file.chomp()
file = file[0...-3]
@trimmedLogFiles << file
end
end
def getIndeciesOfMatchingFiles()
@trimmedLogFiles.each do |file|
if [email protected]?(file)
results_with_index = @trimmedLogFiles.each_with_index.select { |name, index| name.eql?(file)}
result_arr = results_with_index.map! { |i| i[1] }
@fileOccurences << result_arr
@filesAlreadyLocated << file
end
end
end
def placeFilesInDirectory()
@fileOccurences.each_with_index do |fileIndexArray, index|
dirName = @filesAlreadyLocated[index]
if !File.directory?(dirName)
Dir.mkdir(dirName)
end
fileIndexArray.each do |fileIndex|
FileUtils.mv(@logFiles[fileIndex], dirName)
end
readContentsOfDirectory(dirName)
end
end
def readContentsOfDirectory(directory)
dir = @application_log_dir +"/"+ directory
logDirectory = Dir.entries(dir).select {|logFile| !File.directory?(logFile)}
uniqueMovieTitles = Array.new
logDirectory.each do |file|
File.open(dir+"/"+file, 'r').each_line do |line|
splitArr = line.split(' ', 8)
movie = splitArr[7]
if !uniqueMovieTitles.include?(movie)
uniqueMovieTitles << movie
end
end
end
generateReport(dir, uniqueMovieTitles, directory)
end
def generateReport(directory, movieArrayList, newFileName)
fileName = newFileName+"_report_summary.txt"
title = "Unique Movies in this archive \n"
underline = "---------------------------------\n"
report = ""
report << title << underline
movieArrayList.each do |movie|
report << movie+"\n"
end
file = File.new(fileName, 'w')
file.write(report)
file.close()
FileUtils.mv(fileName, directory)
zippedFile = newFileName+".tar.gz"
if File.exists?(zippedFile)
hour = Time.now.hour.to_s
minutes = Time.now.min.to_s
seconds = Time.now.sec.to_s
timeString = hour+"-"+minutes+"-"+seconds+"-"
zippedFile.prepend(timeString)
end
`tar -zcvf #{zippedFile} #{newFileName}`
`rm -rf #{directory}`
end
end
end
#!/bin/ruby
fileManager = FileManager::FileZip.new()
fileManager.trimHoursFromLogFiles()
fileManager.getIndeciesOfMatchingFiles()
fileManager.placeFilesInDirectory()