main: Add configuration infrastructure.
authorMatthijs Kooijman <matthijs@stdin.nl>
Tue, 11 May 2010 19:06:56 +0000 (21:06 +0200)
committerMatthijs Kooijman <matthijs@stdin.nl>
Mon, 17 May 2010 17:06:58 +0000 (19:06 +0200)
src/backupninja
src/lib/backupninja/config.py [new file with mode: 0644]

index df7d3c5bd5441376399d3790313858daaa39da87..48330b3a6a28943e8167b4447aa844ff36141649 100755 (executable)
@@ -27,6 +27,7 @@ import logging
 import sys
 
 from backupninja.log import setup_logging
 import sys
 
 from backupninja.log import setup_logging
+from backupninja import config
 
 log = logging.getLogger()
 
 
 log = logging.getLogger()
 
@@ -46,8 +47,15 @@ def main(argv):
     setup_logging(options)
 
     # Load config file
     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()
 
     # Process command
     parser.print_help()
 
+    return 0
+
 if __name__ == '__main__':
     sys.exit(main(sys.argv))
 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 (file)
index 0000000..af3d4da
--- /dev/null
@@ -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 <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.
+
+""" 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