-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreservica_pre_ingest.py
351 lines (340 loc) · 19.6 KB
/
preservica_pre_ingest.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
import os
import shutil
import pathlib
import hashlib
import csv
import time
from datetime import datetime
from pyrsistent import thaw
from zipfile import ZipFile
from os.path import basename
from cleanup_dates import aspace_dates
from openpyxl import load_workbook
mincol = 2
maxcol = 2
minrow = 2
refidcol = 2
aouricol = 3
titlecol = 7
datecol = 12
filecol = 13
container = ''
#this function takes the folder containing all the preservation masters and renames to be the "container" folder which will ultimately be used for OPEX incremental ingest
#also creates a "project_log.txt" file to store variables so that an ingest project can be worked on over multiple sessions
def create_container(window, mline, init_color, summary_color, projpath, work_order):
mline.update('[START] CREATING CONTAINER\n', text_color_for_value=init_color)
window.refresh()
now = datetime.now()
date_time = now.strftime('%Y-%m-%d_%H-%M-%S')
global container
container = 'container_' + date_time
os.mkdir(os.path.join(projpath, container))
workorder = os.path.basename(work_order)
for file in os.listdir(path = projpath):
if file == workorder or file == container:
continue
else:
shutil.move(os.path.join(projpath, file), os.path.join(projpath, container, file))
mline.update('[SUMMARY] Container directory: {} and moved digital assets into it\n\n'.format(container), append=True, text_color_for_value=summary_color)
window.refresh()
def file_hash_list(window, mline, alt_background, init_color, update_color, summary_color, projpath, alg_choice):
mline.update('[START] CREATING LIST OF FILES AND CHECKSUMS\n', append=True, text_color_for_value=init_color)
window.refresh()
path_container = os.path.join(projpath, container)
file_count = 0
with open(os.path.join(projpath, container + '.csv'), mode='w', newline='') as csv_file:
csv_writer = csv.writer(csv_file)
if alg_choice == 'MD5':
csv_writer.writerow(['File Name', 'MD5 Checksum'])
elif alg_choice == 'SHA-1':
csv_writer.writerow(['File Name', 'SHA-1 Checksum'])
for file in os.listdir(path = path_container):
file_hand = open(os.path.join(path_container, file), 'rb')
file_read = file_hand.read()
if alg_choice == 'MD5':
checksum = hashlib.md5(file_read).hexdigest()
elif alg_choice == 'SHA-1':
checksum = hashlib.sha1(file_read).hexdigest()
csv_writer.writerow([file, checksum])
file_count += 1
if (file_count % 2) == 0:
mline.update('[UPDATE] file: {} checksum: {}\n'.format(file, checksum), append=True, text_color_for_value=update_color)
window.refresh()
else:
mline.update('[UPDATE] file: {} checksum: {}\n'.format(file, checksum), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
window.refresh()
mline.update('[SUMMARY] Generated checksums for {} files\n\n'.format(file_count), append=True, text_color_for_value=summary_color)
window.refresh()
#This function creates paths to the folders and files and then moves the files to their respective folders.
def folder_ds_files(window, mline, alt_background, init_color, update_color, summary_color, projpath, filename_delimiter):
mline.update('[START] CREATING FOLDER STRUCTURE FOR PRESERVATION MASTERS\n', append=True, text_color_for_value=init_color)
window.refresh()
folder_count = 0
file_count = 0
loop_count = 0
path_container = os.path.join(projpath, container)
folder_list = list()
for file in os.listdir(path = path_container):
file_root = file.split('.')[0].strip()
if filename_delimiter in file_root:
file_root = file_root.split(filename_delimiter)[0].strip()
if file_root not in folder_list:
folder_list.append(file_root)
loop_count += 1
if (loop_count % 2) == 0:
mline.update('[UPDATE] added {} to folder_list\n'.format(file_root), append=True, text_color_for_value=update_color)
window.refresh()
else:
mline.update('[UPDATE] added {} to folder_list\n'.format(file_root), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
window.refresh()
for file_root in folder_list:
path_folder = os.path.join(path_container, file_root)
os.mkdir(path_folder)
loop_count += 1
if (loop_count % 2) == 0:
mline.update('[UPDATE] created {}\n'.format(path_folder), append=True, text_color_for_value=update_color)
window.refresh()
else:
mline.update('[UPDATE] created {}\n'.format(path_folder), append=True, text_color_for_value=update_color)
window.refresh()
folder_count += 1
for file in os.listdir(path = path_container):
if '.' not in file:
continue
else:
path_file = os.path.join(path_container, file)
file_prefix = file.split('.')[0].strip()
if '-' in file_prefix:
file_prefix = file_prefix.split('-')[0].strip()
path_folder = os.path.join(path_container, file_prefix, file)
shutil.move(path_file, path_folder)
loop_count += 1
if (loop_count % 2) == 0:
mline.update('[UPDATE] moved {} to {}\n'.format(path_file, path_folder), append=True, text_color_for_value=update_color)
window.refresh()
else:
mline.update('[UPDATE] moved {} to {}\n'.format(path_file, path_folder), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
window.refresh()
file_count += 1
mline.update('[SUMMARY] created {} folders\n'.format(folder_count), append=True, text_color_for_value=summary_color)
mline.update('[SUMMARY] moved {} files\n\n'.format(file_count), append=True, text_color_for_value=summary_color)
window.refresh()
#this function begins to create the PAX structure
#putting PDFs in Representation_Access folders and TIFFs in Representation_Preservation folders
def representation_preservation_access(window, mline, alt_background, init_color, update_color, summary_color, projpath, rep_pres, rep_acc):
mline.update('[START] CREATING REPRESENTATION FOLDERS AND MOVING ASSETS INTO THEM\n', append=True, text_color_for_value=init_color)
window.refresh()
folder_count = 0
file_count = 0
loop_count = 0
path_container = os.path.join(projpath, container)
rep_pres_name = 'Representation_Preservation'
rep_acc_name = 'Representation_Access'
for directory in os.listdir(path = path_container):
path_directory = os.path.join(projpath, container, directory)
path_pres = os.path.join(projpath, container, directory, rep_pres_name)
path_acc = os.path.join(projpath, container, directory, rep_acc_name)
os.mkdir(path_pres)
os.mkdir(path_acc)
folder_count += 2
for file in os.listdir(path = path_directory):
path_directoryfile = os.path.join(projpath, container, directory, file)
if file == rep_pres_name or file == rep_acc_name:
continue
if file.endswith(rep_acc):
file_name = file.split('.')[0]
os.mkdir(os.path.join(path_acc, file_name))
shutil.move(path_directoryfile, os.path.join(path_acc, file_name, file))
loop_count += 1
if (loop_count % 2) == 0:
mline.update('[UPDATE] created directory: {}\n'.format(path_acc + '/' + file_name), append=True, text_color_for_value=update_color)
mline.update('[UPDATE] moved file: {}\n'.format(path_acc + '/' + file_name + '/' + file), append=True, text_color_for_value=update_color)
window.refresh()
else:
mline.update('[UPDATE] created directory: {}\n'.format(path_acc + '/' + file_name), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
mline.update('[UPDATE] moved file: {}\n'.format(path_acc + '/' + file_name + '/' + file), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
window.refresh()
file_count += 1
window.refresh()
if file.endswith(rep_pres):
file_name = file.split('.')[0]
os.mkdir(os.path.join(path_pres, file_name))
shutil.move(path_directoryfile, os.path.join(path_pres, file_name, file))
loop_count += 1
if (loop_count % 2) == 0:
mline.update('[UPDATE] created directory: {}\n'.format(path_pres + '/' + file_name), append=True, text_color_for_value=update_color)
mline.update('[UPDATE] moved file: {}\n'.format(path_pres + '/' + file_name + '/' + file), append=True, text_color_for_value=update_color)
window.refresh()
else:
mline.update('[UPDATE] created directory: {}\n'.format(path_pres + '/' + file_name), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
mline.update('[UPDATE] moved file: {}\n'.format(path_pres + '/' + file_name + '/' + file), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
window.refresh()
file_count += 1
window.refresh()
mline.update('[SUMMARY] Created {} Representation directories | Moved {} files into created directories\n\n'.format(folder_count, file_count), append=True, text_color_for_value=summary_color)
window.refresh()
#this function stages the "Representation_" folders for each asset inside a new directory,
#then zipes the files into individual PAX objects and deletes the source files
def create_pax(window, mline, alt_background, init_color, update_color, summary_color, projpath):
mline.update('[START] CREATING PAX OBJECTS\n', append=True, text_color_for_value=init_color)
window.refresh()
pax_count = 0
path_container = os.path.join(projpath, container)
for directory in os.listdir(path = path_container):
path_directory = os.path.join(projpath, container, directory)
path_paxstage = os.path.join(projpath, container, directory, 'pax_stage')
os.mkdir(path_paxstage)
shutil.move(os.path.join(path_directory, 'Representation_Preservation'), path_paxstage)
shutil.move(os.path.join(path_directory, 'Representation_Access'), path_paxstage)
path_directory = os.path.join(projpath, container, directory)
zip_dir = pathlib.Path(path_paxstage)
pax_obj = ZipFile(os.path.join(path_directory, directory + '.zip'), 'w')
for file_path in zip_dir.rglob("*"):
pax_obj.write(file_path, arcname = file_path.relative_to(zip_dir))
pax_obj.close()
os.replace(os.path.join(path_directory, directory + '.zip'), os.path.join(path_directory, directory + '.pax.zip'))
time.sleep(1)
pax_count += 1
shutil.rmtree(path_paxstage)
if (pax_count % 2) == 0:
mline.update('[UPDATE] created {}\n'.format(str(pax_count) + ': ' + directory + '.pax.zip'), append=True, text_color_for_value=update_color)
window.refresh()
else:
mline.update('[UPDATE] created {}\n'.format(str(pax_count) + ': ' + directory + '.pax.zip'), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
window.refresh()
mline.update('[SUMMARY] Created {} PAX objects\n\n'.format(pax_count), append=True, text_color_for_value=summary_color)
window.refresh()
#this function creates the OPEX metadata file that accompanies an individual zipped PAX package
#this function also includes the metadata necessary for ArchivesSpace sync to Preservica
def pax_metadata(window, mline, alt_background, init_color, update_color, summary_color, projpath, workorder, worksheet, maxrow, format_dates):
mline.update('[START] CREATING METADATA FILES FOR PAX OBJECTS\n', append=True, text_color_for_value=init_color)
window.refresh()
wb = load_workbook(workorder)
ws = wb[worksheet]
dir_count = 0
maximumrow = int(maxrow)
path_container = os.path.join(projpath, container)
for directory in os.listdir(path = path_container):
path_directory = os.path.join(projpath, container, directory)
pax_hand = open(os.path.join(path_directory, directory + '.pax.zip'), 'rb')
pax_read = pax_hand.read()
sha1_checksum = hashlib.sha1(pax_read).hexdigest()
pax_hand.close()
iterrow = 2
for row in ws.iter_rows(min_row = minrow, min_col = mincol, max_row = maximumrow, max_col = maxcol):
for cell in row:
cuid = ws.cell(row = iterrow, column = filecol).value
if cuid == directory:
ref_id = ws.cell(row = iterrow, column = refidcol).value
title = ws.cell(row = iterrow, column = titlecol).value
if '&' in title:
title = title.replace('&', 'and')
if format_dates == True:
date_full = ws.cell(row = iterrow, column = datecol).value
date_formatted = aspace_dates(date_full)
display_title = '{title}{date_formatted}'.format(title=title, date_formatted=date_formatted)
else:
display_title = '{title}'.format(title=title)
opex = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<opex:OPEXMetadata xmlns:opex="http://www.openpreservationexchange.org/opex/v1.0">
<opex:Transfer>
<opex:Fixities>
<opex:Fixity type="SHA-1" value="{sha1_checksum}"/>
</opex:Fixities>
</opex:Transfer>
<opex:Properties>
<opex:Title>{title}</opex:Title>
<opex:Identifiers>
<opex:Identifier type="code">{ref_id}</opex:Identifier>
</opex:Identifiers>
</opex:Properties>
<opex:DescriptiveMetadata>
<LegacyXIP xmlns="http://preservica.com/LegacyXIP">
<AccessionRef>catalogue</AccessionRef>
</LegacyXIP>
</opex:DescriptiveMetadata>
</opex:OPEXMetadata>'''.format(sha1_checksum=sha1_checksum, title=display_title, ref_id=ref_id)
filename = directory + '.pax.zip.opex'
pax_md_hand = open(os.path.join(path_directory, filename), 'w')
pax_md_hand.write(opex)
pax_md_hand.close()
dir_count += 1
if (dir_count % 2) == 0:
mline.update('[UPDATE] created {}\n'.format(filename), append=True, text_color_for_value=update_color)
window.refresh()
else:
mline.update('[UPDATE] created {}\n'.format(filename), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
window.refresh()
iterrow += 1
mline.update('[SUMMARY] Created {} OPEX metdata files for individual assets\n\n'.format(dir_count), append=True, text_color_for_value=summary_color)
window.refresh()
#this function matches directory names (based on CUID) with archival object numbers from work order spreadsheet
#this metadata is another facet required for ArchivesSpace to Preservica synchronization
def ao_opex_metadata(window, mline, alt_background, init_color, update_color, summary_color, projpath, workorder, worksheet, maxrow):
mline.update('[START] CREATE ARCHIVAL OBJECT OPEX METADATA\n', append=True, text_color_for_value=init_color)
window.refresh()
wb = load_workbook(workorder)
ws = wb[worksheet]
file_count = 0
path_container = os.path.join(projpath, container)
maximumrow = int(maxrow)
for directory in os.listdir(path = path_container):
path_directory = os.path.join(projpath, container, directory)
iterrow = 2
for row in ws.iter_rows(min_row = minrow, min_col = mincol, max_row = maximumrow, max_col = maxcol):
for cell in row:
cuid = ws.cell(row = iterrow, column = filecol).value
if cuid == directory:
ao_num_uri = ws.cell(row = iterrow, column = aouricol).value
ao_num = 'archival_object_' + ao_num_uri.split('/')[-1].strip()
opex = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<opex:OPEXMetadata xmlns:opex="http://www.openpreservationexchange.org/opex/v1.0">
<opex:Properties>
<opex:Title>{ao_num}</opex:Title>
<opex:Identifiers>
<opex:Identifier type="code">{ao_num}</opex:Identifier>
</opex:Identifiers>
</opex:Properties>
<opex:DescriptiveMetadata>
<LegacyXIP xmlns="http://preservica.com/LegacyXIP">
<Virtual>false</Virtual>
</LegacyXIP>
</opex:DescriptiveMetadata>
</opex:OPEXMetadata>'''.format(ao_num = ao_num)
with open(os.path.join(path_directory, ao_num + '.opex'), 'w') as ao_md:
ao_md.write(opex)
file_count += 1
os.replace(path_directory, os.path.join(path_container, ao_num))
time.sleep(1)
if (file_count % 2) == 0:
mline.update('[UPDATE] processed folder metadata in new folder: {}\n'.format(ao_num), append=True, text_color_for_value=update_color)
window.refresh()
else:
mline.update('[UPDATE] processed folder metadata in new folder: {}\n'.format(ao_num), append=True, text_color_for_value=update_color, background_color_for_value=alt_background)
window.refresh()
iterrow += 1
mline.update('[SUMMARY] Created {} archival object metadata files\n\n'.format(file_count), append=True, text_color_for_value=summary_color)
window.refresh()
#this function creates the last OPEX metadata file required for the OPEX incremental ingest, for the container folder
#this OPEX file has the folder manifest to ensure that content is ingested properly
def opex_container_metadata(window, mline, init_color, summary_color, projpath):
mline.update('[START] CREATE CONTAINER OBJECT OPEX METADATA\n', append=True, text_color_for_value=init_color)
window.refresh()
opex1 = '''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<opex:OPEXMetadata xmlns:opex="http://www.openpreservationexchange.org/opex/v1.0">
<opex:Transfer>
<opex:Manifest>
<opex:Folders>\n'''
opex2 = ''
path_container = os.path.join(projpath, container)
for directory in os.listdir(path = path_container):
opex2 += '\t\t\t\t<opex:Folder>' + directory + '</opex:Folder>\n'
opex3 = '''\t\t\t</opex:Folders>
</opex:Manifest>
</opex:Transfer>
</opex:OPEXMetadata>'''
container_opex_hand = open(os.path.join(projpath, container, container + '.opex'), 'w')
container_opex_hand.write(opex1 + opex2 + opex3)
mline.update('[SUMMARY] Created OPEX metadata file for {} directory\n\n'.format(container), append=True, text_color_for_value=summary_color)
window.refresh()
container_opex_hand.close()