changeset 65:1cd3e3c71bff

determine which backup to use from the given date
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 07 May 2010 14:14:17 -0700
parents 18f16bd1ba6b
children 67dc95a355cc
files profilemanager/manager.py setup.py
diffstat 2 files changed, 40 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/profilemanager/manager.py	Fri May 07 12:23:49 2010 -0700
+++ b/profilemanager/manager.py	Fri May 07 14:14:17 2010 -0700
@@ -7,9 +7,10 @@
 import string
 import time
 import ConfigParser
+from datetime import datetime
+from dateutil.parser import parse
 from random import Random
 from utils import format_tabular
-#from ConfigParser import NoOptionError
 from ConfigParser import SafeConfigParser as ConfigParser
 
 # Changes to profiles.ini:
@@ -27,6 +28,11 @@
         Exception.__init__(self,
                            'Profile %s not found in %s' % (profile, config))
 
+class NoBackupError(Exception):
+    """
+    exception when a sought backup is not found
+    """
+
 class ProfileManager(object):
 
     backups_dir = 'backups' # directory for backups relative to profile_dir
@@ -170,7 +176,6 @@
         - datestamp: format of date; otherwise, seconds since epoch
         """
         if profile is None: # all profiles
-            # XXX this should probably be recursive
             retval = {}
             profiles_dict = self.profiles_dict()
             for profile in profiles_dict:
@@ -207,15 +212,46 @@
         """
         restore the profile from a backup
         the most recent backup is used unless `date` is given
-        - date : date to restore from
+        - date : date to restore from (will not delete)
         - delete : delete the backup after restoration
         """
 
+        assert not (date and delete), 'date and delete cannot be used in conjunction'
+
         # get the possible backups
         backups = self.backups(profile)
         # TODO: check to see if these all exist (print warnings if not)
 
+        # determine the backup to use
+        if date:
+
+            if isinstance(date, basestring):
+
+                # test for s since epoch
+                seconds_snice_epoch = None
+                try:
+                    seconds_since_epoch = int(date)
+                    
+                except ValueError:
+                    pass
+                if seconds_since_epoch and seconds_since_epoch > time.localtime().tm_year:
+                    date = seconds_since_epoch
+                else:
+                    date = parse(date) # parse date from string
+
+            if isinstance(date, datetime):
+                # convert to s since epoch
+                date = time.mktime(date.timetuple())
+            
+            for backup in backups:
+                raise NotImplementedError
+        else:
+            if not backups:
+                raise NoBackupError("No backups for profile %s in %s" % (profile, self.profiles))
+            backups = backups[0][0]
+
         # restore the backup over ``profile``
+        
 
         if delete: # delete the backup
             # delete the directory
--- a/setup.py	Fri May 07 12:23:49 2010 -0700
+++ b/setup.py	Fri May 07 14:14:17 2010 -0700
@@ -19,6 +19,7 @@
       zip_safe=False,
       install_requires=[
           # -*- Extra requirements: -*-
+        'python-dateutil'
       ],
       entry_points="""
       # -*- Entry points: -*-