forked from kieranjol/IFIscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcpfixity.py
345 lines (282 loc) · 13.3 KB
/
dcpfixity.py
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from lxml import etree
import sys
import pdb
from glob import glob
import csv
import os
from os import listdir
from os.path import isfile, join
import subprocess
import base64
import time
import re
import smtplib
import argparse
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.message import Message
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import bagit
parser = argparse.ArgumentParser(description='DCP FIXITY checker/bagging tool.'
' Written by Kieran O\'Leary.')
parser.add_argument('input')
parser.add_argument(
'-bag',
action='store_true',help='bag the dcp_dir if it passes the hash check')
parser.add_argument(
'-csv',
action='store_true',help='File level csv is stored as sidecar to dcp directory')
parser.add_argument(
'-m',
action='store_true',help='send email report')
args = parser.parse_args()
if args.bag:
bagging = 'enabled'
else:
bagging = 'disabled'
if args.m:
email = 'enabled'
else:
email = 'disabled'
#bagrm = os.path.abspath('bag-rm.py')
#bagit = os.path.abspath('bagit.py')
#print bagrm
dcp_dir = args.input
# Two csv functions. One to create a csv, the other to add info to.
def create_csv(csv_file, *args):
f = open(csv_file, 'wb')
try:
writer = csv.writer(f)
writer.writerow(*args)
finally:
f.close()
def append_csv(csv_file, *args):
f = open(csv_file, 'ab')
try:
writer = csv.writer(f)
writer.writerow(*args)
finally:
f.close()
# Create a new .csv file with headings.
# CSV filename will be DCp directory name + time/date.
# CSV will be saved to your Desktop.
csv_report_filename = os.path.basename(dcp_dir) + '_dcp_level' + time.strftime("_%Y_%m_%dT%H_%M_%S")
# CSV will be saved to your Desktop.
csv_report = os.path.expanduser("~/Desktop/%s.csv") % csv_report_filename
create_csv(csv_report, ('DCP NAME', 'DIRECTORY NAME', 'JUDGEMENT'))
if args.csv:
csv_setting = 'enabled'
else:
csv_setting = 'disabled'
csv_filename = os.path.basename(dcp_dir) + '_file_level' + time.strftime("_%Y_%m_%dT%H_%M_%S")
csvfile = os.path.expanduser("~/Desktop/%s.csv") % csv_filename
#csv_file = os.path.expanduser("~/Desktop/%s.csv") % csv_filename
create_csv(csvfile, ('MXF HASH', 'STORED HASH', 'FILENAME', 'JUDGEMENT'))
for root,dirnames,filenames in os.walk(dcp_dir):
if ("ASSETMAP.xml" in filenames) or ("ASSETMAP" in filenames) :
dir = root
#print os.path.basename(os.path.dirname(root))
filenoext = os.path.splitext(os.path.dirname(root))[0]
#print filenoext + 'dfsdfjkljoewuiljkdfs'
# Change directory to directory with video files
if csv_setting == 'enabled':
#print filenoext
# Generate new directory names in AIP
fixity_dir = "%s/fixity" % filenoext
os.makedirs(fixity_dir)
csvfile = fixity_dir + '/' + os.path.basename(os.path.dirname(root)) + '_item_level' + time.strftime("_%Y_%m_%dT%H_%M_%S") + '.csv'
#csv_file = os.path.expanduser("~/Desktop/%s.csv") % csv_filename
create_csv(csvfile, ('MXF HASH', 'STORED HASH', 'FILENAME', 'JUDGEMENT'))
# Changing directory makes globbing easier (from my experience anyhow).
os.chdir(dir)
# Scan the main DCP directory for an assetmap.
dcp_files = [f for f in listdir(dir) if isfile(join(dir, f))]
if 'ASSETMAP' in dcp_files:
assetmap = 'ASSETMAP'
elif 'ASSETMAP.xml' in dcp_files:
assetmap = 'ASSETMAP.xml'
# Parse the assetmap in order to find the namespace.
try:
assetmap_xml = etree.parse(assetmap)
except SyntaxError:
append_csv(csvfile,('NOT A VALID ASSETMAP', 'NOT A VALID ASSETMAP', dir,'NOT A VALID ASSETMAP'))
append_csv(csv_report,(os.path.basename(dir), dir, 'NOT A VALID ASSETMAP'))
print 'not an assetmap!!!!'
continue
assetmap_namespace = assetmap_xml.xpath('namespace-uri(.)')
# Get a list of all XML files in the main DCP directory.
xmlfiles = glob('*.xml')
# Generate an empty list as there may be multiple PKLs.
pkl_list = []
# Loop through xmlfiles in order to find any PKL files.
for i in xmlfiles:
try:
xmlname = etree.parse(i)
except SyntaxError:
append_csv(csvfile,('NOT A VALID PKL', 'NOT A VALID PKL', dir,'NOT A VALID PKL'))
append_csv(csv_report,(os.path.basename(dir), dir, 'NOT A VALID PKL'))
print 'not a valid PKL!!!!'
continue
except KeyError:
append_csv(csvfile,('PKL APPEARS TO BE MISSING', 'PKL APPEARS TO BE MISSING', dir,'PKL APPEARS TO BE MISSING'))
append_csv(csv_report,(os.path.basename(dir), dir, 'PKL APPEARS TO BE MISSING'))
print 'Missing PKL!!!!'
continue
is_pkl = xmlname.xpath('namespace-uri(.)')
if 'PKL' in is_pkl:
pkl_list.append(i)
if len(pkl_list) == 0:
append_csv(csvfile,('PKL APPEARS TO BE MISSING', 'PKL APPEARS TO BE MISSING', dir,'PKL APPEARS TO BE MISSING'))
append_csv(csv_report,(os.path.basename(dir), dir, 'PKL APPEARS TO BE MISSING'))
continue
# Generate an empty dictionary that will link the PKL hashes to each UUID.
pkl_hashes = {}
# Loop through the PKLs and link each hash to a UUID.
for i in pkl_list:
pkl_parse = etree.parse(i)
pkl_namespace = pkl_parse.xpath('namespace-uri(.)')
hashes = pkl_parse.findall('//ns:Hash',namespaces={'ns': pkl_namespace})
xmluuid = pkl_parse.findall('//ns:Asset/ns:Id',namespaces={'ns': pkl_namespace})
counter = 0
while counter <= len(hashes) -1 : # The -1 is there because of lxml's zero indexing.
pkl_hashes[xmluuid[counter].text] = hashes[counter].text # {pkl_uuid:pkl_hash}
counter +=1
# Begin analysis of assetmap xml.
counter = 0
assetmap_paths = assetmap_xml.findall('//ns:Path',namespaces={'ns': assetmap_namespace})
assetmap_uuids = assetmap_xml.findall('//ns:Asset/ns:Id',namespaces={'ns': assetmap_namespace})
#while counter <= len(assetmap_paths) -1 :
counter = 0
file_paths = {}
while counter <= len(assetmap_paths) -1 :
if 'file:///' in assetmap_paths[counter].text:
remove_this = 'file:///'
assetmap_paths[counter].text = assetmap_paths[counter].text.replace(remove_this,"")
elif 'file://' in assetmap_paths[counter].text:
remove_this = 'file://'
assetmap_paths[counter].text = assetmap_paths[counter].text.replace(remove_this,"")
elif 'file:/' in assetmap_paths[counter].text:
remove_this = 'file:/'
assetmap_paths[counter].text = assetmap_paths[counter].text.replace(remove_this,"")
file_paths[assetmap_uuids[counter].text] = [assetmap_paths[counter].text] # {assetmapuuid:assetmapfilename}
counter +=1
# Removes PKLs from list of files to hash, as these files are not in manifest.
keys_to_remove = []
for i in file_paths:
if file_paths[i][0] in pkl_list:
keys_to_remove.append(i)
# PKL files are deleted from the file_paths dictionary.
for i in keys_to_remove:
del file_paths[i]
# Check if there are any files missing from the DCP.
missing_files = []
for i in file_paths:
if not os.path.isfile(file_paths[i][0]): # This checks if the file exists.
print time.strftime("%Y-%m-%dT%H:%M:%S") + ' - **********' + file_paths[i][0] + ' is missing **********'
missing_files.append(i)
# Add missing file info to the csv.
append_csv(csvfile,('MISSING FILE', pkl_hashes[i], os.path.abspath(file_paths[i][0]),'MISSING FILE'))
# This removes the missing files from the hashable list.
for i in missing_files:
del file_paths[i]
del pkl_hashes[i]
# Generate fresh hashes on the actual files in the DCP.
for i in file_paths:
print time.strftime("%Y-%m-%dT%H:%M:%S") + ' - Generating fresh hash for ' + file_paths[i][0]
# Create SHA-1 binary hashes with OPENSSL.
openssl_hash = subprocess.check_output(['openssl',
'sha1',
'-binary',
file_paths[i][0]])
# Encode the hashes as base64.
b64hash = base64.b64encode(openssl_hash)
# Append hashes to the list within the file_paths dictionary.
file_paths[i].append(b64hash)
# Compare the hashes in the PKL manifest to the fresh hashes.
hash_mismatches = []
for i in file_paths:
if file_paths[i][1] == pkl_hashes[i]:
print file_paths[i][0] + ' is ok'
append_csv(csvfile,(file_paths[i][1], pkl_hashes[i], os.path.abspath(file_paths[i][0]),'HASH MATCH'))
else:
print file_paths[i][0] + ' mismatch'
hash_mismatches.append(file_paths[i][0])
append_csv(csvfile,(file_paths[i][1], pkl_hashes[i], os.path.abspath(file_paths[i][0]),'HASH MISMATCH'))
if len(hash_mismatches) > 0:
report = ' but THERE ARE HASH MISMATCHES. SCROLL UP FOR MORE INFO OR CHECK THE CSV'
baggable = 'n'
print 'This DCP will not be bagged as it could not pass a fixity check'
else:
report = ' and all hashes match.'
baggable = 'y'
if len(missing_files) > 0:
print time.strftime("%Y-%m-%dT%H:%M:%S") + ' - WARNING - THERE ARE FILES MISSING FROM THIS DCP. SCROLL UP FOR MORE INFO OR CHECK THE CSV'
append_csv(csv_report,(os.path.dirname(root), dir, 'FILES MISSING - CHECK REPORT'))
print 'This DCP will not be bagged as it could not pass a fixity check'
else:
print time.strftime("%Y-%m-%dT%H:%M:%S") + ' - All files are present in your DCP' + report
append_csv(csv_report,(os.path.dirname(root), dir,'All files present ' + report))
if bagging == 'enabled':
if baggable == 'y':
#pdb.set_trace()
if os.path.dirname(root) != args.input:
dir = os.path.dirname(root)
if args.input == os.path.dirname(dir):
os.chdir(dir)
bag = bagit.make_bag(dir)
else:
print 'bagging not supported for this folder structure right now'
else:
print 'bagging not supported for this folder structure right now'
if email == 'enabled':
emailfrom = ""
emailto = ['', '']
#emailto = ", ".join(emailto)
fileToSend = csv_report
username = ""
password = ""
msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = ", ".join(emailto)
msg["Subject"] = "Hash check complete"
msg.preamble = "testtesttest"
body = MIMEText("example email body")
msg.attach(body)
ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
if maintype == "text":
fp = open(fileToSend)
# Note: we should handle calculating the charset
attachment = MIMEText(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "image":
fp = open(fileToSend, "rb")
attachment = MIMEImage(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "audio":
fp = open(fileToSend, "rb")
attachment = MIMEAudio(fp.read(), _subtype=subtype)
fp.close()
else:
fp = open(fileToSend, "rb")
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=fileToSend)
msg.attach(attachment)
server_ssl = smtplib.SMTP_SSL("smtp.gmail.com", 465)
server_ssl.ehlo() # optional, called by login()
server_ssl.login(username, password)
# ssl server doesn't support or need tls, so don't call server_ssl.starttls()
server_ssl.sendmail(emailfrom, emailto, msg.as_string())
print msg.as_string()
#server_ssl.quit()
server_ssl.close()
print 'successfully sent the mail'