3
3
require 'bagit'
4
4
require 'yaml'
5
5
require 'optparse'
6
+ require 'pathname'
6
7
7
8
option = { }
8
9
@@ -15,10 +16,13 @@ OptionParser.new do |opts|
15
16
opts . on ( "-c" , "--confirm" , "Confirm manifest" ) do |d |
16
17
option = 'confirm'
17
18
end
19
+ opts . on ( "-b" , "--bagdump=val" , "Bag dump" , String ) { |val | $bagdump = val }
20
+
18
21
opts . on ( "-h" , "--help" , "Help" ) do
19
22
puts opts
20
23
exit
21
24
end
25
+
22
26
if ARGV . empty?
23
27
puts opts
24
28
end
@@ -36,6 +40,13 @@ def green(input)
36
40
puts "\e [36m#{ input } \e [0m"
37
41
end
38
42
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
+
39
50
def Create_manifest ( input )
40
51
#Check and limit input
41
52
if input . length > 1
@@ -52,9 +63,21 @@ def Create_manifest(input)
52
63
red ( "#{ input } /tapemanifest.txt already exists. Exiting." )
53
64
exit
54
65
end
55
- #Get list of directories
66
+ #Get list of directories (skip dump list bags if present)
56
67
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
+
58
81
#Check if supposed bags are actually directories
59
82
bag_list . each do |isdirectory |
60
83
if ! File . directory? ( isdirectory )
@@ -67,12 +90,19 @@ def Create_manifest(input)
67
90
bag_list . each do |isbag |
68
91
if ! File . exist? ( "#{ isbag } /bag-info.txt" ) || ! File . exist? ( "#{ isbag } /bagit.txt" )
69
92
red ( "Warning! Unbagged directory found at -- #{ isbag } Exiting." )
93
+ exit
70
94
end
71
95
end
72
96
73
97
#Verify all bags are valid bags
74
98
bag_list . each do |isvalidbag |
75
99
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
76
106
if bag . valid?
77
107
TargetBags << isvalidbag
78
108
green ( "Confirmed bag: #{ isvalidbag } " )
@@ -89,20 +119,21 @@ def Create_manifest(input)
89
119
targetBagsSorted . each do |bagparse |
90
120
metafile = "#{ bagparse } /manifest-md5.txt"
91
121
contents = File . readlines ( metafile )
92
- bagcontents << bagparse
93
- bagcontents << contents
122
+ parsedcontents = { "Bag Name" => bagparse , "Bag Contents" => contents }
123
+ bagcontents << parsedcontents
94
124
end
95
125
96
126
#Write manifest of bags and checksums
97
127
data = { "Bag List" => targetBagsSorted , "Contents" => bagcontents }
128
+
98
129
File . write ( 'tapemanifest.txt' , data . to_yaml )
99
130
green ( "Manifest written at #{ input } /tapemanifest.txt" )
100
131
end
101
132
102
133
def Auditmanifest ( input )
103
134
#Confirm input
104
135
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." )
106
137
exit
107
138
else
108
139
input = input [ 0 ]
0 commit comments