-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltomanifest
executable file
·181 lines (162 loc) · 4.66 KB
/
ltomanifest
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
#!/usr/bin/env ruby
require 'bagit'
require 'yaml'
require 'optparse'
require 'pathname'
option = {}
OptionParser.new do |opts|
opts.banner = "Usage: ltomanifest.rb [option] [inputfile]"
opts.on("-m", "--make", "Make manifest") do |e|
option = 'make'
end
opts.on("-c", "--confirm", "Confirm manifest") do |d|
option = 'confirm'
end
opts.on("-b", "--bagdump=val", "Bag dump", String) { |val| $bagdump = val }
opts.on("-h", "--help", "Help") do
puts opts
exit
end
if ARGV.empty?
puts opts
end
end.parse!
input=ARGV
TargetBags = Array.new
# Methods for colored text output
def red(input)
puts "\e[31m#{input}\e[0m"
end
def green(input)
puts "\e[36m#{input}\e[0m"
end
# Parse Bag Dump if selected
if ! $bagdump.nil?
bagdumppath = Pathname.new($bagdump)
bagdumpcontents = YAML::load_file(bagdumppath)
@confirmed_dumpbags = bagdumpcontents['ConfirmedBags']
end
def Create_manifest(input)
#Check and limit input
if input.length > 1
red("Please only use one directory as input. Exiting.")
exit
else
input = input[0]
end
if ! File.directory?(input)
red("Input is not a valid directory. Exiting")
exit
elsif File.exist?("#{input}/tapemanifest.txt")
red("#{input}/tapemanifest.txt already exists. Exiting.")
exit
end
#Get list of directories (skip dump list bags if present)
Dir.chdir(input)
if ! @confirmed_dumpbags.nil?
green("Will skip previously confirmed bags listed in BagListDump.txt")
bag_list = Dir.glob('*') - @confirmed_dumpbags
@confirmed_dumpbags.each do |isvalidbag|
TargetBags << isvalidbag
end
else
bag_list = Dir.glob('*')
end
if bag_list.include? 'BagListDump.txt'
red("BagListDump.txt detected in target directory. Please move this outside of target and try again! Exiting.")
end
#Check if supposed bags are actually directories
bag_list.each do |isdirectory|
if ! File.directory?(isdirectory)
red("Warning! Files not contained in bags found at -- #{isdirectory} -- Exiting.")
exit
end
end
#Check if directories are bags (contains metadata files)
bag_list.each do |isbag|
if ! File.exist?("#{isbag}/bag-info.txt") || ! File.exist?("#{isbag}/bagit.txt")
red("Warning! Unbagged directory found at -- #{isbag} Exiting.")
exit
end
end
#Verify all bags are valid bags
bag_list.each do |isvalidbag|
bag = BagIt::Bag.new isvalidbag
# Check files by name first
if ! bag.valid_oxum?
red("Warning! Manifest contents do not match actual bag contents in -- #{isvalidbag} Exiting.")
exit
end
#Check files by checksums
if bag.valid?
TargetBags << isvalidbag
green("Confirmed bag: #{isvalidbag}")
else
red("Warning! Invalid Bag Detected at -- #{isvalidbag} -- Dumping List of Validated Bags and Exiting!")
data = {"ConfirmedBags" => TargetBags}
File.write('BagListDump.txt',data.to_yaml)
exit
end
end
targetBagsSorted = TargetBags.sort
bagcontents = Array.new
#Gather checksums from individual bags
targetBagsSorted.each do |bagparse|
metafile = "#{bagparse}/manifest-md5.txt"
contents = File.readlines(metafile)
parsedcontents = {"Bag Name" => bagparse, "Bag Contents" => contents}
bagcontents << parsedcontents
end
#Write manifest of bags and checksums
data = {"Bag List" => targetBagsSorted, "Contents" => bagcontents}
File.write('tapemanifest.txt',data.to_yaml)
green("Manifest written at #{input}/tapemanifest.txt")
end
def Auditmanifest(input)
#Confirm input
if input.length > 1
red("Please only use one manifest file as input. Exiting.")
exit
else
input = input[0]
end
if ! File.exist?(input)
red("Please use a valid input file")
exit
end
#Read manifest file
manifestlocation = File.dirname(input)
manifestinfo = YAML::load_file(input) || red(error)
bags = manifestinfo['Bag List']
Dir.chdir(manifestlocation)
#Confirm validity of all bags listed in manifest file
confirmedBags = Array.new
problemBags = Array.new
if bags.empty?
red 'No Bag Information Found. Please Confirm Manifest'
end
bags.each do |isvalidbag|
bag = BagIt::Bag.new isvalidbag
if bag.valid?
green("Contents Confirmed: #{isvalidbag}")
confirmedBags << isvalidbag
else
puts "Warning: Invalid bag found at -- #{isvalidbag}"
problemBags << isvalidbag
end
end
#List warning of problem bags
if problemBags.length > 0
red("These Bags Failed Verification")
red(problemBags)
else
green("All Bags Verified Successfully")
end
end
if option == 'make'
Create_manifest(input)
elsif option == 'confirm'
Auditmanifest(input)
else
puts "You must use an option"
end