--- /dev/null
+# -*- mode: python; sh-basic-offset: 4; indent-tabs-mode: nil; -*-
+# vim: set filetype=python sw=4 sts=4 expandtab autoindent:
+#
+# Backupninja python reimplementation, based on original backupninja program
+# by riseup.net.
+# Copyright (C) 2010 Matthijs Kooijman <matthijs@stdin.nl>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+""" Running backup actions """
+
+from backupninja import config
+from backupninja import handlers
+
+def run_all_actions(opts, global_config):
+ """
+ Run all defined actions.
+
+ opts are the parsed commandline options, global_config is the parsed
+ global configuration.
+ """
+ actions = config.list_actions(opts)
+
+ for action in actions:
+ run_action(action, opts, global_config)
+
+def run_action(action, opts, global_config):
+ """
+ Run a single action. opts are the parsed commandline options,
+ global_config is the parsed global configuration.
+ """
+ # Split the action filename
+ parts = action.split('.', 2)
+ if (len(parts) < 2):
+ log.error('Invalid action filename: "%s". Should be in the form name.type, where type is a valid handler.')
+ return
+ (action_name, action_ty) = parts
+
+ # Get the config for this action
+ action_config = config.get_action_config(opts, action)
+ # Create a handler for this action
+ handler = handlers.create_handler(action_ty, action_config)
+
+ if handler:
+ # Run the handler
+ handler.run()
+ handler.finish()
+ # Silently skip invalid handlers, create_handler will have
+ # logged an error
+#
+# Backupninja python reimplementation, based on original backupninja program
+# by riseup.net.
+# Copyright (C) 2010 Matthijs Kooijman <matthijs@stdin.nl>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License along
+# with this program; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+
+""" Handler superclass with common functionality """
+
+import sys
+import logging as log
+
+class Handler(object):
+ def __init__(self, conf):
+ self.conf = conf
+
+ def run(self):
+ """
+ Run this handler for a single target. Override this method
+ in a subclass
+ """
+ pass
+
+ def finish(self):
+ """
+ Called when all targets have been processed. Can be overridden
+ in a subclass.
+ """
+ pass
+
+def create_handler(ty, conf):
+ """
+ Create a new (subclass of) Handler object for an action with the
+ given type. conf is the configuration to pass to the handler.
+
+ If the handler cannot be loaded, it is logged and None is returned
+ (but any exceptions raised by the handler code itself are not
+ handled).
+ """
+ modname = 'backupninja.handlers.%s' % ty
+ # Load the handler if it is not loaded yet
+ if not modname in sys.modules:
+ try:
+ __import__(modname, globals(), locals(), [])
+ except ImportError, e:
+ log.error('Cannot load action handler for "%s": %s'
+ , ty, e)
+ return None
+ # Get the module from the module table
+ module = sys.modules[modname]
+
+ # Check that the module has a "handler" top level function, which
+ # should create a new Handler object.
+ if not hasattr(module, 'handler'):
+ log.error('Action handler for "%s" (in "%s) is not valid: it '
+ 'does not have a "handler" top level function.'
+ , ty, module.__file__)
+ return None
+
+ # Call the "handler" function to create the actual handler
+ handler = module.handler(conf)
+
+ # Check if the handler returned is really a subclass of Handler
+ if not isinstance(handler, Handler):
+ log.error('Action handler for "%s" (in "%s) is not valid: it '
+ 'does not return a subclass of backupninja.handlers.Handler.'
+ , ty, module.__file__)
+ return None
+ return handler