Back to index

obnam  1.1
forget_plugin.py
Go to the documentation of this file.
00001 # Copyright (C) 2010  Lars Wirzenius
00002 #
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 #
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 #
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import datetime
00018 
00019 import obnamlib
00020 
00021 
00022 class ForgetPlugin(obnamlib.ObnamPlugin):
00023 
00024     '''Forget generations.'''
00025     
00026     def enable(self):
00027         self.app.add_subcommand('forget', self.forget,
00028                                 arg_synopsis='[GENERATION]...')
00029         self.app.settings.string(['keep'],
00030                                   'policy for what generations to keep '
00031                                   'when forgetting')
00032 
00033     def forget(self, args):
00034         '''Forget (remove) specified backup generations.'''
00035         self.app.settings.require('repository')
00036         self.app.settings.require('client-name')
00037         
00038         self.app.ts['gen'] = None
00039         self.app.ts['gens'] = []
00040         self.app.ts.format('forgetting generations: %Index(gen,gens) done')
00041 
00042         self.repo = self.app.open_repository()
00043         self.repo.lock_client(self.app.settings['client-name'])
00044         self.repo.lock_shared()
00045 
00046         self.app.dump_memory_profile('at beginning')
00047         if args:
00048             self.app.ts['gens'] = args
00049             for genspec in args:
00050                 self.app.ts['gen'] = genspec
00051                 genid = self.repo.genspec(genspec)
00052                 self.app.ts.notify('Forgetting generation %s' % genid)
00053                 self.remove(genid)
00054                 self.app.dump_memory_profile('after removing %s' % genid)
00055         elif self.app.settings['keep']:
00056             genlist = []
00057             dt = datetime.datetime(1970, 1, 1, 0, 0, 0)
00058             for genid in self.repo.list_generations():
00059                 start, end = self.repo.get_generation_times(genid)
00060                 genlist.append((genid, dt.fromtimestamp(end)))
00061 
00062             fp = obnamlib.ForgetPolicy()
00063             rules = fp.parse(self.app.settings['keep'])
00064             keeplist = fp.match(rules, genlist)
00065             keepids = set(genid for genid, dt in keeplist)
00066             removeids = [genid 
00067                          for genid, dt in genlist 
00068                          if genid not in keepids]
00069 
00070             self.app.ts['gens'] = removeids
00071             for genid in removeids:
00072                 self.app.ts['gen'] = genid
00073                 self.remove(genid)
00074                 self.app.dump_memory_profile('after removing %s' % genid)
00075 
00076         self.repo.commit_client()
00077         self.repo.commit_shared()
00078         self.app.dump_memory_profile('after committing')
00079         self.repo.fs.close()
00080         self.app.ts.finish()
00081 
00082     def remove(self, genid):
00083         if self.app.settings['pretend']:
00084             self.app.ts.notify('Pretending to remove generation %s' % genid)
00085         else:
00086             self.repo.remove_generation(genid)
00087