From bc4498e59eed915b12fe546c766deb97de3eb677 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 21 Sep 2017 19:45:12 +0100 Subject: [PATCH 1/9] Add missing imports: ast, compiler Found using pylint --- mitogen/master.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mitogen/master.py b/mitogen/master.py index e64e1796..bac43951 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -1,4 +1,6 @@ +import ast import commands +import compiler import errno import getpass import imp From 6b9ac07e657dd27c8f41afce8fc1e0bd0bbfb8d4 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 21 Sep 2017 19:45:50 +0100 Subject: [PATCH 2/9] Declare self-less method as static Found using pylint --- mitogen/master.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mitogen/master.py b/mitogen/master.py index bac43951..de9048eb 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -565,6 +565,7 @@ class Stream(mitogen.core.Stream): # base64'd and passed to 'python -c'. It forks, dups 0->100, creates a # pipe, then execs a new interpreter with a custom argv. 'CONTEXT_NAME' is # replaced with the context name. Optimized for size. + @staticmethod def _first_stage(): import os,sys,zlib R,W=os.pipe() From 7245d574ef8a767cb56df177c23be0525abc5e32 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 21 Sep 2017 19:47:14 +0100 Subject: [PATCH 3/9] Add missing auth_incorrect_msg used to in exception message Found using pylint --- mitogen/ssh.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mitogen/ssh.py b/mitogen/ssh.py index 88f0edc3..53018389 100644 --- a/mitogen/ssh.py +++ b/mitogen/ssh.py @@ -70,6 +70,7 @@ class Stream(mitogen.master.Stream): if self.port: self.name += ':%s' % (self.port,) + auth_incorrect_msg = 'SSH authentication is incorrect' password_incorrect_msg = 'SSH password is incorrect' password_required_msg = 'SSH password was requested, but none specified' From 8ac45565a5d507b31c40d27038760b06e5cbcd5f Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 21 Sep 2017 19:48:50 +0100 Subject: [PATCH 4/9] Add missing import of AnsibleError Found with pylint --- mitogen/ansible/connection.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mitogen/ansible/connection.py b/mitogen/ansible/connection.py index 4070707f..d35944f6 100644 --- a/mitogen/ansible/connection.py +++ b/mitogen/ansible/connection.py @@ -20,6 +20,7 @@ import mitogen.ssh import mitogen.utils from mitogen.ansible import helpers +import ansible.errors import ansible.plugins.connection @@ -47,7 +48,7 @@ class Connection(ansible.plugins.connection.ConnectionBase): def exec_command(self, cmd, in_data=None, sudoable=True): super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable) if in_data: - raise AnsibleError("does not support module pipelining") + raise ansible.errors.AnsibleError("does not support module pipelining") return self.context.call(helpers.exec_command, cmd, in_data) From 0384e6189c50d3e1449d39489a6432aac4a519cf Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 21 Sep 2017 19:50:20 +0100 Subject: [PATCH 5/9] Use fully qualified Context Fixes an undefined variable error found by pylint --- mitogen/tcp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mitogen/tcp.py b/mitogen/tcp.py index b0b7c6d3..ace294ef 100644 --- a/mitogen/tcp.py +++ b/mitogen/tcp.py @@ -21,7 +21,7 @@ class Listener(mitogen.core.BasicStream): def on_receive(self, broker): sock, addr = self._sock.accept() - context = Context(self._broker, name=addr) + context = mitogen.core.Context(self._broker, name=addr) stream = mitogen.core.Stream(context) stream.accept(sock.fileno(), sock.fileno()) From 794a8a7a07e9249403ce284bbf730529657fd4a5 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 21 Sep 2017 19:52:27 +0100 Subject: [PATCH 6/9] Add missing LOG import Fixes an undefined variable error found by pylint --- mitogen/tcp.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mitogen/tcp.py b/mitogen/tcp.py index ace294ef..18829ab7 100644 --- a/mitogen/tcp.py +++ b/mitogen/tcp.py @@ -7,6 +7,8 @@ import socket import mitogen.core +from mitogen.core import LOG + class Listener(mitogen.core.BasicStream): def __init__(self, broker, address=None, backlog=30): From a8d216eeabc62a60a1081a0f1c1a2d5393bea790 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 21 Sep 2017 19:59:53 +0100 Subject: [PATCH 7/9] Update lingering references to with_broker & run_with_broker A previous commit renamed run_with_broker() and with_broker() to run_with_router() and with_router() respctively. Some references were missed. --- mitogen/utils.py | 8 ++++---- tests/timing_test.py | 6 +++--- tests/utils_test.py | 26 +++++++++++++------------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/mitogen/utils.py b/mitogen/utils.py index f67125af..1afd9a80 100644 --- a/mitogen/utils.py +++ b/mitogen/utils.py @@ -45,7 +45,7 @@ def log_to_file(path=None, io=True, level='INFO'): def run_with_router(func, *args, **kwargs): - """Arrange for `func(broker, *args, **kwargs)` to run with a temporary + """Arrange for `func(router, *args, **kwargs)` to run with a temporary :py:class:`mitogen.master.Router`, ensuring the Router and Broker are correctly shut down during normal or exceptional return.""" broker = mitogen.master.Broker() @@ -58,12 +58,12 @@ def run_with_router(func, *args, **kwargs): def with_router(func): - """Decorator version of :py:func:`run_with_broker`. Example: + """Decorator version of :py:func:`run_with_router`. Example: .. code-block:: python - @with_broker - def do_stuff(broker, arg): + @with_router + def do_stuff(router, arg): pass do_stuff(blah, 123) diff --git a/tests/timing_test.py b/tests/timing_test.py index 36bb9da7..e89fc3a9 100644 --- a/tests/timing_test.py +++ b/tests/timing_test.py @@ -8,9 +8,9 @@ import mitogen.master import mitogen.utils -@mitogen.utils.with_broker -def do_stuff(broker): - context = mitogen.master.connect(broker) +@mitogen.utils.with_router +def do_stuff(router): + context = router.connect(mitogen.master.Stream) t0 = time.time() ncalls = 1000 for x in xrange(ncalls): diff --git a/tests/utils_test.py b/tests/utils_test.py index 6fc25e8c..2f0c1f08 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -6,27 +6,27 @@ import mitogen.master import mitogen.utils -def func0(broker): - return broker +def func0(router): + return router -@mitogen.utils.with_broker -def func(broker): - return broker +@mitogen.utils.with_router +def func(router): + return router -class RunWithBrokerTest(unittest.TestCase): +class RunWithRouterTest(unittest.TestCase): # test_shutdown_on_exception # test_shutdown_on_success def test_run_with_broker(self): - broker = mitogen.utils.run_with_broker(func0) - self.assertTrue(isinstance(broker, mitogen.master.Broker)) - self.assertFalse(broker._thread.isAlive()) + router = mitogen.utils.run_with_router(func0) + self.assertTrue(isinstance(router, mitogen.master.Router)) + self.assertFalse(router.broker._thread.isAlive()) -class WithBrokerTest(unittest.TestCase): +class WithRouterTest(unittest.TestCase): def test_with_broker(self): - broker = func() - self.assertTrue(isinstance(broker, mitogen.master.Broker)) - self.assertFalse(broker._thread.isAlive()) + router = func() + self.assertTrue(isinstance(router, mitogen.master.Router)) + self.assertFalse(router.broker._thread.isAlive()) From 81fe37b1086f154991119a929a0cbcc5d19d4283 Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 21 Sep 2017 20:03:51 +0100 Subject: [PATCH 8/9] Fix stale references to m.master.connect & m.ssh.connect I'm fairly sure these updates are correct, but I'm not able to run the test suite to confirm. --- mitogen/ansible/connection.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mitogen/ansible/connection.py b/mitogen/ansible/connection.py index d35944f6..25849eb1 100644 --- a/mitogen/ansible/connection.py +++ b/mitogen/ansible/connection.py @@ -39,11 +39,13 @@ class Connection(ansible.plugins.connection.ConnectionBase): if self.connected: return self.broker = mitogen.master.Broker() + self.router = mitogen.master.Router(self.broker) if self._play_context.remote_addr == 'localhost': - self.context = mitogen.master.connect(self.broker) + self.context = self.router.connect(mitogen.master.Stream) else: - self.context = mitogen.ssh.connect(broker, - self._play_context.remote_addr) + self.context = self.router.connect(mitogen.ssh.Stream, + hostname=self._play_context.remote_addr, + ) def exec_command(self, cmd, in_data=None, sudoable=True): super(Connection, self).exec_command(cmd, in_data=in_data, sudoable=sudoable) From c854bec6f630304b6d4401e5bf6a01ecc03711ce Mon Sep 17 00:00:00 2001 From: Alex Willmer Date: Thu, 21 Sep 2017 22:12:07 +0100 Subject: [PATCH 9/9] Handle failure to import the ast module --- mitogen/master.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/mitogen/master.py b/mitogen/master.py index de9048eb..db91aece 100644 --- a/mitogen/master.py +++ b/mitogen/master.py @@ -1,4 +1,9 @@ -import ast +try: + import ast +except ImportError: + # ast module is not available in Python 2.4.x, instead we shall use the + # the compiler module as a fallback + ast = None import commands import compiler import errno