summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarry Warsaw2016-04-22 15:24:25 -0400
committerBarry Warsaw2016-04-22 16:33:37 -0400
commitd8d7608902ef58bcc3cf1225a710cf8adf5fca82 (patch)
tree9469f0d1d2fe93144674fa983c320679185191bd
parent4ce8c79d9b758d66fdb1c0a400fe1130b75db552 (diff)
downloadmailman-d8d7608902ef58bcc3cf1225a710cf8adf5fca82.tar.gz
mailman-d8d7608902ef58bcc3cf1225a710cf8adf5fca82.tar.zst
mailman-d8d7608902ef58bcc3cf1225a710cf8adf5fca82.zip
-rw-r--r--src/mailman/commands/cli_withlist.py44
-rw-r--r--src/mailman/commands/docs/conf.rst1
-rw-r--r--src/mailman/commands/tests/test_shell.py74
-rw-r--r--src/mailman/config/schema.cfg4
-rw-r--r--src/mailman/docs/NEWS.rst3
5 files changed, 119 insertions, 7 deletions
diff --git a/src/mailman/commands/cli_withlist.py b/src/mailman/commands/cli_withlist.py
index 7e64e921a..79dc3b966 100644
--- a/src/mailman/commands/cli_withlist.py
+++ b/src/mailman/commands/cli_withlist.py
@@ -20,6 +20,7 @@
import re
import sys
+from contextlib import ExitStack
from functools import partial
from lazr.config import as_boolean
from mailman import public
@@ -29,6 +30,7 @@ from mailman.interfaces.command import ICLISubCommand
from mailman.interfaces.listmanager import IListManager
from mailman.utilities.interact import DEFAULT_BANNER, interact
from mailman.utilities.modules import call_name
+from string import Template
from traceback import print_exc
from zope.component import getUtility
from zope.interface import implementer
@@ -164,7 +166,16 @@ class Withlist:
commit=config.db.commit,
abort=config.db.abort,
config=config,
+ getUtility=getUtility
)
+ # Bootstrap some useful names into the namespace, mostly to make
+ # the component architecture and interfaces easily available.
+ for module_name in sys.modules:
+ if not module_name.startswith('mailman.interfaces.'):
+ continue
+ module = sys.modules[module_name]
+ for name in module.__all__:
+ overrides[name] = getattr(module, name)
banner = config.shell.banner + '\n' + (
banner if isinstance(banner, str) else '')
try:
@@ -194,13 +205,32 @@ class Withlist:
def _start_python(self, overrides, banner):
# Set the tab completion.
- try:
- import readline, rlcompleter # noqa
- readline.parse_and_bind('tab: complete')
- except ImportError:
- pass
- sys.ps1 = config.shell.prompt + ' '
- interact(upframe=False, banner=banner, overrides=overrides)
+ with ExitStack() as resources:
+ try: # pragma: no cover
+ import readline, rlcompleter # noqa
+ except ImportError: # pragma: no cover
+ print(_('readline not available'), file=sys.stderr)
+ pass
+ else:
+ readline.parse_and_bind('tab: complete')
+ history_file_template = config.shell.history_file.strip()
+ if len(history_file_template) > 0:
+ # Expand substitutions.
+ substitutions = {
+ key.lower(): getattr(config, key)
+ for key in dir(config) if key.endswith('_DIR')
+ }
+ history_file = Template(
+ history_file_template).safe_substitute(substitutions)
+ try:
+ readline.read_history_file(history_file)
+ except FileNotFoundError:
+ pass
+ resources.callback(
+ readline.write_history_file,
+ history_file)
+ sys.ps1 = config.shell.prompt + ' '
+ interact(upframe=False, banner=banner, overrides=overrides)
def _details(self):
"""Print detailed usage."""
diff --git a/src/mailman/commands/docs/conf.rst b/src/mailman/commands/docs/conf.rst
index 1db85918f..f465aab53 100644
--- a/src/mailman/commands/docs/conf.rst
+++ b/src/mailman/commands/docs/conf.rst
@@ -33,6 +33,7 @@ You can list all the key-value pairs of a specific section.
>>> FakeArgs.section = 'shell'
>>> command.process(FakeArgs)
[shell] banner: Welcome to the GNU Mailman shell
+ [shell] history_file:
[shell] prompt: >>>
[shell] use_ipython: no
diff --git a/src/mailman/commands/tests/test_shell.py b/src/mailman/commands/tests/test_shell.py
new file mode 100644
index 000000000..153591f5a
--- /dev/null
+++ b/src/mailman/commands/tests/test_shell.py
@@ -0,0 +1,74 @@
+# Copyright (C) 2016 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/>.
+
+"""Test the withlist/shell command."""
+
+import os
+import unittest
+
+from mailman.commands.cli_withlist import Withlist
+from mailman.config import config
+from mailman.interfaces.usermanager import IUserManager
+from mailman.testing.helpers import configuration
+from mailman.testing.layers import ConfigLayer
+from unittest.mock import patch
+
+
+class FakeArgs:
+ interactive = None
+ run = None
+ details = False
+ listname = None
+
+
+class TestShell(unittest.TestCase):
+ layer = ConfigLayer
+
+ def setUp(self):
+ self._shell = Withlist()
+
+ def test_namespace(self):
+ args = FakeArgs()
+ args.interactive = True
+ with patch.object(self._shell, '_start_python') as mock:
+ self._shell.process(args)
+ self.assertEqual(mock.call_count, 1)
+ # Don't test that all names are available, just a few choice ones.
+ positional, keywords = mock.call_args
+ namespace = positional[0]
+ self.assertIn('getUtility', namespace)
+ self.assertIn('IArchiver', namespace)
+ self.assertEqual(namespace['IUserManager'], IUserManager)
+
+ @configuration('shell', banner='my banner')
+ def test_banner(self):
+ args = FakeArgs()
+ args.interactive = True
+ with patch('mailman.commands.cli_withlist.interact') as mock:
+ self._shell.process(args)
+ self.assertEqual(mock.call_count, 1)
+ positional, keywords = mock.call_args
+ self.assertEqual(keywords['banner'], 'my banner\n')
+
+ @configuration('shell', history_file='$var_dir/history.py')
+ def test_history_file(self):
+ args = FakeArgs()
+ args.interactive = True
+ with patch('mailman.commands.cli_withlist.interact'):
+ self._shell.process(args)
+ history_file = os.path.join(config.VAR_DIR, 'history.py')
+ self.assertTrue(os.path.exists(history_file))
diff --git a/src/mailman/config/schema.cfg b/src/mailman/config/schema.cfg
index f00cc5e29..1cc209e73 100644
--- a/src/mailman/config/schema.cfg
+++ b/src/mailman/config/schema.cfg
@@ -89,6 +89,10 @@ banner: Welcome to the GNU Mailman shell
# that any import errors will be displayed to stderr.
use_ipython: no
+# Set this to allow for command line history if readline is available. This
+# can be as simple as $var_dir/history.py to put the file in the var directory.
+history_file:
+
[paths.master]
# Important directories for Mailman operation. These are defined here so that
diff --git a/src/mailman/docs/NEWS.rst b/src/mailman/docs/NEWS.rst
index b8362048d..ec71afc9f 100644
--- a/src/mailman/docs/NEWS.rst
+++ b/src/mailman/docs/NEWS.rst
@@ -97,6 +97,9 @@ Command line
* ``mailman`` subcommands now properly commit any outstanding transactions.
(Closes #223)
* ``mailman digests`` has grown ``--verbose`` and ``-dry-run`` options.
+ * ``mailman shell`` now supports readline history if you set the
+ ``[shell]history_file`` variable in mailman.cfg. Also, many useful names
+ are pre-populated in the namespace of the shell. (Closes: #228)
Interfaces
----------