Skip to content

Commit 840fc24

Browse files
authored
Merge pull request #2 from pugetsoundandvision/enhance-bag-verification
Enhance bag verification
2 parents 7a97628 + 2f7ef43 commit 840fc24

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

ltomanifest

+36-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'bagit'
44
require 'yaml'
55
require 'optparse'
6+
require 'pathname'
67

78
option = {}
89

@@ -15,10 +16,13 @@ OptionParser.new do |opts|
1516
opts.on("-c", "--confirm", "Confirm manifest") do |d|
1617
option = 'confirm'
1718
end
19+
opts.on("-b", "--bagdump=val", "Bag dump", String) { |val| $bagdump = val }
20+
1821
opts.on("-h", "--help", "Help") do
1922
puts opts
2023
exit
2124
end
25+
2226
if ARGV.empty?
2327
puts opts
2428
end
@@ -36,6 +40,13 @@ def green(input)
3640
puts "\e[36m#{input}\e[0m"
3741
end
3842

43+
# Parse Bag Dump if selected
44+
if ! $bagdump.nil?
45+
bagdumppath = Pathname.new($bagdump)
46+
bagdumpcontents = YAML::load_file(bagdumppath)
47+
@confirmed_dumpbags = bagdumpcontents['ConfirmedBags']
48+
end
49+
3950
def Create_manifest(input)
4051
#Check and limit input
4152
if input.length > 1
@@ -52,9 +63,21 @@ def Create_manifest(input)
5263
red("#{input}/tapemanifest.txt already exists. Exiting.")
5364
exit
5465
end
55-
#Get list of directories
66+
#Get list of directories (skip dump list bags if present)
5667
Dir.chdir(input)
57-
bag_list = Dir.glob('*')
68+
if ! @confirmed_dumpbags.nil?
69+
green("Will skip previously confirmed bags listed in BagListDump.txt")
70+
bag_list = Dir.glob('*') - @confirmed_dumpbags
71+
@confirmed_dumpbags.each do |isvalidbag|
72+
TargetBags << isvalidbag
73+
end
74+
else
75+
bag_list = Dir.glob('*')
76+
end
77+
if bag_list.include? 'BagListDump.txt'
78+
red("BagListDump.txt detected in target directory. Please move this outside of target and try again! Exiting.")
79+
end
80+
5881
#Check if supposed bags are actually directories
5982
bag_list.each do |isdirectory|
6083
if ! File.directory?(isdirectory)
@@ -67,12 +90,19 @@ def Create_manifest(input)
6790
bag_list.each do |isbag|
6891
if ! File.exist?("#{isbag}/bag-info.txt") || ! File.exist?("#{isbag}/bagit.txt")
6992
red("Warning! Unbagged directory found at -- #{isbag} Exiting.")
93+
exit
7094
end
7195
end
7296

7397
#Verify all bags are valid bags
7498
bag_list.each do |isvalidbag|
7599
bag = BagIt::Bag.new isvalidbag
100+
# Check files by name first
101+
if ! bag.valid_oxum?
102+
red("Warning! Manifest contents do not match actual bag contents in -- #{isvalidbag} Exiting.")
103+
exit
104+
end
105+
#Check files by checksums
76106
if bag.valid?
77107
TargetBags << isvalidbag
78108
green("Confirmed bag: #{isvalidbag}")
@@ -89,20 +119,21 @@ def Create_manifest(input)
89119
targetBagsSorted.each do |bagparse|
90120
metafile = "#{bagparse}/manifest-md5.txt"
91121
contents = File.readlines(metafile)
92-
bagcontents << bagparse
93-
bagcontents << contents
122+
parsedcontents = {"Bag Name" => bagparse, "Bag Contents" => contents}
123+
bagcontents << parsedcontents
94124
end
95125

96126
#Write manifest of bags and checksums
97127
data = {"Bag List" => targetBagsSorted, "Contents" => bagcontents}
128+
98129
File.write('tapemanifest.txt',data.to_yaml)
99130
green("Manifest written at #{input}/tapemanifest.txt")
100131
end
101132

102133
def Auditmanifest(input)
103134
#Confirm input
104135
if input.length > 1
105-
red("Please only use one maifest file as input. Exiting.")
136+
red("Please only use one manifest file as input. Exiting.")
106137
exit
107138
else
108139
input = input[0]

0 commit comments

Comments
 (0)