From 51a175634d24eec42ad5a7cbe23660b42a63470c Mon Sep 17 00:00:00 2001 From: Joel Andres Granados Date: Thu, 7 Aug 2008 19:02:36 +0200 Subject: Create a new backup directory every time, unless the user tells us otherwise. Use tempfile to create safe backup directories. User should define backup.fullpath if he wants to override the default behaviour. User can also choose the directory where the backup directory goes. --- pyfirstaidkit/utils/backup.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'pyfirstaidkit/utils/backup.py') diff --git a/pyfirstaidkit/utils/backup.py b/pyfirstaidkit/utils/backup.py index f7670c2..79e4b85 100644 --- a/pyfirstaidkit/utils/backup.py +++ b/pyfirstaidkit/utils/backup.py @@ -25,6 +25,7 @@ import hashlib import weakref import cPickle as pickle import copy +import tempfile class BackupException(Exception): pass @@ -209,20 +210,28 @@ class FileBackupStore(BackupStoreInterface): return False - def __init__(self, path): + def __init__(self, rootpath = "/tmp", fullpath = ""): if self.__class__._singleton: raise BackupException("BackupStore with %s type can have only " "one instance" % (self.__name__,)) assert self.__class__._singleton==None - self._path = path - self._backups = {} - if os.path.isdir(self._path): - raise BackupException("Backupdir %s already exists. Erase dir or " - "change backup dir." % self._path) + if fullpath: + if os.path.isdir(fullpath): + raise BackupException("Backupdir %s already exists. Erase " + "dir or change backup dir." % self._path) + else: + self._path = fullpath + os.makedirs(fullpath) + else: - os.makedirs(self._path) + if not os.path.isdir(rootpath): + os.makedirs(rootpath) + self._path = tempfile.mkdtemp(prefix = "firstaidkitbackup-", dir = rootpath) + + self._backups = {} + self.__class__._singleton = weakref.proxy(self) print("Backup system initialized") -- cgit