mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-25 19:44:13 +01:00
Added backup directory.
This commit is contained in:
parent
f69b87a93a
commit
904665cea6
@ -576,7 +576,6 @@ registerGlobalValue(supybot.drivers, 'module',
|
|||||||
###
|
###
|
||||||
# supybot.directories, for stuff relating to directories.
|
# supybot.directories, for stuff relating to directories.
|
||||||
###
|
###
|
||||||
|
|
||||||
class Directory(registry.String):
|
class Directory(registry.String):
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
# ??? Should we perhaps always return an absolute path here?
|
# ??? Should we perhaps always return an absolute path here?
|
||||||
@ -620,6 +619,9 @@ registerGlobalValue(supybot.directories, 'conf',
|
|||||||
put into."""))
|
put into."""))
|
||||||
registerGlobalValue(supybot.directories, 'data',
|
registerGlobalValue(supybot.directories, 'data',
|
||||||
Directory('data', """Determines what directory data is put into."""))
|
Directory('data', """Determines what directory data is put into."""))
|
||||||
|
registerGlobalValue(supybot.directories, 'backup',
|
||||||
|
Directory('backup', """Determines what directory backup data is put
|
||||||
|
into."""))
|
||||||
registerGlobalValue(supybot.directories.data, 'tmp',
|
registerGlobalValue(supybot.directories.data, 'tmp',
|
||||||
DataFilenameDirectory('tmp', """Determines what directory temporary files
|
DataFilenameDirectory('tmp', """Determines what directory temporary files
|
||||||
are put into."""))
|
are put into."""))
|
||||||
@ -627,7 +629,7 @@ registerGlobalValue(supybot.directories.data, 'tmp',
|
|||||||
# Remember, we're *meant* to replace this nice little wrapper.
|
# Remember, we're *meant* to replace this nice little wrapper.
|
||||||
def transactionalFile(*args, **kwargs):
|
def transactionalFile(*args, **kwargs):
|
||||||
kwargs['tmpDir'] = supybot.directories.data.tmp()
|
kwargs['tmpDir'] = supybot.directories.data.tmp()
|
||||||
# ??? Should we offer an option not to makeBackupIfSmaller?
|
kwargs['backupDir'] = supybot.directories.backup()
|
||||||
return utils.AtomicFile(*args, **kwargs)
|
return utils.AtomicFile(*args, **kwargs)
|
||||||
utils.transactionalFile = transactionalFile
|
utils.transactionalFile = transactionalFile
|
||||||
|
|
||||||
|
24
src/utils.py
24
src/utils.py
@ -714,13 +714,14 @@ class AtomicFile(file):
|
|||||||
"""Used for files that need to be atomically written -- i.e., if there's a
|
"""Used for files that need to be atomically written -- i.e., if there's a
|
||||||
failure, the original file remains, unmodified. mode must be 'w' or 'wb'"""
|
failure, the original file remains, unmodified. mode must be 'w' or 'wb'"""
|
||||||
def __init__(self, filename, mode='w', allowEmptyOverwrite=True,
|
def __init__(self, filename, mode='w', allowEmptyOverwrite=True,
|
||||||
makeBackupIfSmaller=True, tmpDir=None):
|
makeBackupIfSmaller=True, tmpDir=None, backupDir=None):
|
||||||
if mode not in ('w', 'wb'):
|
if mode not in ('w', 'wb'):
|
||||||
raise ValueError, 'Invalid mode: %r' % mode
|
raise ValueError, 'Invalid mode: %r' % mode
|
||||||
self.rolledback = False
|
self.rolledback = False
|
||||||
self.allowEmptyOverwrite = allowEmptyOverwrite
|
self.allowEmptyOverwrite = allowEmptyOverwrite
|
||||||
self.makeBackupIfSmaller = makeBackupIfSmaller
|
self.makeBackupIfSmaller = makeBackupIfSmaller
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
|
self.backupDir = backupDir
|
||||||
if tmpDir is None:
|
if tmpDir is None:
|
||||||
# If not given a tmpDir, we'll just put a random token on the end
|
# If not given a tmpDir, we'll just put a random token on the end
|
||||||
# of our filename and put it in the same directory.
|
# of our filename and put it in the same directory.
|
||||||
@ -746,19 +747,24 @@ class AtomicFile(file):
|
|||||||
super(AtomicFile, self).close()
|
super(AtomicFile, self).close()
|
||||||
# We don't mind writing an empty file if the file we're overwriting
|
# We don't mind writing an empty file if the file we're overwriting
|
||||||
# doesn't exist.
|
# doesn't exist.
|
||||||
size = os.path.getsize(self.tempFilename)
|
newSize = os.path.getsize(self.tempFilename)
|
||||||
originalExists = os.path.exists(self.filename)
|
originalExists = os.path.exists(self.filename)
|
||||||
# We use shutil.move here instead of os.rename because
|
if newSize or self.allowEmptyOverwrite or not originalExists:
|
||||||
# the latter doesn't work on Windows when self.filename
|
|
||||||
# (the target) already exists. shutil.move handles those
|
|
||||||
# intricacies for us.
|
|
||||||
if size or self.allowEmptyOverwrite or not originalExists:
|
|
||||||
if originalExists:
|
if originalExists:
|
||||||
currentSize = os.path.getsize(self.filename)
|
oldSize = os.path.getsize(self.filename)
|
||||||
if self.makeBackupIfSmaller and size < currentSize:
|
if self.makeBackupIfSmaller and newSize < oldSize:
|
||||||
|
print '*** newSize:', newSize, 'oldSize:', oldSize
|
||||||
now = int(time.time())
|
now = int(time.time())
|
||||||
backupFilename = '%s.backup.%s' % (self.filename, now)
|
backupFilename = '%s.backup.%s' % (self.filename, now)
|
||||||
|
if self.backupDir is not None:
|
||||||
|
backupFilename = os.path.basename(backupFilename)
|
||||||
|
backupFilename = os.path.join(self.backupDir,
|
||||||
|
backupFilename)
|
||||||
shutil.copy(self.filename, backupFilename)
|
shutil.copy(self.filename, backupFilename)
|
||||||
|
# We use shutil.move here instead of os.rename because
|
||||||
|
# the latter doesn't work on Windows when self.filename
|
||||||
|
# (the target) already exists. shutil.move handles those
|
||||||
|
# intricacies for us.
|
||||||
shutil.move(self.tempFilename, self.filename)
|
shutil.move(self.tempFilename, self.filename)
|
||||||
else:
|
else:
|
||||||
raise ValueError, 'AtomicFile.close called after rollback.'
|
raise ValueError, 'AtomicFile.close called after rollback.'
|
||||||
|
Loading…
Reference in New Issue
Block a user