-
Notifications
You must be signed in to change notification settings - Fork 103
/
fileOperations.py
297 lines (251 loc) · 11.9 KB
/
fileOperations.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
#!/usr/bin/env python2
# This file is part of Archivematica.
#
# Copyright 2010-2013 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica. If not, see <http://www.gnu.org/licenses/>.
# @package Archivematica
# @subpackage archivematicaCommon
# @author Joseph Perry <[email protected]>
from __future__ import absolute_import, print_function
import csv
import os
import uuid
import sys
import shutil
from databaseFunctions import insertIntoFiles
from executeOrRunSubProcess import executeOrRun
from databaseFunctions import insertIntoEvents
import MySQLdb
from archivematicaFunctions import unicodeToStr, get_setting, get_file_checksum
from main.models import File, Transfer
def updateSizeAndChecksum(fileUUID, filePath, date, eventIdentifierUUID, fileSize=None, checksum=None, checksumType=None, add_event=True):
"""
Update a File with its size, checksum and checksum type. These are
parameters that can be either generated or provided via keywords.
Finally, insert the corresponding Event. This behavior can be cancelled
using the boolean keyword 'add_event'.
"""
if not fileSize:
fileSize = os.path.getsize(filePath)
if not checksumType:
checksumType = get_setting('checksum_type', 'sha256')
if not checksum:
checksum = get_file_checksum(filePath, checksumType)
File.objects.filter(uuid=fileUUID).update(size=fileSize, checksum=checksum, checksumtype=checksumType)
if add_event:
insertIntoEvents(fileUUID=fileUUID,
eventType='message digest calculation',
eventDateTime=date,
eventDetail='program="python"; module="hashlib.{}()"'.format(checksumType),
eventOutcomeDetailNote=checksum)
def addFileToTransfer(filePathRelativeToSIP, fileUUID, transferUUID, taskUUID, date, sourceType="ingestion", eventDetail="", use="original"):
# print filePathRelativeToSIP, fileUUID, transferUUID, taskUUID, date, sourceType, eventDetail, use
insertIntoFiles(fileUUID, filePathRelativeToSIP, date, transferUUID=transferUUID, use=use)
insertIntoEvents(fileUUID=fileUUID,
eventType=sourceType,
eventDateTime=date,
eventDetail=eventDetail,
eventOutcome="",
eventOutcomeDetailNote="")
addAccessionEvent(fileUUID, transferUUID, date)
def addAccessionEvent(fileUUID, transferUUID, date):
transfer = Transfer.objects.get(uuid=transferUUID)
if transfer.accessionid:
eventOutcomeDetailNote = "accession#" + MySQLdb.escape_string(transfer.accessionid)
insertIntoEvents(fileUUID=fileUUID,
eventType="registration",
eventDateTime=date,
eventDetail="",
eventOutcome="",
eventOutcomeDetailNote=eventOutcomeDetailNote)
def addFileToSIP(filePathRelativeToSIP, fileUUID, sipUUID, taskUUID, date, sourceType="ingestion", use="original"):
insertIntoFiles(fileUUID, filePathRelativeToSIP, date, sipUUID=sipUUID, use=use)
insertIntoEvents(fileUUID=fileUUID,
eventType=sourceType,
eventDateTime=date,
eventDetail="",
eventOutcome="",
eventOutcomeDetailNote="")
# Used to write to file
# @output - the text to append to the file
# @fileName - The name of the file to create, or append to.
# @returns - 0 if ok, non zero if error occured.
def writeToFile(output, fileName, writeWhite=False):
# print fileName
if not writeWhite and output.isspace():
return 0
if fileName and output:
# print "writing to: " + fileName
try:
f = open(fileName, 'a')
f.write(output.__str__())
f.close()
os.chmod(fileName, 488)
except OSError as ose:
print("output Error", ose, file=sys.stderr)
return -2
except IOError as e:
(errno, strerror) = e.args
print("I/O error({0}): {1}".format(errno, strerror))
return -3
else:
print("No output, or file specified")
return 0
def rename(source, destination):
"""Used to move/rename directories. This function was before used to wrap the operation with sudo."""
command = ["mv", source, destination]
exitCode, stdOut, stdError = executeOrRun("command", command, "", printing=False)
if exitCode:
print("exitCode:", exitCode, file=sys.stderr)
print(stdOut, file=sys.stderr)
print(stdError, file=sys.stderr)
exit(exitCode)
def updateDirectoryLocation(src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith):
srcDB = src.replace(unitPath, unitPathReplaceWith)
if not srcDB.endswith("/") and srcDB != unitPathReplaceWith:
srcDB += "/"
dstDB = dst.replace(unitPath, unitPathReplaceWith)
if not dstDB.endswith("/") and dstDB != unitPathReplaceWith:
dstDB += "/"
kwargs = {
"removedtime__isnull": True,
"currentlocation__startswith": srcDB,
unitIdentifierType: unitIdentifier
}
files = File.objects.filter(**kwargs)
for f in files:
f.currentlocation = f.currentlocation.replace(srcDB, dstDB)
f.save()
if os.path.isdir(dst):
if dst.endswith("/"):
dst += "."
else:
dst += "/."
print("moving: ", src, dst)
shutil.move(src, dst)
def updateFileLocation2(src, dst, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith):
"""Dest needs to be the actual full destination path with filename."""
srcDB = src.replace(unitPath, unitPathReplaceWith)
dstDB = dst.replace(unitPath, unitPathReplaceWith)
# Fetch the file UUID
kwargs = {
"removedtime__isnull": True,
"currentlocation": srcDB,
unitIdentifierType: unitIdentifier
}
try:
f = File.objects.get(**kwargs)
except (File.DoesNotExist, File.MultipleObjectsReturned) as e:
if isinstance(e, File.DoesNotExist):
message = "no results found"
else:
message = "multiple results found"
print('ERROR: file information not found:', message, "for arguments:", repr(kwargs), file=sys.stderr)
exit(4)
# Move the file
print("Moving", src, 'to', dst)
shutil.move(src, dst)
# Update the DB
f.currentlocation = dstDB
f.save()
def updateFileLocation(src, dst, eventType="", eventDateTime="", eventDetail="", eventIdentifierUUID=uuid.uuid4().__str__(), fileUUID="None", sipUUID=None, transferUUID=None, eventOutcomeDetailNote="", createEvent=True):
"""
Updates file location in the database, and optionally writes an event for the sanitization to the database.
Note that this does not actually move a file on disk.
If the file uuid is not provided, will use the SIP uuid and the old path to find the file uuid.
To suppress creation of an event, pass the createEvent keyword argument (for example, if the file moved due to the renaming of a parent directory and not the file itself).
"""
src = unicodeToStr(src)
dst = unicodeToStr(dst)
fileUUID = unicodeToStr(fileUUID)
if not fileUUID or fileUUID == "None":
kwargs = {
"removedtime__isnull": True,
"currentlocation": src
}
if sipUUID:
kwargs["sip_id"] = sipUUID
elif transferUUID:
kwargs["transfer_id"] = transferUUID
else:
raise ValueError("One of fileUUID, sipUUID, or transferUUID must be provided")
f = File.objects.get(**kwargs)
else:
f = File.objects.get(uuid=fileUUID)
# UPDATE THE CURRENT FILE PATH
f.currentlocation = dst
f.save()
if not createEvent:
return
if eventOutcomeDetailNote == "":
eventOutcomeDetailNote = "Original name=\"%s\"; cleaned up name=\"%s\"" % (src, dst)
# CREATE THE EVENT
insertIntoEvents(fileUUID=f.uuid, eventType=eventType, eventDateTime=eventDateTime, eventDetail=eventDetail, eventOutcome="", eventOutcomeDetailNote=eventOutcomeDetailNote)
def getFileUUIDLike(filePath, unitPath, unitIdentifier, unitIdentifierType, unitPathReplaceWith):
"""Dest needs to be the actual full destination path with filename."""
srcDB = filePath.replace(unitPath, unitPathReplaceWith)
kwargs = {
"removedtime__isnull": True,
"currentlocation__contains": srcDB,
unitIdentifierType: unitIdentifier
}
return {f.currentlocation: f.uuid for f in File.objects.filter(**kwargs)}
def updateFileGrpUsefileGrpUUID(fileUUID, fileGrpUse, fileGrpUUID):
File.objects.filter(uuid=fileUUID).update(filegrpuse=fileGrpUse, filegrpuuid=fileGrpUUID)
def updateFileGrpUse(fileUUID, fileGrpUse):
File.objects.filter(uuid=fileUUID).update(filegrpuse=fileGrpUse)
def findFileInNormalizatonCSV(csv_path, commandClassification, target_file, sip_uuid):
""" Returns the original filename or None for a manually normalized file.
:param str csv_path: absolute path to normalization.csv
:param str commandClassification: "access" or "preservation"
:param str target_file: Path for access or preservation file to match against, relative to the objects directory
:param str sip_uuid: UUID of the SIP the files belong to
:returns: Path to the origin file for `target_file`. Note this is the path from normalization.csv, so will be the original location.
"""
# use universal newline mode to support unusual newlines, like \r
with open(csv_path, 'rbU') as csv_file:
reader = csv.reader(csv_file)
# Search CSV for an access/preservation filename that matches target_file
# Get original name of target file, to handle sanitized names
try:
f = File.objects.get(removedtime__isnull=True,
currentlocation__endswith=target_file,
sip_id=sip_uuid)
except File.MultipleObjectsReturned:
print("More than one result found for {} file ({}) in DB.".format(commandClassification, target_file), file=sys.stderr)
sys.exit(2)
except File.DoesNotExist:
print("{} file ({}) not found in DB.".format(commandClassification, target_file), file=sys.stderr)
sys.exit(2)
target_file = f.originallocation.replace('%transferDirectory%objects/', '', 1).replace('%SIPDirectory%objects/', '', 1)
try:
for row in reader:
if not row:
continue
if "#" in row[0]: # ignore comments
continue
original, access, preservation = row
if commandClassification == "access" and access == target_file:
print("Found access file ({0}) for original ({1})".format(access, original))
return original
if commandClassification == "preservation" and preservation == target_file:
print("Found preservation file ({0}) for original ({1})".format(preservation, original))
return original
else:
return None
except csv.Error:
print("Error reading {filename} on line {linenum}".format(
filename=csv_path, linenum=reader.line_num), file=sys.stderr)
sys.exit(2)