changeset 52:85b5fe402ab4

finish backups functionality (to test)
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 07 May 2010 11:22:58 -0700
parents dc9324b52c2a
children c279d2797d45
files profilemanager/manager.py
diffstat 1 files changed, 32 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/profilemanager/manager.py	Fri May 07 09:59:08 2010 -0700
+++ b/profilemanager/manager.py	Fri May 07 11:22:58 2010 -0700
@@ -161,18 +161,45 @@
         parser.set(section, 'Backups', backups)
         self.write(parser)
 
-    def backups(self, profile=None):
+    def backups(self, profile=None, datestamp=None):
         """
         list backups for a given profile, or all profiles if the
         profile is not given;  returns a list of backups if
         profile is given or a dictionary of lists otherwise
         """
-        if profile is None:
-            # all profiles
+        if profile is None: # all profiles
+            # XXX this should probably be recursive
             retval = {}
+            profiles_dict = self.profiles_dict()
+            for profile in profiles_dict:
+                if 'Backups' in profiles_dict[profile]:
+                    retval[profile] = profiles_dict[profile].split(':')
             return retval
-        # TODO
-        raise NotImplementedError
+
+        # single profile
+        profile_dict = self.profile_dict(profile)
+        if 'Backups' not in profile_dict:
+            return []
+        backups = profile_dict['Backups'].split(':')
+        backup_dirs = []
+        for backup in backups:
+            if os.path.isabs(backup):
+                backup_dir = backup
+            else:
+                backup_dir = os.path.join(self.profile_dir, self.backups_dir, backup)
+            backups_dirs.append((backup, int(os.path.getmtime(backup_dir))))
+        # TODO: check if the getmtime == the datestamp for relative backups
+
+        # sort by reverse datestamp
+        backups_dirs.sort(key=lambda x: x[1], reverse=True)
+
+        # format to datestamps, if applicable
+        if datestamp:
+            backup_dirs = [ (i[0], time.strftime(datestamp,
+                                                 time.localtime(int(i[1]))))
+                            for i ni backup_dirs ]
+
+        return backups_dirs
 
     def restore(self, profile, date=None, delete=False):
         """