-
Notifications
You must be signed in to change notification settings - Fork 34
/
deletefiles.py
executable file
·140 lines (132 loc) · 4.7 KB
/
deletefiles.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
#!/usr/bin/env python
'''
Deletes files after sipcreator has been run, but before accession.py has been run.
Manifests are updated and metadata is deleted.
'''
import os
import argparse
import sys
import datetime
import ififuncs
def parse_args(args_):
'''
Parse command line arguments.
'''
parser = argparse.ArgumentParser(
description='Deletes files from Object Entry packages'
' Written by Kieran O\'Leary.'
)
parser.add_argument(
'-i', nargs='+',
help='full path of files to be deleted', required=True
)
parser.add_argument(
'-uuid_path',
help='full path of \'sipcreator\' Object Entry package'
)
parser.add_argument(
'-user',
help='Declare who you are. If this is not set, you will be prompted.')
parsed_args = parser.parse_args(args_)
return parsed_args
def remove_from_manifest(manifest, old_oe, new_log_textfile):
'''
Removes a file from a manifest and logs the result in the logfile.
'''
updated_lines = []
with open(manifest, 'r') as file_object:
checksums = file_object.readlines()
for line in checksums:
if old_oe in line:
print(('%s has been removed from the package manifest' % line))
ififuncs.generate_log(
new_log_textfile,
'EVENT = eventType=metadata modification,'
' agentName=deletefiles.py,'
' eventDetail=%s has been removed from the package manifest' % line)
continue
else:
updated_lines.append(line)
with open(manifest, 'w') as updated_manifest:
for updated_line in updated_lines:
updated_manifest.write(updated_line)
def main(args_):
'''
Launch all the functions for creating an IFI SIP.
'''
args = parse_args(args_)
source = args.uuid_path
sip_path = ififuncs.check_for_sip([source])
if sip_path is not None:
oe_path = os.path.dirname(sip_path)
uuid = os.path.basename(sip_path)
sip_manifest = os.path.join(
oe_path, uuid
) + '_manifest.md5'
else:
# this is assuming that the other workflow will be the
# special collections workflow that has the uuid as the parent.
# some real checks should exist for this whole if/else flow.
sip_path = args.uuid_path
oe_path = os.path.dirname(args.uuid_path)
uuid = os.path.basename(sip_path)
sip_manifest = os.path.join(
oe_path, uuid + '_manifest.md5'
)
start = datetime.datetime.now()
print(args)
if args.user:
user = args.user
else:
user = ififuncs.get_user()
new_log_textfile = os.path.join(sip_path, 'logs' + '/' + uuid + '_sip_log.log')
ififuncs.generate_log(
new_log_textfile,
'EVENT = deletefiles.py started'
)
ififuncs.generate_log(
new_log_textfile,
'eventDetail=deletefiles.py %s' % ififuncs.get_script_version('deletefiles.py')
)
ififuncs.generate_log(
new_log_textfile,
'Command line arguments: %s' % args
)
ififuncs.generate_log(
new_log_textfile,
'EVENT = agentName=%s' % user
)
metadata_dir = os.path.join(sip_path, 'metadata')
for filename in args.i:
# add test to see if it actually deleted - what if read only?
os.remove(filename)
print(('%s has been deleted' % filename))
ififuncs.generate_log(
new_log_textfile,
'EVENT = eventType=deletion,'
' eventOutcomeDetailNote=%s has been deleted,'
' agentName=os.remove()'
% filename
)
for metadata in os.listdir(metadata_dir):
if os.path.basename(filename) in metadata:
os.remove(os.path.join(metadata_dir, metadata))
print(('%s has been deleted' % os.path.join(metadata_dir, metadata)))
ififuncs.generate_log(
new_log_textfile,
'EVENT = eventType=deletion,'
' eventOutcomeDetailNote=%s has been deleted,'
' agentName=os.remove()'
% os.path.join(metadata_dir, metadata)
)
remove_from_manifest(sip_manifest, os.path.basename(filename), new_log_textfile)
ififuncs.sort_manifest(sip_manifest)
ififuncs.generate_log(
new_log_textfile,
'EVENT = deletefiles.py finished'
)
ififuncs.checksum_replace(sip_manifest, new_log_textfile, 'md5')
finish = datetime.datetime.now()
print('\n- %s ran this script at %s and it finished at %s' % (user, start, finish))
if __name__ == '__main__':
main(sys.argv[1:])