Back to index

obnam  1.1
force_lock_plugin.py
Go to the documentation of this file.
00001 # Copyright (C) 2009, 2010, 2011  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 logging
00018 import os
00019 
00020 import obnamlib
00021 
00022 
00023 class ForceLockPlugin(obnamlib.ObnamPlugin):
00024 
00025     def enable(self):
00026         self.app.add_subcommand('force-lock', self.force_lock)
00027 
00028     def force_lock(self, args):
00029         '''Force a locked repository to be open.'''
00030         self.app.settings.require('repository')
00031         self.app.settings.require('client-name')
00032 
00033         repourl = self.app.settings['repository']
00034         client_name = self.app.settings['client-name']
00035         logging.info('Forcing lock')
00036         logging.info('Repository: %s' % repourl)
00037         logging.info('Client: %s' % client_name)
00038 
00039         try:
00040             repo = self.app.open_repository()
00041         except OSError, e:
00042             raise obnamlib.Error('Repository does not exist ' 
00043                                   'or cannot be accessed.\n' +
00044                                   str(e))
00045 
00046         all_clients = repo.list_clients()
00047         if client_name not in all_clients:
00048             msg = 'Client does not exist in repository.'
00049             logging.warning(msg)
00050             self.app.output.write('Warning: %s\n' % msg)
00051             return
00052 
00053         all_dirs = ['clientlist', 'chunksums', 'chunklist', 'chunks', '.']
00054         for client_name in all_clients:
00055             client_id = repo.clientlist.get_client_id(client_name)
00056             client_dir = repo.client_dir(client_id)
00057             all_dirs.append(client_dir)
00058 
00059         for one_dir in all_dirs:
00060             lockname = os.path.join(one_dir, 'lock')
00061             if repo.fs.exists(lockname):
00062                 logging.info('Removing lockfile %s' % lockname)
00063                 repo.fs.remove(lockname)
00064             else:
00065                 logging.info('%s is not locked' % one_dir)
00066 
00067         repo.fs.close()
00068 
00069         return 0