-
Notifications
You must be signed in to change notification settings - Fork 34
/
rearrange.py
executable file
·132 lines (124 loc) · 4.26 KB
/
rearrange.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
#!/usr/bin/env python
'''
Moves files into subfolders, updates logfiles and manifests.
'''
import os
import argparse
import sys
import datetime
import shutil
import ififuncs
def parse_args(args_):
'''
Parse command line arguments.
'''
parser = argparse.ArgumentParser(
description='Moves files into subfolders, updates logfiles and manifests.'
' Written by Kieran O\'Leary.'
)
parser.add_argument(
'-i', nargs='+',
help='full path of files to be moved', required=True
)
parser.add_argument(
'-new_folder',
help='full path of the new destination folder', required=True
)
parser.add_argument(
'input',
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 update_manifest(manifest, old_path, new_path, new_log_textfile):
'''
Updates the path in a checksum manifest to reflect the new location.
'''
updated_lines = []
with open(manifest, 'r') as file_object:
checksums = file_object.readlines()
for line in checksums:
if old_path in line:
line = line.replace(old_path, new_path)
print 'the following path: %s has been updated with %s in the package manifest' % (old_path, new_path)
ififuncs.generate_log(
new_log_textfile,
'EVENT = eventType=metadata modification,'
' agentName=rearrange.py,'
' eventDetail=the following path: %s has been updated with %s in the package manifest' % (old_path, new_path)
)
updated_lines.append(line)
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.input
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'
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 = rearrange.py started'
)
ififuncs.generate_log(
new_log_textfile,
'eventDetail=rearrange.py %s' % ififuncs.get_script_version('rearrange.py')
)
ififuncs.generate_log(
new_log_textfile,
'Command line arguments: %s' % args
)
ififuncs.generate_log(
new_log_textfile,
'EVENT = agentName=%s' % user
)
if not os.path.isdir(args.new_folder):
os.makedirs(args.new_folder)
for filename in args.i:
# add test to see if it actually deleted - what if read only?
shutil.move(filename, args.new_folder)
print '%s has been moved into %s' % (filename, args.new_folder)
ififuncs.generate_log(
new_log_textfile,
'EVENT = eventType=file movement,'
' eventOutcomeDetailNote=%s has been moved into %s'
' agentName=shutil.move()'
% (filename, args.new_folder)
)
relative_filename = filename.replace(args.input + '/', '')
relative_new_folder = args.new_folder.replace(args.input + '/', '')
update_manifest(
sip_manifest,
relative_filename,
os.path.join(relative_new_folder, os.path.basename(relative_filename)),
new_log_textfile
)
ififuncs.generate_log(
new_log_textfile,
'EVENT = rearrange.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:])