From 07482c18ee1764db459bd883e8a27b0932e6cd8e Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Fri, 10 Aug 2012 15:59:49 +0800 Subject: [PATCH] Backup names are more user friendly --- lib/backup.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/backup.py b/lib/backup.py index 70227d3..aacc2d2 100644 --- a/lib/backup.py +++ b/lib/backup.py @@ -33,7 +33,7 @@ class BackupManager(object): raise Exception('Cannot backup instance without identifier!') now = int(time.time()) - backup_identifier = '%s_%s' % (name, now) + backup_identifier = self.createIdentifier(name) Wp = Workplace() # Copy whole directory, shutil will create topath @@ -63,17 +63,31 @@ class BackupManager(object): return True + def createIdentifier(self, name): + """Creates an identifier""" + for i in range(1, 100): + identifier = '{0}_{1:0>2}'.format(name, i) + if not self.exists(identifier): + break + identifier = None + if not identifier: + raise Exception('Could not generate a backup identifier! How many backup did you do?!') + return identifier + + def exists(self, name): + """Checks whether a backup exists under this name or not""" + d = os.path.join(self.path, name) + f = os.path.join(d, jason) + if not os.path.isdir(d): + return False + return os.path.isfile(f) + def get(self, name): return Backup(self.getPath(name)) def getPath(self, name): return os.path.join(self.path, name) - def exists(self, name): - """Checks whether a backup exists under this name or not""" - f = os.path.join(self.path, name, jason) - return os.path.isfile(f) - def list(self): """Returns a list of backups with their information""" dirs = os.listdir(self.path) -- 2.11.0