-
Notifications
You must be signed in to change notification settings - Fork 0
/
manual_copy.py
286 lines (205 loc) · 9.47 KB
/
manual_copy.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
import os
import shutil
import zlib
import datetime
import csv
import zipfile
import shelve
def crc32(fileName):
with open(fileName, 'rb') as fh:
hash = 0
while True:
s = fh.read(65536)
if not s:
break
hash = zlib.crc32(s, hash)
return "%08X" % (hash & 0xFFFFFFFF)
def check_path(file_path):
if not os.path.exists(file_path):
print(os.path.basename(file_path), 'does not exist')
return False
else:
return True
def main():
global barcode, ship_dir, scan_dir, list_of_files, log_file, dest, cut_path, zip_file_list, item_info
barcode = input('Enter barcode: ')
ship_dir = input('Enter path to shipment directory: ')
scan_dir = input('Enter path to target directory: ')
list_done = input('Is file list already compiled? (Y/N) ')
if list_done.lower() == 'y':
list_done = True
else:
list_done = False
list_of_files = os.path.join('Z:/bdpl_transfer_lists', '{}.txt'.format(barcode))
log_file = os.path.join(ship_dir, barcode, 'metadata', 'logs', 'copy_log.csv')
dest = os.path.join(ship_dir, barcode, 'files')
zip_file_list = 'C:/temp/bdpl_zip.txt'
item_info = os.path.join(ship_dir, 'item_ingest_info')
for dir in (ship_dir, scan_dir, dest):
status = check_path(dir)
if not status:
return
cut_path = input('Trim source path at this point: ')
if not list_done:
file_list()
copy_files()
extract_zips()
def file_list():
print('\n\nCreating a list of files to be copied...')
avoid_ext = []
include_ext = []
avoid_files = []
avoid_dirs = []
with open(list_of_files, 'w', encoding='utf8') as f:
for root, dirs, files in os.walk(scan_dir):
for file in files:
file_ext = os.path.splitext(file)[1].lower()
if file.lower() in ['thumbs.db', '.ds_store']:
continue
if len(include_ext) > 0:
if not file_ext in include_ext:
continue
if len(avoid_ext) > 0:
if file_ext in avoid_ext:
continue
target = os.path.join(root, file).replace(os.sep, os.altsep)
if len(avoid_files) > 0:
if target in avoid_files:
continue
if len(avoid_dirs) > 0:
if any(i in os.path.dirname(target) for i in avoid_dirs):
continue
f.write('{}\n'.format(target))
print('\nFile list is compiled!')
def copy_files():
zfl = open(zip_file_list, 'w', encoding='utf8')
header = ['File', 'Source CRC', 'Destination CRC', 'Time']
copy_error = False
with open(log_file, 'w', encoding='utf8') as o_f:
writer = csv.writer(o_f, lineterminator='\n')
writer.writerow(header)
print('Reviewing copy list....\n\n')
with open(list_of_files, 'r', encoding='utf8') as i_f:
counter = 0
temp_list = i_f.read().splitlines()
total = len(temp_list)
for file in temp_list:
counter += 1
rel_path = os.path.relpath(os.path.dirname(file), cut_path)
dest_path = os.path.join(dest, rel_path)
moved_file = os.path.join(dest_path, os.path.basename(file))
if os.path.exists(moved_file):
continue
print('Working on file {} of {} ({})'.format(counter, total, file))
crc_source = crc32(file)
if not os.path.exists(dest_path):
os.makedirs(dest_path)
shutil.copy(file, dest_path)
if os.path.exists(moved_file):
crc_dest = crc32(moved_file)
else:
print('Error:', file)
copy_error = True
continue
if crc_source != crc_dest:
dest_file = os.path.join(dest_path, os.path.basename(file))
try:
os.remove(dest_file)
except:
pass
copy_error = True
timestamp = str(datetime.datetime.now())
info = [moved_file, crc_source, crc_dest, timestamp]
writer.writerow(info)
if os.path.splitext(moved_file)[1].lower() == '.zip':
zfl.write('{}\n'.format(moved_file))
zfl.close()
if copy_error:
print('\nCheck errors and manually copy files as needed')
else:
print('\nCopy complete!')
temp_dict = {}
temp_dict['eventType'] = 'replication'
temp_dict['eventOutcomeDetail'] = 0
temp_dict['timestamp'] = str(datetime.datetime.now())
temp_dict['eventDetailInfo'] = "python copy.shutil with CRC32 hash comparison"
temp_dict['eventDetailInfo_additional'] = "The process of creating a copy of an object that is, bit-wise, identical to the original."
temp_dict['linkingAgentIDvalue'] = 'python 3.7.3'
write_premis(temp_dict)
def write_premis(temp_dict):
my_shelve = os.path.join(item_info, '{}-info'.format(barcode))
db = shelve.open(my_shelve, writeback=True)
db['premis'].append(temp_dict)
db.sync()
db.close()
def extract_zips():
existing = []
barcode_list = []
error_list = []
with open(zip_file_list, 'r') as f:
f_ls = f.read().splitlines()
total = len(f_ls)
if total == 0:
return
counter = 0
for file in f_ls:
counter +=1
print('\nWorking on .zip {} of {} ({})'.format(counter, total, file))
dir_name = os.path.splitext(file)[0]
if os.path.exists(dir_name):
print('\tAlready exists!'.format(dir_name))
existing.append(file)
continue
else:
print('\tMaking folders')
os.makedirs(dir_name)
#check zip file and then extract
with zipfile.ZipFile(file, 'r') as zip_ref:
print('\tTesting zip...')
chk = zip_ref.testzip()
if chk is None:
print('\tExtracting zip...')
zip_ref.extractall(dir_name)
else:
print('\tERROR!!!')
error_list.append(file)
continue
#delete zip file
print('\tDeleting zip...')
os.remove(file)
#record in log file
print('\tWriting to log file...')
timestamp =str(datetime.datetime.now())
unpack_log = os.path.join(ship_dir, barcode, 'metadata', 'logs', 'unpack_log.csv')
header = ['Archive File', 'Folder' 'Time']
info = [file, dir_name, timestamp]
if not os.path.exists(unpack_log):
need_header = True
else:
need_header = False
with open(unpack_log, 'a', encoding='utf8') as o_f:
writer = csv.writer(o_f, lineterminator='\n')
if need_header:
writer.writerow(header)
writer.writerow(info)
#add premis metadata
if barcode in barcode_list:
continue
else:
print('\tWriting PREMIS...')
temp_dict = {}
temp_dict['eventType'] = 'unpacking'
temp_dict['eventOutcomeDetail'] = 0
temp_dict['timestamp'] = timestamp
temp_dict['eventDetailInfo'] = "with zipfile.ZipFile(file, 'r') as zip_ref: zip_ref.extractall(dir_name)"
temp_dict['eventDetailInfo_additional'] = "The process of extracting objects from .zip packages."
temp_dict['linkingAgentIDvalue'] = 'python 3.7.3 zipfile.ZipFile.extractall()'
write_premis(temp_dict)
barcode_list.append(barcode)
if len(existing) > 0:
print('\n\nThese folders already existed and may have same content as .ZIP:\n\t{}'.format('\n\t'.join(existing)))
if len(error_list) > 0:
print('\n\nThese .ZIP files had errors:\n\t{}'.format('\n\t'.join(error_list)))
print('\n\nRe-run analysis on these items:\n\t{}'.format('\n\t'.join(barcode_list)))
if __name__ == '__main__':
main()