diff options
| author | Barry Warsaw | 2008-12-20 15:41:16 -0500 |
|---|---|---|
| committer | Barry Warsaw | 2008-12-20 15:41:16 -0500 |
| commit | dfccf2b7d2b5749c86267645aaf870f128dab2d8 (patch) | |
| tree | 205cc5253b2b5462e9e5409b09b0a7cea7f30a7b /mailman/core | |
| parent | 33a924c56669b12e268bce5df68f598b690cdcf0 (diff) | |
| download | mailman-dfccf2b7d2b5749c86267645aaf870f128dab2d8.tar.gz mailman-dfccf2b7d2b5749c86267645aaf870f128dab2d8.tar.zst mailman-dfccf2b7d2b5749c86267645aaf870f128dab2d8.zip | |
Lots of changes to make bin/withlist work under a buildout environment.
mailman.configuration -> mailman.config.config
mailman.initialize -> mailma.core.initialize
mailman.loginit -> mailman.core.logging (yay future absolute imports!)
Convert all configurations to lazr.config, though some legacy still remains,
and I haven't been able to remove Defaults.py yet. Added as_boolean() and
as_log_level() helpers for explicit type conversion. Added a schema.cfg.
Diffstat (limited to 'mailman/core')
| -rw-r--r-- | mailman/core/chains.py | 4 | ||||
| -rw-r--r-- | mailman/core/initialize.py | 116 | ||||
| -rw-r--r-- | mailman/core/logging.py | 109 | ||||
| -rw-r--r-- | mailman/core/pipelines.py | 2 | ||||
| -rw-r--r-- | mailman/core/rules.py | 2 | ||||
| -rw-r--r-- | mailman/core/styles.py | 2 |
6 files changed, 230 insertions, 5 deletions
diff --git a/mailman/core/chains.py b/mailman/core/chains.py index bebbf5bee..812302d3f 100644 --- a/mailman/core/chains.py +++ b/mailman/core/chains.py @@ -17,11 +17,11 @@ """Application support for chain processing.""" +__metaclass__ = type __all__ = [ 'initialize', 'process', ] -__metaclass__ = type from mailman.chains.accept import AcceptChain @@ -30,7 +30,7 @@ from mailman.chains.discard import DiscardChain from mailman.chains.headers import HeaderMatchChain from mailman.chains.hold import HoldChain from mailman.chains.reject import RejectChain -from mailman.configuration import config +from mailman.config import config from mailman.interfaces import LinkAction diff --git a/mailman/core/initialize.py b/mailman/core/initialize.py new file mode 100644 index 000000000..16dcecf94 --- /dev/null +++ b/mailman/core/initialize.py @@ -0,0 +1,116 @@ +# Copyright (C) 2006-2008 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman 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 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman 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 +# GNU Mailman. If not, see <http://www.gnu.org/licenses/>. + +"""Initialize all global state. + +Every entrance into the Mailman system, be it by command line, mail program, +or cgi, must call the initialize function here in order for the system's +global state to be set up properly. Typically this is called after command +line argument parsing, since some of the initialization behavior is controlled +by the command line arguments. +""" + +import os + +from zope.interface.interface import adapter_hooks +from zope.interface.verify import verifyObject + +import mailman.config.config +import mailman.core.logging + +from mailman.core.plugins import get_plugin +from mailman.interfaces import IDatabase + + + +# These initialization calls are separated for the testing framework, which +# needs to do some internal calculations after config file loading and log +# initialization, but before database initialization. Generally all other +# code will just call initialize(). + +def initialize_1(config_path, propagate_logs): + """First initialization step. + + * The configuration system + * Run-time directories + * The logging subsystem + + :param config_path: The path to the configuration file. + :type config_path: string + :param propagate_logs: Should the log output propagate to stderr? + :type propagate_logs: boolean + """ + # By default, set the umask so that only owner and group can read and + # write our files. Specifically we must have g+rw and we probably want + # o-rwx although I think in most cases it doesn't hurt if other can read + # or write the files. Note that the Pipermail archive has more + # restrictive permissions in order to handle private archives, but it + # handles that correctly. + os.umask(007) + mailman.config.config.load(config_path) + # Create the queue and log directories if they don't already exist. + mailman.config.config.ensure_directories_exist() + mailman.core.logging.initialize(propagate_logs) + + +def initialize_2(debug=False): + """Second initialization step. + + * Archivers + * Rules + * Chains + * Pipelines + * Commands + + :param debug: Should the database layer be put in debug mode? + :type debug: boolean + """ + database_plugin = get_plugin('mailman.database') + # Instantiate the database plugin, ensure that it's of the right type, and + # initialize it. Then stash the object on our configuration object. + database = database_plugin() + verifyObject(IDatabase, database) + database.initialize(debug) + mailman.config.config.db = database + # Initialize the rules and chains. Do the imports here so as to avoid + # circular imports. + from mailman.app.commands import initialize as initialize_commands + from mailman.archiving import initialize as initialize_archivers + from mailman.core.chains import initialize as initialize_chains + from mailman.core.pipelines import initialize as initialize_pipelines + from mailman.core.rules import initialize as initialize_rules + initialize_archivers() + initialize_rules() + initialize_chains() + initialize_pipelines() + initialize_commands() + + +def initialize_3(): + """Third initialization step. + + * Adapters + """ + from mailman.app.registrar import adapt_domain_to_registrar + adapter_hooks.append(adapt_domain_to_registrar) + + + +def initialize(config_path=None, propagate_logs=False): + initialize_1(config_path, propagate_logs) + initialize_2() + initialize_3() diff --git a/mailman/core/logging.py b/mailman/core/logging.py new file mode 100644 index 000000000..608b59c2c --- /dev/null +++ b/mailman/core/logging.py @@ -0,0 +1,109 @@ +# Copyright (C) 2006-2008 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman 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 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman 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 +# GNU Mailman. If not, see <http://www.gnu.org/licenses/>. + +"""Logging initialization, using Python's standard logging package.""" + +from __future__ import absolute_import + +import os +import sys +import codecs +import logging +import ConfigParser + +from mailman.config import config +from mailman.config.helpers import as_boolean, as_log_level + + +_handlers = [] + + + +class ReopenableFileHandler(logging.Handler): + def __init__(self, filename): + self._filename = filename + self._stream = self._open() + logging.Handler.__init__(self) + + def _open(self): + return codecs.open(self._filename, 'a', 'utf-8') + + def flush(self): + if self._stream: + self._stream.flush() + + def emit(self, record): + # It's possible for the stream to have been closed by the time we get + # here, due to the shut down semantics. This mostly happens in the + # test suite, but be defensive anyway. + stream = (self._stream if self._stream else sys.stderr) + try: + msg = self.format(record) + fs = '%s\n' + try: + stream.write(fs % msg) + except UnicodeError: + stream.write(fs % msg.encode('string-escape')) + self.flush() + except: + self.handleError(record) + + def close(self): + self.flush() + self._stream.close() + self._stream = None + logging.Handler.close(self) + + def reopen(self): + self._stream.close() + self._stream = self._open() + + + +def initialize(propagate=False): + # First, find the root logger and configure the logging subsystem. + # Initialize the root logger, then create a formatter for all the sublogs. + logging.basicConfig(format=config.logging.root.format, + datefmt=config.logging.root.datefmt, + level=as_log_level(config.logging.root.level)) + # Create the subloggers + for logger_config in config.logger_configs: + logger_name = 'mailman.' + logger_config.name.split('.')[-1] + log = logging.getLogger(logger_name) + # Get settings from log configuration file (or defaults). + log_format = logger_config.format + log_datefmt = logger_config.datefmt + # Propagation to the root logger is how we handle logging to stderr + # when the qrunners are not run as a subprocess of mailmanctl. + log.propagate = as_boolean(logger_config.propagate) + # Set the logger's level. + log.setLevel(as_log_level(logger_config.level)) + # Create a formatter for this logger, then a handler, and link the + # formatter to the handler. + formatter = logging.Formatter(fmt=log_format, datefmt=log_datefmt) + path_str = logger_config.path + path_abs = os.path.normpath(os.path.join(config.LOG_DIR, path_str)) + handler = ReopenableFileHandler(path_abs) + _handlers.append(handler) + handler.setFormatter(formatter) + log.addHandler(handler) + + + +def reopen(): + for handler in _handlers: + handler.reopen() diff --git a/mailman/core/pipelines.py b/mailman/core/pipelines.py index c790901b2..7c177b511 100644 --- a/mailman/core/pipelines.py +++ b/mailman/core/pipelines.py @@ -27,7 +27,7 @@ __all__ = [ from zope.interface import implements from zope.interface.verify import verifyObject -from mailman.configuration import config +from mailman.config import config from mailman.core.plugins import get_plugins from mailman.i18n import _ from mailman.interfaces import IHandler, IPipeline diff --git a/mailman/core/rules.py b/mailman/core/rules.py index 2c1e4fb3b..28f58d76e 100644 --- a/mailman/core/rules.py +++ b/mailman/core/rules.py @@ -26,7 +26,7 @@ __all__ = [ from zope.interface import implements from zope.interface.verify import verifyObject -from mailman.configuration import config +from mailman.config import config from mailman.core.plugins import get_plugins from mailman.interfaces import IRule diff --git a/mailman/core/styles.py b/mailman/core/styles.py index 96104c204..d264143f5 100644 --- a/mailman/core/styles.py +++ b/mailman/core/styles.py @@ -30,7 +30,7 @@ from zope.interface import implements from zope.interface.verify import verifyObject from mailman import Utils -from mailman.configuration import config +from mailman.config import config from mailman.core.plugins import get_plugins from mailman.i18n import _ from mailman.interfaces import ( |
