diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/mailman/app/docs/plugins.rst | 50 | ||||
| -rw-r--r-- | src/mailman/bin/mailman.py | 6 | ||||
| -rw-r--r-- | src/mailman/bin/master.py | 2 | ||||
| -rw-r--r-- | src/mailman/bin/tests/test_mailman.py | 18 | ||||
| -rw-r--r-- | src/mailman/config/schema.cfg | 9 | ||||
| -rw-r--r-- | src/mailman/core/initialize.py | 28 | ||||
| -rw-r--r-- | src/mailman/rest/docs/systemconf.rst | 2 | ||||
| -rw-r--r-- | src/mailman/rest/tests/test_systemconf.py | 2 | ||||
| -rw-r--r-- | src/mailman/utilities/plugins.py | 2 |
9 files changed, 94 insertions, 25 deletions
diff --git a/src/mailman/app/docs/plugins.rst b/src/mailman/app/docs/plugins.rst index a66e4413d..4430179ae 100644 --- a/src/mailman/app/docs/plugins.rst +++ b/src/mailman/app/docs/plugins.rst @@ -117,7 +117,6 @@ initialization process. ... def rest_object(self): ... pass ... """, file=fp) - >>> fp.close() Running the hooks ----------------- @@ -143,12 +142,12 @@ script that will produce no output to force the hooks to run. >>> import subprocess >>> from mailman.testing.layers import ConfigLayer - >>> def call(): + >>> def call(cfg_path, python_path): ... exe = os.path.join(os.path.dirname(sys.executable), 'mailman') ... env = os.environ.copy() ... env.update( - ... MAILMAN_CONFIG_FILE=config_path, - ... PYTHONPATH=config_directory, + ... MAILMAN_CONFIG_FILE=cfg_path, + ... PYTHONPATH=python_path, ... ) ... test_cfg = os.environ.get('MAILMAN_EXTRA_TESTING_CFG') ... if test_cfg is not None: @@ -161,10 +160,51 @@ script that will produce no output to force the hooks to run. ... stdout, stderr = proc.communicate() ... assert proc.returncode == 0, stderr ... print(stdout) + ... print(stderr) - >>> call() + >>> call(config_path, config_directory) pre-hook: 1 post-hook: 2 <BLANKLINE> + <BLANKLINE> >>> os.remove(config_path) + +Deprecated hooks +---------------- + +The old-style `pre_hook` and `post_hook` callables are deprecated and are no +longer called upon startup. +:: + + >>> deprecated_hook_path = os.path.join(config_directory, 'deprecated_hooks.py') + >>> with open(deprecated_hook_path, 'w') as fp: + ... print("""\ + ... def do_something(): + ... print("does something") + ... + ... def do_something_else(): + ... print("does something else") + ... + ... """, file=fp) + + >>> deprecated_config_path = os.path.join(config_directory, 'deprecated.cfg') + >>> with open(deprecated_config_path, 'w') as fp: + ... print("""\ + ... [meta] + ... extends: test.cfg + ... + ... [mailman] + ... pre_hook: deprecated_hooks.do_something + ... post_hook: deprecated_hooks.do_something_else + ... """, file=fp) + + >>> call(deprecated_config_path, config_directory) + does something + does something else + <BLANKLINE> + ... UserWarning: The pre_hook configuration value has been replaced by the plugins infrastructure. ... + ... UserWarning: The post_hook configuration value has been replaced by the plugins infrastructure. ... + <BLANKLINE> + + >>> os.remove(deprecated_config_path) diff --git a/src/mailman/bin/mailman.py b/src/mailman/bin/mailman.py index ece223180..f8006218d 100644 --- a/src/mailman/bin/mailman.py +++ b/src/mailman/bin/mailman.py @@ -44,9 +44,9 @@ class Subcommands(click.MultiCommand): self._commands) self._loaded = True - def list_commands(self, ctx): + def list_commands(self, ctx): # pragma: nocover self._load() - return sorted(self._commands) # pragma: nocover + return sorted(self._commands) def get_command(self, ctx, name): self._load() @@ -88,7 +88,7 @@ class Subcommands(click.MultiCommand): def initialize_config(ctx, param, value): - if ctx.resilient_parsing: + if ctx.resilient_parsing: # pragma: nocover return initialize(value) diff --git a/src/mailman/bin/master.py b/src/mailman/bin/master.py index a23fb8e0b..ccaa16398 100644 --- a/src/mailman/bin/master.py +++ b/src/mailman/bin/master.py @@ -405,7 +405,7 @@ class Loop: except InterruptedError: # pragma: nocover # If the system call got interrupted, just restart it. continue - if pid not in self._kids: + if pid not in self._kids: # pragma: nocover # Not a runner subprocess, maybe a plugin started one # ignore it continue diff --git a/src/mailman/bin/tests/test_mailman.py b/src/mailman/bin/tests/test_mailman.py index 54ee54bce..196437291 100644 --- a/src/mailman/bin/tests/test_mailman.py +++ b/src/mailman/bin/tests/test_mailman.py @@ -27,6 +27,7 @@ from mailman.config import config from mailman.database.transaction import transaction from mailman.testing.layers import ConfigLayer from mailman.utilities.datetime import now +from pkg_resources import resource_filename from unittest.mock import patch @@ -36,7 +37,19 @@ class TestMailmanCommand(unittest.TestCase): def setUp(self): self._command = CliRunner() - def test_mailman_command_without_subcommand_prints_help(self): + def test_mailman_command_config(self): + config_path = resource_filename('mailman.testing', 'testing.cfg') + with patch('mailman.bin.mailman.initialize') as init: + self._command.invoke(main, ('-C', config_path, 'info')) + init.assert_called_once_with(config_path) + + def test_mailman_command_no_config(self): + with patch('mailman.bin.mailman.initialize') as init: + self._command.invoke(main, ('info',)) + init.assert_called_once_with(None) + + @patch('mailman.bin.mailman.initialize') + def test_mailman_command_without_subcommand_prints_help(self, mock): # Issue #137: Running `mailman` without a subcommand raises an # AttributeError. result = self._command.invoke(main) @@ -46,7 +59,8 @@ class TestMailmanCommand(unittest.TestCase): # command line. self.assertEqual(lines[0], 'Usage: main [OPTIONS] COMMAND [ARGS]...') - def test_mailman_command_with_bad_subcommand_prints_help(self): + @patch('mailman.bin.mailman.initialize') + def test_mailman_command_with_bad_subcommand_prints_help(self, mock): # Issue #137: Running `mailman` without a subcommand raises an # AttributeError. result = self._command.invoke(main, ('not-a-subcommand',)) diff --git a/src/mailman/config/schema.cfg b/src/mailman/config/schema.cfg index a55c37ff4..742bceae1 100644 --- a/src/mailman/config/schema.cfg +++ b/src/mailman/config/schema.cfg @@ -74,10 +74,17 @@ html_to_plain_text_command: /usr/bin/lynx -dump $filename # unpredictable. listname_chars: [-_.0-9a-z] +# Deprecated, callable run before DB initialization. +pre_hook: + +# Deprecated, callable run after DB initialization. +post_hook: + + [plugin.master] # Plugin package # It's sub packages will be searched for components. -# - commands for IEmailCommand +# - commands for IEmailCommand and ICliSubCommand # - chains for IChain # - rules for IRule # - pipelines for IPipeline diff --git a/src/mailman/core/initialize.py b/src/mailman/core/initialize.py index a1314b51e..830bd9b84 100644 --- a/src/mailman/core/initialize.py +++ b/src/mailman/core/initialize.py @@ -31,7 +31,9 @@ import traceback import mailman.config.config import mailman.core.logging +from mailman.core.i18n import _ from mailman.interfaces.database import IDatabaseFactory +from mailman.utilities.modules import call_name from pkg_resources import resource_string as resource_bytes from public import public from zope.component import getUtility @@ -152,27 +154,28 @@ def initialize_2(debug=False, propagate_logs=None, testing=False): initialize_plugins() # Check for deprecated features in config. config = mailman.config.config - if 'pre_hook' in config.mailman: # pragma: no cover + if config.mailman.pre_hook: # pragma: nocover warnings.warn( - 'The pre_hook configuration value has been replaced by the ' - 'plugins infrastructure.', DeprecationWarning) + _('The pre_hook configuration value has been replaced by the ' + 'plugins infrastructure.'), UserWarning) + call_name(config.mailman.pre_hook) # Run the plugin pre_hooks, if one fails, disable the offending plugin. for name in list(config.plugins.keys()): plugin = config.plugins[name] try: plugin.pre_hook() - except BaseException: # pragma: no cover + except: # pragma: nocover traceback.print_exc() - warnings.warn('Plugin {} failed to run its pre_hook,' - 'it will be disabled and its components' - 'wont be loaded.'.format(name), RuntimeWarning) + warnings.warn(_('Plugin $name failed to run its pre_hook,' + 'it will be disabled and its components' + 'wont be loaded.'), RuntimeWarning) # It failed, push disabling overlay to config. This will stop # components from being loaded. config.push(name + '_error', """\ [plugin.{}] enable: no """.format(name)) - # And forget about it. This will stop running its pot_hook. + # And forget about it. This will stop running its post_hook. del config.plugins[name] # Instantiate the database class, ensure that it's of the right type, and # initialize it. Then stash the object on our configuration object. @@ -199,14 +202,15 @@ def initialize_3(): """ # Run the plugin post_hooks config = mailman.config.config - if 'post_hook' in config.mailman: # pragma: no cover + if config.mailman.post_hook: # pragma: nocover warnings.warn( - 'The post_hook configuration value has been replaced by the ' - 'plugins infrastructure.', DeprecationWarning) + _('The post_hook configuration value has been replaced by the ' + 'plugins infrastructure.'), UserWarning) + call_name(config.mailman.post_hook) for plugin in config.plugins.values(): try: plugin.post_hook() - except: # pragma: no cover + except: # pragma: nocover # A post_hook may fail, here we just hope for the best that the # plugin can work even if it post_hook failed as it's components # are already loaded. diff --git a/src/mailman/rest/docs/systemconf.rst b/src/mailman/rest/docs/systemconf.rst index f5c9b691a..a65b81ab8 100644 --- a/src/mailman/rest/docs/systemconf.rst +++ b/src/mailman/rest/docs/systemconf.rst @@ -24,6 +24,8 @@ You can also get all the values for a particular section, such as the listname_chars: [-_.0-9a-z] noreply_address: noreply pending_request_life: 3d + post_hook: + pre_hook: self_link: http://localhost:9001/3.0/system/configuration/mailman sender_headers: from from_ reply-to sender site_owner: noreply@example.com diff --git a/src/mailman/rest/tests/test_systemconf.py b/src/mailman/rest/tests/test_systemconf.py index fe5d951a8..59f0619a3 100644 --- a/src/mailman/rest/tests/test_systemconf.py +++ b/src/mailman/rest/tests/test_systemconf.py @@ -46,6 +46,8 @@ class TestSystemConfiguration(unittest.TestCase): listname_chars='[-_.0-9a-z]', noreply_address='noreply', pending_request_life='3d', + post_hook='', + pre_hook='', self_link='http://localhost:9001/3.0/system/configuration/mailman', sender_headers='from from_ reply-to sender', site_owner='noreply@example.com', diff --git a/src/mailman/utilities/plugins.py b/src/mailman/utilities/plugins.py index 3fb5e49bc..aad909056 100644 --- a/src/mailman/utilities/plugins.py +++ b/src/mailman/utilities/plugins.py @@ -70,7 +70,7 @@ def add_pluggable_components(subpackage, interface, mapping): """ for component in find_pluggable_components(subpackage, interface): if component.name in mapping: - raise RuntimeError( # pragma: no cover + raise RuntimeError( # pragma: nocover 'Duplicate key "{}" found in {}; previously {}'.format( component.name, component, mapping[component.name])) mapping[component.name] = component |
