changeset 66:67dc95a355cc

finish basic restore logic
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 07 May 2010 14:56:59 -0700
parents 1cd3e3c71bff
children 7da81d5545a2
files profilemanager/manager.py
diffstat 1 files changed, 37 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/profilemanager/manager.py	Fri May 07 14:14:17 2010 -0700
+++ b/profilemanager/manager.py	Fri May 07 14:56:59 2010 -0700
@@ -5,6 +5,7 @@
 import os
 import shutil
 import string
+import tempfile
 import time
 import ConfigParser
 from datetime import datetime
@@ -218,6 +219,9 @@
 
         assert not (date and delete), 'date and delete cannot be used in conjunction'
 
+        # get the path to the profile
+        path = self.path(profile)
+
         # get the possible backups
         backups = self.backups(profile)
         # TODO: check to see if these all exist (print warnings if not)
@@ -248,16 +252,46 @@
         else:
             if not backups:
                 raise NoBackupError("No backups for profile %s in %s" % (profile, self.profiles))
-            backups = backups[0][0]
+            backup = backups[0]
+
+        # determine path of the backup
+        if os.path.isabs(backup[0])
+            backup_path = backup
+        else:
+            backup_path = os.path.join(self.profile_dir, self.backups_dir, backup)
 
         # restore the backup over ``profile``
-        
+        # make a backup copy first to avoid data loss
+        tempdir = tempfile.mktemp()
+        shutil.move(path, tempdir)
+        try:
+            shutil.copytree(backup_path, path)
+        except Exception, e:
+            shutil.rmtree(path)
+            shutil.move(tempdir, path)
+            raise IOError("Couldn't restore backup: %s" % str(e))
+        shutil.rmtree(tempdir)
+        # TODO: (optionally) make a backup of the current profile before restore
 
         if delete: # delete the backup
+
             # delete the directory
+            shutil.rmtree(backup_path)
+            
             # delete the entry from ``profiles.ini``
+            parser = self.parser()
+            section = self.section(profile)
+            backups = parser.get(section, 'Backups')
+            backups = [ i for i in backups if i != backup[0] ]
+            parser.set(section, 'Backups', ':'.join(backups))
+
             # if there are no backups, delete the ``backups`` line
-            pass #TODO
+            # and the backups directory
+            if not backups:
+                parser.remove(section, 'Backups')
+                backups_dir = os.path.join(self.profile_dir, self.backups_dir)
+                if not os.listdir(backups_dir):
+                    shutil.rmtree(backups_dir)
 
     def temporary(self):
         """make a temporary profile"""