comparison profilemanager/manager.py @ 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
comparison
equal deleted inserted replaced
65:1cd3e3c71bff 66:67dc95a355cc
3 """ 3 """
4 4
5 import os 5 import os
6 import shutil 6 import shutil
7 import string 7 import string
8 import tempfile
8 import time 9 import time
9 import ConfigParser 10 import ConfigParser
10 from datetime import datetime 11 from datetime import datetime
11 from dateutil.parser import parse 12 from dateutil.parser import parse
12 from random import Random 13 from random import Random
216 - delete : delete the backup after restoration 217 - delete : delete the backup after restoration
217 """ 218 """
218 219
219 assert not (date and delete), 'date and delete cannot be used in conjunction' 220 assert not (date and delete), 'date and delete cannot be used in conjunction'
220 221
222 # get the path to the profile
223 path = self.path(profile)
224
221 # get the possible backups 225 # get the possible backups
222 backups = self.backups(profile) 226 backups = self.backups(profile)
223 # TODO: check to see if these all exist (print warnings if not) 227 # TODO: check to see if these all exist (print warnings if not)
224 228
225 # determine the backup to use 229 # determine the backup to use
246 for backup in backups: 250 for backup in backups:
247 raise NotImplementedError 251 raise NotImplementedError
248 else: 252 else:
249 if not backups: 253 if not backups:
250 raise NoBackupError("No backups for profile %s in %s" % (profile, self.profiles)) 254 raise NoBackupError("No backups for profile %s in %s" % (profile, self.profiles))
251 backups = backups[0][0] 255 backup = backups[0]
256
257 # determine path of the backup
258 if os.path.isabs(backup[0])
259 backup_path = backup
260 else:
261 backup_path = os.path.join(self.profile_dir, self.backups_dir, backup)
252 262
253 # restore the backup over ``profile`` 263 # restore the backup over ``profile``
254 264 # make a backup copy first to avoid data loss
265 tempdir = tempfile.mktemp()
266 shutil.move(path, tempdir)
267 try:
268 shutil.copytree(backup_path, path)
269 except Exception, e:
270 shutil.rmtree(path)
271 shutil.move(tempdir, path)
272 raise IOError("Couldn't restore backup: %s" % str(e))
273 shutil.rmtree(tempdir)
274 # TODO: (optionally) make a backup of the current profile before restore
255 275
256 if delete: # delete the backup 276 if delete: # delete the backup
277
257 # delete the directory 278 # delete the directory
279 shutil.rmtree(backup_path)
280
258 # delete the entry from ``profiles.ini`` 281 # delete the entry from ``profiles.ini``
282 parser = self.parser()
283 section = self.section(profile)
284 backups = parser.get(section, 'Backups')
285 backups = [ i for i in backups if i != backup[0] ]
286 parser.set(section, 'Backups', ':'.join(backups))
287
259 # if there are no backups, delete the ``backups`` line 288 # if there are no backups, delete the ``backups`` line
260 pass #TODO 289 # and the backups directory
290 if not backups:
291 parser.remove(section, 'Backups')
292 backups_dir = os.path.join(self.profile_dir, self.backups_dir)
293 if not os.listdir(backups_dir):
294 shutil.rmtree(backups_dir)
261 295
262 def temporary(self): 296 def temporary(self):
263 """make a temporary profile""" 297 """make a temporary profile"""
264 raise NotImplementedError 298 raise NotImplementedError
265 299