-
Notifications
You must be signed in to change notification settings - Fork 103
/
parse_mets_to_db.py
executable file
·457 lines (409 loc) · 21 KB
/
parse_mets_to_db.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#!/usr/bin/env python2
from __future__ import print_function
import argparse
import datetime
from lxml import etree
import sys
import os
import uuid
import django
django.setup()
# dashboard
from main import models
from fpr import models as fpr_models
# archivematicaCommon
import namespaces as ns
import fileOperations
import databaseFunctions
MD_TYPE_SIP_ID = "3e48343d-e2d2-4956-aaa3-b54d26eb9761"
def parse_format_version(element):
"""
Parses the FPR FormatVersion for the file.
Element can be the amdSec, or a PREMIS:OBJECT
:param element: lxml Element that contains premis:format.
:return: FormatVersion object or None
"""
format_version = None
try:
# Looks for PRONOM ID first
if element.findtext('.//premis:formatRegistryName', namespaces=ns.NSMAP) == 'PRONOM':
puid = element.findtext('.//premis:formatRegistryKey', namespaces=ns.NSMAP)
print('PUID', puid)
format_version = fpr_models.FormatVersion.active.get(pronom_id=puid)
elif element.findtext('.//premis:formatRegistryName', namespaces=ns.NSMAP) == 'Archivematica Format Policy Registry':
key = element.findtext('.//premis:formatRegistryKey', namespaces=ns.NSMAP)
print('FPR key', key)
format_version = fpr_models.IDRule.active.get(command_output=key).format
except fpr_models.FormatVersion.DoesNotExist:
pass
return format_version
def parse_files(root):
filesec = root.find('.//mets:fileSec', namespaces=ns.NSMAP)
files = []
for fe in filesec.findall('.//mets:file', namespaces=ns.NSMAP):
filegrpuse = fe.getparent().get('USE')
print('filegrpuse', filegrpuse)
amdid = fe.get('ADMID')
print('amdid', amdid)
current_techmd = root.xpath('mets:amdSec[@ID="' + amdid + '"]/mets:techMD[not(@STATUS="superseded")]', namespaces=ns.NSMAP)[0]
file_uuid = current_techmd.findtext('.//premis:objectIdentifierValue', namespaces=ns.NSMAP)
print('file_uuid', file_uuid)
original_path = current_techmd.findtext('.//premis:originalName', namespaces=ns.NSMAP)
original_path = original_path.replace('%transferDirectory%', '%SIPDirectory%')
print('original_path', original_path)
current_path = fe.find('mets:FLocat', namespaces=ns.NSMAP).get(ns.xlinkBNS+'href')
current_path = '%SIPDirectory%' + current_path
print('current_path', current_path)
checksum = current_techmd.findtext('.//premis:messageDigest', namespaces=ns.NSMAP)
print('checksum', checksum)
checksumtype = current_techmd.findtext('.//premis:messageDigestAlgorithm', namespaces=ns.NSMAP)
print('checksumtype', checksumtype)
size = current_techmd.findtext('.//premis:size', namespaces=ns.NSMAP)
print('size', size)
# FormatVersion
format_version = parse_format_version(current_techmd)
print('format_version', format_version)
# Derivation
derivation = derivation_event = None
event = current_techmd.findtext('.//premis:relatedEventIdentifierValue', namespaces=ns.NSMAP)
print('derivation event', event)
related_uuid = current_techmd.findtext('.//premis:relatedObjectIdentifierValue', namespaces=ns.NSMAP)
print('related_uuid', related_uuid)
rel = current_techmd.findtext('.//premis:relationshipSubType', namespaces=ns.NSMAP)
print('relationship', rel)
if rel == 'is source of':
derivation = related_uuid
derivation_event = event
file_info = {
'uuid': file_uuid,
'original_path': original_path,
'current_path': current_path,
'use': filegrpuse,
'checksum': checksum,
'checksumtype': checksumtype,
'size': size,
'format_version': format_version,
'derivation': derivation,
'derivation_event': derivation_event,
}
files.append(file_info)
print()
return files
def update_files(sip_uuid, files):
"""
Update file information to DB.
:param sip_uuid: UUID of the SIP to parse the metadata for.
:param files: List of dicts containing file info.
"""
now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
# Add information to the DB
for file_info in files:
# Add file & reingest event
event_id = str(uuid.uuid4())
fileOperations.addFileToSIP(
filePathRelativeToSIP=file_info['original_path'],
fileUUID=file_info['uuid'],
sipUUID=sip_uuid,
taskUUID=event_id,
date=now,
sourceType="reingestion",
use=file_info['use'],
)
# Update other file info
# This doesn't use updateSizeAndChecksum because it also updates currentlocation
models.File.objects.filter(uuid=file_info['uuid']).update(
checksum=file_info['checksum'],
checksumtype=file_info['checksumtype'],
size=file_info['size'],
currentlocation=file_info['current_path']
)
if file_info['format_version']:
# Add Format ID
models.FileFormatVersion.objects.create(
file_uuid_id=file_info['uuid'],
format_version=file_info['format_version']
)
# Derivation info
# Has to be separate loop, as derived file may not be in DB otherwise
# May not need to be parsed, if Derivation info can be roundtripped in METS Reader/Writer
for file_info in files:
if file_info['derivation'] is None:
continue
databaseFunctions.insertIntoDerivations(
sourceFileUUID=file_info['uuid'],
derivedFileUUID=file_info['derivation'],
)
def parse_dc(sip_uuid, root):
"""
Parse SIP-level DublinCore metadata into the DublinCore table.
Deletes existing entries associated with this SIP.
:param str sip_uuid: UUID of the SIP to parse the metadata for.
:param root: root Element of the METS file.
:return: DublinCore DB object, or None
"""
# Delete existing DC
models.DublinCore.objects.filter(metadataappliestoidentifier=sip_uuid, metadataappliestotype_id=MD_TYPE_SIP_ID).delete()
# Parse DC
dmds = root.xpath('mets:dmdSec/mets:mdWrap[@MDTYPE="DC"]/parent::*', namespaces=ns.NSMAP)
dc_model = None
# Find which DC to parse into DB
if len(dmds) > 0:
DC_TERMS_MATCHING = {
'title': 'title',
'creator': 'creator',
'subject': 'subject',
'description': 'description',
'publisher': 'publisher',
'contributor': 'contributor',
'date': 'date',
'type': 'type',
'format': 'format',
'identifier': 'identifier',
'source': 'source',
'relation': 'relation',
'language': 'language',
'coverage': 'coverage',
'rights': 'rights',
'isPartOf': 'is_part_of',
}
# Want most recently updated
dmds = sorted(dmds, key=lambda e: e.get('CREATED'))
# Only want SIP DC, not file DC
div = root.find('mets:structMap/mets:div/mets:div[@TYPE="Directory"][@LABEL="objects"]', namespaces=ns.NSMAP)
dmdids = div.get('DMDID')
# No SIP DC
if dmdids is None:
return
dmdids = dmdids.split()
for dmd in dmds[::-1]: # Reversed
if dmd.get('ID') in dmdids:
dc_xml = dmd.find('mets:mdWrap/mets:xmlData/dcterms:dublincore', namespaces=ns.NSMAP)
break
dc_model = models.DublinCore(
metadataappliestoidentifier=sip_uuid,
metadataappliestotype_id=MD_TYPE_SIP_ID,
status=models.METADATA_STATUS_REINGEST,
)
print('Dublin Core:')
for elem in dc_xml:
tag = elem.tag.replace(ns.dctermsBNS, '', 1).replace(ns.dcBNS, '', 1)
print(tag, elem.text)
setattr(dc_model, DC_TERMS_MATCHING[tag], elem.text)
dc_model.save()
return dc_model
def parse_rights(sip_uuid, root):
"""
Parse PREMIS:RIGHTS metadata into the database.
Deletes existing entries associated with this SIP.
:param str sip_uuid: UUID of the SIP to parse the metadata for.
:param root: root Element of the METS file.
:return: List of RightsStatement objects for the parsed entries. Maybe be empty
"""
# Delete existing PREMIS Rights
del_rights = models.RightsStatement.objects.filter(metadataappliestoidentifier=sip_uuid, metadataappliestotype_id=MD_TYPE_SIP_ID)
# TODO delete all the other rights things?
models.RightsStatementCopyright.objects.filter(rightsstatement__in=del_rights).delete()
models.RightsStatementLicense.objects.filter(rightsstatement__in=del_rights).delete()
models.RightsStatementStatuteInformation.objects.filter(rightsstatement__in=del_rights).delete()
models.RightsStatementOtherRightsInformation.objects.filter(rightsstatement__in=del_rights).delete()
models.RightsStatementRightsGranted.objects.filter(rightsstatement__in=del_rights).delete()
del_rights.delete()
parsed_rights = []
amds = root.xpath('mets:amdSec/mets:rightsMD/parent::*', namespaces=ns.NSMAP)
if amds:
amd = amds[0]
# Get rightsMDs
# METS from original AIPs will not have @STATUS, and reingested AIPs will have only one @STATUS that is 'current'
rights_stmts = amd.xpath('mets:rightsMD[not(@STATUS) or @STATUS="current"]/mets:mdWrap[@MDTYPE="PREMIS:RIGHTS"]/*/premis:rightsStatement', namespaces=ns.NSMAP)
# Parse to DB
for statement in rights_stmts:
rights_basis = statement.findtext('premis:rightsBasis', namespaces=ns.NSMAP)
print('rights_basis', rights_basis)
# Don't parse identifier type/value so if it's modified the new one gets unique identifiers
rights = models.RightsStatement.objects.create(
metadataappliestotype_id=MD_TYPE_SIP_ID,
metadataappliestoidentifier=sip_uuid,
rightsstatementidentifiertype="",
rightsstatementidentifiervalue="",
rightsbasis=rights_basis,
status=models.METADATA_STATUS_REINGEST,
)
if rights_basis == 'Copyright':
status = statement.findtext('.//premis:copyrightStatus', namespaces=ns.NSMAP) or ""
jurisdiction = statement.findtext('.//premis:copyrightJurisdiction', namespaces=ns.NSMAP) or ""
det_date = statement.findtext('.//premis:copyrightStatusDeterminationDate', namespaces=ns.NSMAP) or ""
start_date = statement.findtext('.//premis:copyrightApplicableDates/premis:startDate', namespaces=ns.NSMAP) or ""
end_date = statement.findtext('.//premis:copyrightApplicableDates/premis:endDate', namespaces=ns.NSMAP) or ""
end_open = False
if end_date == 'OPEN':
end_open = True
end_date = None
cr = models.RightsStatementCopyright.objects.create(
rightsstatement=rights,
copyrightstatus=status,
copyrightjurisdiction=jurisdiction,
copyrightstatusdeterminationdate=det_date,
copyrightapplicablestartdate=start_date,
copyrightapplicableenddate=end_date,
copyrightenddateopen=end_open,
)
id_type = statement.findtext('.//premis:copyrightDocumentationIdentifierType', namespaces=ns.NSMAP) or ""
id_value = statement.findtext('.//premis:copyrightDocumentationIdentifierValue', namespaces=ns.NSMAP) or ""
id_role = statement.findtext('.//premis:copyrightDocumentationRole', namespaces=ns.NSMAP) or ""
models.RightsStatementCopyrightDocumentationIdentifier.objects.create(
rightscopyright=cr,
copyrightdocumentationidentifiertype=id_type,
copyrightdocumentationidentifiervalue=id_value,
copyrightdocumentationidentifierrole=id_role,
)
note = statement.findtext('.//premis:copyrightNote', namespaces=ns.NSMAP) or ""
models.RightsStatementCopyrightNote.objects.create(
rightscopyright=cr,
copyrightnote=note,
)
elif rights_basis == 'License':
terms = statement.findtext('.//premis:licenseTerms', namespaces=ns.NSMAP) or ""
start_date = statement.findtext('.//premis:licenseApplicableDates/premis:startDate', namespaces=ns.NSMAP) or ""
end_date = statement.findtext('.//premis:licenseApplicableDates/premis:endDate', namespaces=ns.NSMAP) or ""
end_open = False
if end_date == 'OPEN':
end_open = True
end_date = None
li = models.RightsStatementLicense.objects.create(
rightsstatement=rights,
licenseterms=terms,
licenseapplicablestartdate=start_date,
licenseapplicableenddate=end_date,
licenseenddateopen=end_open,
)
id_type = statement.findtext('.//premis:licenseDocumentationIdentifierType', namespaces=ns.NSMAP) or ""
id_value = statement.findtext('.//premis:licenseDocumentationIdentifierValue', namespaces=ns.NSMAP) or ""
id_role = statement.findtext('.//premis:licenseDocumentationRole', namespaces=ns.NSMAP) or ""
models.RightsStatementLicenseDocumentationIdentifier.objects.create(
rightsstatementlicense=li,
licensedocumentationidentifiertype=id_type,
licensedocumentationidentifiervalue=id_value,
licensedocumentationidentifierrole=id_role,
)
note = statement.findtext('.//premis:licenseNote', namespaces=ns.NSMAP) or ""
models.RightsStatementLicenseNote.objects.create(
rightsstatementlicense=li,
licensenote=note,
)
elif rights_basis == 'Statute':
jurisdiction = statement.findtext('.//premis:statuteJurisdiction', namespaces=ns.NSMAP) or ""
citation = statement.findtext('.//premis:statuteCitation', namespaces=ns.NSMAP) or ""
det_date = statement.findtext('.//premis:statuteInformationDeterminationDate', namespaces=ns.NSMAP) or ""
start_date = statement.findtext('.//premis:statuteApplicableDates/premis:startDate', namespaces=ns.NSMAP) or ""
end_date = statement.findtext('.//premis:statuteApplicableDates/premis:endDate', namespaces=ns.NSMAP) or ""
end_open = False
if end_date == 'OPEN':
end_open = True
end_date = None
st = models.RightsStatementStatuteInformation.objects.create(
rightsstatement=rights,
statutejurisdiction=jurisdiction,
statutecitation=citation,
statutedeterminationdate=det_date,
statuteapplicablestartdate=start_date,
statuteapplicableenddate=end_date,
statuteenddateopen=end_open,
)
id_type = statement.findtext('.//premis:statuteDocumentationIdentifierType', namespaces=ns.NSMAP) or ""
id_value = statement.findtext('.//premis:statuteDocumentationIdentifierValue', namespaces=ns.NSMAP) or ""
id_role = statement.findtext('.//premis:statuteDocumentationRole', namespaces=ns.NSMAP) or ""
models.RightsStatementStatuteDocumentationIdentifier.objects.create(
rightsstatementstatute=st,
statutedocumentationidentifiertype=id_type,
statutedocumentationidentifiervalue=id_value,
statutedocumentationidentifierrole=id_role,
)
note = statement.findtext('.//premis:statuteNote', namespaces=ns.NSMAP) or ""
models.RightsStatementStatuteInformationNote.objects.create(
rightsstatementstatute=st,
statutenote=note,
)
elif rights_basis in ('Donor', 'Policy', 'Other'):
other_basis = statement.findtext('.//premis:otherRightsBasis', namespaces=ns.NSMAP) or "Other"
rights.rightsbasis = other_basis
rights.save()
start_date = statement.findtext('.//premis:otherRightsApplicableDates/premis:startDate', namespaces=ns.NSMAP) or ""
end_date = statement.findtext('.//premis:otherRightsApplicableDates/premis:endDate', namespaces=ns.NSMAP) or ""
end_open = False
if end_date == 'OPEN':
end_open = True
end_date = None
ot = models.RightsStatementOtherRightsInformation.objects.create(
rightsstatement=rights,
otherrightsbasis=other_basis,
otherrightsapplicablestartdate=start_date,
otherrightsapplicableenddate=end_date,
otherrightsenddateopen=end_open,
)
id_type = statement.findtext('.//premis:otherRightsDocumentationIdentifierType', namespaces=ns.NSMAP) or ""
id_value = statement.findtext('.//premis:otherRightsDocumentationIdentifierValue', namespaces=ns.NSMAP) or ""
id_role = statement.findtext('.//premis:otherRightsDocumentationRole', namespaces=ns.NSMAP) or ""
models.RightsStatementOtherRightsDocumentationIdentifier.objects.create(
rightsstatementotherrights=ot,
otherrightsdocumentationidentifiertype=id_type,
otherrightsdocumentationidentifiervalue=id_value,
otherrightsdocumentationidentifierrole=id_role,
)
note = statement.findtext('.//premis:otherRightsNote', namespaces=ns.NSMAP) or ""
models.RightsStatementOtherRightsInformationNote.objects.create(
rightsstatementotherrights=ot,
otherrightsnote=note,
)
# Parse rightsGranted
for rightsgranted_elem in statement.findall('.//premis:rightsGranted', namespaces=ns.NSMAP):
rights_act = rightsgranted_elem.findtext('premis:act', namespaces=ns.NSMAP) or ""
rights_start_date = rightsgranted_elem.findtext('.//premis:startDate', namespaces=ns.NSMAP) or ""
rights_end_date = rightsgranted_elem.findtext('.//premis:endDate', namespaces=ns.NSMAP) or ""
rights_end_open = False
if rights_end_date == 'OPEN':
rights_end_date = None
rights_end_open = True
print('rights_act', rights_act)
print('rights_start_date', rights_start_date)
print('rights_end_date', rights_end_date)
print('rights_end_open', rights_end_open)
rights_granted = models.RightsStatementRightsGranted.objects.create(
rightsstatement=rights,
act=rights_act,
startdate=rights_start_date,
enddate=rights_end_date,
enddateopen=rights_end_open,
)
rights_note = rightsgranted_elem.findtext('premis:rightsGrantedNote', namespaces=ns.NSMAP) or ""
print('rights_note', rights_note)
models.RightsStatementRightsGrantedNote.objects.create(
rightsgranted=rights_granted,
rightsgrantednote=rights_note,
)
rights_restriction = rightsgranted_elem.findtext('premis:restriction', namespaces=ns.NSMAP) or ""
print('rights_restriction', rights_restriction)
models.RightsStatementRightsGrantedRestriction.objects.create(
rightsgranted=rights_granted,
restriction=rights_restriction,
)
parsed_rights.append(rights)
return parsed_rights
def main(sip_uuid, sip_path):
# Set reingest type
# TODO also support AIC-REIN
sip = models.SIP.objects.filter(uuid=sip_uuid)
sip.update(sip_type='AIP-REIN')
# Parse METS to extract information needed by later microservices
mets_path = os.path.join(sip_path, 'metadata', 'submissionDocumentation', 'METS.' + sip_uuid + '.xml')
root = etree.parse(mets_path)
files = parse_files(root)
update_files(sip_uuid, files)
parse_dc(sip_uuid, root)
parse_rights(sip_uuid, root)
if __name__ == '__main__':
print('METS Reader')
parser = argparse.ArgumentParser()
parser.add_argument('sip_uuid', help='%SIPUUID%')
parser.add_argument('sip_path', help='%SIPDirectory%')
args = parser.parse_args()
sys.exit(main(args.sip_uuid, args.sip_path))