From 38783916e32faa5653f43620f9e31acc9418721e Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Tue, 11 May 2010 21:06:56 +0200 Subject: [PATCH] main: Add configuration infrastructure. --- src/backupninja | 8 ++++ src/lib/backupninja/config.py | 76 +++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/lib/backupninja/config.py diff --git a/src/backupninja b/src/backupninja index df7d3c5..48330b3 100755 --- a/src/backupninja +++ b/src/backupninja @@ -27,6 +27,7 @@ import logging import sys from backupninja.log import setup_logging +from backupninja import config log = logging.getLogger() @@ -46,8 +47,15 @@ def main(argv): setup_logging(options) # Load config file + global_conf = config.get_global_config(options) + if global_conf is None: + # Error was already logged + return 1 + # Process command parser.print_help() + return 0 + if __name__ == '__main__': sys.exit(main(sys.argv)) diff --git a/src/lib/backupninja/config.py b/src/lib/backupninja/config.py new file mode 100644 index 0000000..af3d4da --- /dev/null +++ b/src/lib/backupninja/config.py @@ -0,0 +1,76 @@ +# -*- 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 +# +# 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. + +""" Load configuration for backupninja and configured actions """ + +import os, ConfigParser + +default_config_dir = '/etc/backupninja' +default_global_config = 'backupninja.conf' +default_actions_dir = 'actions' + +import logging as log + +def get_global_config(opts): + """ + Returns the global configuration, in a SafeConfigParser object. + If the configuration file can not be found, logs an error and + returns None. + + opts are the parsed commandline options. + """ + global_config = os.path.join(default_config_dir, default_global_config) + return _load_config(global_config) + +def get_action_config(opts, action): + """ + Returns the configuration for the named action, in a + SafeConfigParser object. If the configuration file can not be found, + logs an error and returns None. + + opts are the parsed commandline options. + """ + actions_dir = os.path.join(default_config_dir, default_actions_dir) + return _load_config(os.path.join(actions_dir, action)) + +def list_actions(opts): + """ + Lists all actions defined in the configuration directory. Returns a + list of action names that can be passed to get_action_config. + opts are the parsed commandline options. + """ + actions_dir = os.path.join(default_config_dir, default_actions_dir) + return os.listdir(actions_dir) + +def _load_config(filename): + # Open a file and read it + config = ConfigParser.SafeConfigParser() + log.debug('Reading config file "%s"', filename) + try: + file = open(filename, 'r') + except IOError, e: + # Log the error and return None + msg = 'Unable to open configuration file "%s": %s' % (filename, e) + log.error(msg) + return None + + config.readfp(file) + return config -- 2.30.2