Python twisted.internet.protocol.ProcessProtocol() Examples

The following are 30 code examples of twisted.internet.protocol.ProcessProtocol(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module twisted.internet.protocol , or try the search function .
Example #1
Source File: test_process.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_stdout(self):
        """
        ProcessProtocol.transport.closeStdout actually closes the pipe.
        """
        d = self.doit(1)
        def _check(errput):
            if _PY3:
                if runtime.platform.isWindows():
                    self.assertIn(b"OSError", errput)
                    self.assertIn(b"22", errput)
                else:
                    self.assertIn(b'BrokenPipeError', errput)
            else:
                self.assertIn(b'OSError', errput)
            if runtime.platform.getType() != 'win32':
                self.assertIn(b'Broken pipe', errput)
        d.addCallback(_check)
        return d 
Example #2
Source File: test_process.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_stdout(self):
        """
        ProcessProtocol.transport.closeStdout actually closes the pipe.
        """
        d = self.doit(1)
        def _check(errput):
            if _PY3:
                if runtime.platform.isWindows():
                    self.assertIn(b"OSError", errput)
                    self.assertIn(b"22", errput)
                else:
                    self.assertIn(b'BrokenPipeError', errput)
            else:
                self.assertIn(b'OSError', errput)
            if runtime.platform.getType() != 'win32':
                self.assertIn(b'Broken pipe', errput)
        d.addCallback(_check)
        return d 
Example #3
Source File: utils.py    From tensor with MIT License 6 votes vote down vote up
def fork(executable, args=(), env={}, path=None, timeout=3600):
    """fork
    Provides a deferred wrapper function with a timeout function

    :param executable: Executable
    :type executable: str.
    :param args: Tupple of arguments
    :type args: tupple.
    :param env: Environment dictionary
    :type env: dict.
    :param timeout: Kill the child process if timeout is exceeded
    :type timeout: int.
    """
    d = defer.Deferred()
    p = ProcessProtocol(d, timeout)
    reactor.spawnProcess(p, executable, (executable,)+tuple(args), env, path)
    return d 
Example #4
Source File: test_process.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_stderr(self):
        """
        Bytes written to stderr by the spawned process are passed to the
        C{errReceived} callback on the C{ProcessProtocol} passed to
        C{spawnProcess}.
        """
        value = "42"

        p = Accumulator()
        d = p.endedDeferred = defer.Deferred()
        reactor.spawnProcess(p, pyExe,
                             [pyExe, b"-c",
                              networkString("import sys; sys.stderr.write"
                                            "('{0}')".format(value))],
                             env=None, path="/tmp",
                             usePTY=self.usePTY)

        def processEnded(ign):
            self.assertEqual(b"42", p.errF.getvalue())
        return d.addCallback(processEnded) 
Example #5
Source File: shutdownmanager.py    From landscape-client with GNU General Public License v2.0 6 votes vote down vote up
def _respond_failure(self, failure, operation_id, reboot):
        logging.info("Shutdown request failed.")
        failure_report = '\n'.join([
            failure.value.data,
            "",
            "Attempting to force {operation}. Please note that if this "
            "succeeds, Landscape will have no way of knowing and will still "
            "mark this activity as having failed. It is recommended you check "
            "the state of the machine manually to determine whether "
            "{operation} succeeded.".format(
                operation="reboot" if reboot else "shutdown")
        ])
        deferred = self._respond(FAILED, failure_report, operation_id)
        # Add another callback spawning the poweroff or reboot command (which
        # seem more reliable in aberrant situations like a post-trusty release
        # upgrade where upstart has been replaced with systemd). If this
        # succeeds, we won't have any opportunity to report it and if it fails
        # we'll already have responded indicating we're attempting to force
        # the operation so either way there's no sense capturing output
        protocol = ProcessProtocol()
        command, args = self._get_command_and_args(protocol, reboot, True)
        deferred.addCallback(
            lambda _: self._process_factory.spawnProcess(
                protocol, command, args=args))
        return deferred 
Example #6
Source File: test_session.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_requestExec(self):
        """
        When a client requests a command, the SSHSession object should get
        the command by getting an ISession adapter for the avatar, then
        calling execCommand with a ProcessProtocol to attach and the
        command line.
        """
        ret = self.session.requestReceived(b'exec',
                                           common.NS(b'failure'))
        self.assertFalse(ret)
        self.assertRequestRaisedRuntimeError()
        self.assertIsNone(self.session.client)

        self.assertTrue(self.session.requestReceived(b'exec',
                                                     common.NS(b'success')))
        self.assertSessionIsStubSession()
        self.assertIsInstance(self.session.client,
                              session.SSHSessionProcessProtocol)
        self.assertIs(self.session.session.execProtocol, self.session.client)
        self.assertEqual(self.session.session.execCommandLine,
                b'success') 
Example #7
Source File: test_session.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_requestShell(self):
        """
        When a client requests a shell, the SSHSession object should get
        the shell by getting an ISession adapter for the avatar, then
        calling openShell() with a ProcessProtocol to attach.
        """
        # gets a shell the first time
        ret = self.session.requestReceived(b'shell', b'')
        self.assertTrue(ret)
        self.assertSessionIsStubSession()
        self.assertIsInstance(self.session.client,
                              session.SSHSessionProcessProtocol)
        self.assertIs(self.session.session.shellProtocol, self.session.client)
        # doesn't get a shell the second time
        self.assertFalse(self.session.requestReceived(b'shell', b''))
        self.assertRequestRaisedRuntimeError() 
Example #8
Source File: test_session.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def test_requestExec(self):
        """
        When a client requests a command, the SSHSession object should get
        the command by getting an ISession adapter for the avatar, then
        calling execCommand with a ProcessProtocol to attach and the
        command line.
        """
        ret = self.session.requestReceived('exec',
                                           common.NS('failure'))
        self.assertFalse(ret)
        self.assertRequestRaisedRuntimeError()
        self.assertIdentical(self.session.client, None)

        self.assertTrue(self.session.requestReceived('exec',
                                                     common.NS('success')))
        self.assertSessionIsStubSession()
        self.assertIsInstance(self.session.client,
                              session.SSHSessionProcessProtocol)
        self.assertIdentical(self.session.session.execProtocol,
                self.session.client)
        self.assertEquals(self.session.session.execCommandLine,
                'success') 
Example #9
Source File: test_disttrial.py    From learn_python3_spider with MIT License 6 votes vote down vote up
def test_launchWorkerProcesses(self):
        """
        Given a C{spawnProcess} function, C{launchWorkerProcess} launches a
        python process with an existing path as its argument.
        """
        protocols = [ProcessProtocol() for i in range(4)]
        arguments = []
        environment = {}

        def fakeSpawnProcess(processProtocol, executable, args=(), env={},
                             path=None, uid=None, gid=None, usePTY=0,
                             childFDs=None):
            arguments.append(executable)
            arguments.extend(args)
            environment.update(env)

        self.runner.launchWorkerProcesses(
            fakeSpawnProcess, protocols, ["foo"])
        self.assertEqual(arguments[0], arguments[1])
        self.assertTrue(os.path.exists(arguments[2]))
        self.assertEqual("foo", arguments[3])
        self.assertEqual(os.pathsep.join(sys.path),
                         environment["TRIAL_PYTHONPATH"]) 
Example #10
Source File: test_process.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_stderr(self):
        """
        Bytes written to stderr by the spawned process are passed to the
        C{errReceived} callback on the C{ProcessProtocol} passed to
        C{spawnProcess}.
        """
        value = "42"

        p = Accumulator()
        d = p.endedDeferred = defer.Deferred()
        reactor.spawnProcess(p, pyExe,
                             [pyExe, b"-c",
                              networkString("import sys; sys.stderr.write"
                                            "('{0}')".format(value))],
                             env=None, path="/tmp",
                             usePTY=self.usePTY)

        def processEnded(ign):
            self.assertEqual(b"42", p.errF.getvalue())
        return d.addCallback(processEnded) 
Example #11
Source File: test_process.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
def test_stderr(self):
        """
        Bytes written to stderr by the spawned process are passed to the
        C{errReceived} callback on the C{ProcessProtocol} passed to
        C{spawnProcess}.
        """
        cmd = sys.executable

        value = "42"

        p = Accumulator()
        d = p.endedDeferred = defer.Deferred()
        reactor.spawnProcess(p, cmd,
                             [cmd, "-c", 
                              "import sys; sys.stderr.write('%s')" % (value,)],
                             env=None, path="/tmp",
                             usePTY=self.usePTY)

        def processEnded(ign):
            self.assertEquals(value, p.errF.getvalue())
        return d.addCallback(processEnded) 
Example #12
Source File: twisted.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
def makeDeferredWithProcessProtocol():
    """Returns a (`Deferred`, `ProcessProtocol`) tuple.

    The Deferred's `callback()` will be called (with None) if the
    `ProcessProtocol` is called back indicating that no error occurred.
    Its `errback()` will be called with the `Failure` reason otherwise.
    """
    done = Deferred()
    protocol = ProcessProtocol()
    # Call the errback if the "failure" object indicates a non-zero exit.
    protocol.processEnded = lambda reason: (
        done.errback(reason)
        if (reason and not reason.check(ProcessDone))
        else done.callback(None)
    )
    return done, protocol 
Example #13
Source File: test_session.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_requestExec(self):
        """
        When a client requests a command, the SSHSession object should get
        the command by getting an ISession adapter for the avatar, then
        calling execCommand with a ProcessProtocol to attach and the
        command line.
        """
        ret = self.session.requestReceived(b'exec',
                                           common.NS(b'failure'))
        self.assertFalse(ret)
        self.assertRequestRaisedRuntimeError()
        self.assertIsNone(self.session.client)

        self.assertTrue(self.session.requestReceived(b'exec',
                                                     common.NS(b'success')))
        self.assertSessionIsStubSession()
        self.assertIsInstance(self.session.client,
                              session.SSHSessionProcessProtocol)
        self.assertIs(self.session.session.execProtocol, self.session.client)
        self.assertEqual(self.session.session.execCommandLine,
                b'success') 
Example #14
Source File: test_session.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_requestShell(self):
        """
        When a client requests a shell, the SSHSession object should get
        the shell by getting an ISession adapter for the avatar, then
        calling openShell() with a ProcessProtocol to attach.
        """
        # gets a shell the first time
        ret = self.session.requestReceived(b'shell', b'')
        self.assertTrue(ret)
        self.assertSessionIsStubSession()
        self.assertIsInstance(self.session.client,
                              session.SSHSessionProcessProtocol)
        self.assertIs(self.session.session.shellProtocol, self.session.client)
        # doesn't get a shell the second time
        self.assertFalse(self.session.requestReceived(b'shell', b''))
        self.assertRequestRaisedRuntimeError() 
Example #15
Source File: test_disttrial.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
def test_launchWorkerProcesses(self):
        """
        Given a C{spawnProcess} function, C{launchWorkerProcess} launches a
        python process with an existing path as its argument.
        """
        protocols = [ProcessProtocol() for i in range(4)]
        arguments = []
        environment = {}

        def fakeSpawnProcess(processProtocol, executable, args=(), env={},
                             path=None, uid=None, gid=None, usePTY=0,
                             childFDs=None):
            arguments.append(executable)
            arguments.extend(args)
            environment.update(env)

        self.runner.launchWorkerProcesses(
            fakeSpawnProcess, protocols, ["foo"])
        self.assertEqual(arguments[0], arguments[1])
        self.assertTrue(os.path.exists(arguments[2]))
        self.assertEqual("foo", arguments[3])
        self.assertEqual(os.pathsep.join(sys.path),
                         environment["TRIAL_PYTHONPATH"]) 
Example #16
Source File: test_session.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_lookupSubsystem(self):
        """
        When a client requests a subsystem, the SSHSession object should get
        the subsystem by calling avatar.lookupSubsystem, and attach it as
        the client.
        """
        ret = self.session.requestReceived(
            'subsystem', common.NS('TestSubsystem') + 'data')
        self.assertTrue(ret)
        self.assertIsInstance(self.session.client, protocol.ProcessProtocol)
        self.assertIdentical(self.session.client.transport.proto,
                             self.session.avatar.subsystem) 
Example #17
Source File: services.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def createProcessProtocol(self):
        """
        Creates and returns the ProcessProtocol that will be used to
        communicate with the process.

        This MUST be overridden in subclasses.
        """ 
Example #18
Source File: batch.py    From axiom with MIT License 5 votes vote down vote up
def errConnectionLost(self):
        log.msg("Standard err closed")
        protocol.ProcessProtocol.errConnectionLost(self) 
Example #19
Source File: test_shutdownmanager.py    From landscape-client with GNU General Public License v2.0 5 votes vote down vote up
def test_restart_fails(self):
        """
        If an error occurs before the error checking timeout the activity will
        be failed.  Data printed by the process prior to the failure is
        included in the activity's result text.
        """
        message = {"type": "shutdown", "reboot": True, "operation-id": 100}
        self.plugin.perform_shutdown(message)

        def restart_failed(message_id):
            self.assertTrue(self.broker_service.exchanger.is_urgent())
            messages = self.broker_service.message_store.get_pending_messages()
            self.assertEqual(len(messages), 1)
            message = messages[0]
            self.assertEqual(message["type"], "operation-result")
            self.assertEqual(message["api"], b"3.2")
            self.assertEqual(message["operation-id"], 100)
            self.assertEqual(message["timestamp"], 0)
            self.assertEqual(message["status"], FAILED)
            self.assertIn(u"Failure text is reported.", message["result-text"])

            # Check that after failing, we attempt to force the shutdown by
            # switching the binary called
            [spawn1_args, spawn2_args] = self.process_factory.spawns
            protocol = spawn2_args[0]
            self.assertIsInstance(protocol, ProcessProtocol)
            self.assertEqual(spawn2_args[1:3],
                             ("/sbin/reboot", ["/sbin/reboot"]))

        [arguments] = self.process_factory.spawns
        protocol = arguments[0]
        protocol.result.addCallback(restart_failed)
        protocol.childDataReceived(0, b"Failure text is reported.")
        protocol.processEnded(Failure(ProcessTerminated(exitCode=1)))
        return protocol.result 
Example #20
Source File: test_conch.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def __init__(self, protocol, data):
        """
        @type protocol: L{ConchTestForwardingProcess}
        @param protocol: The L{ProcessProtocol} which made this connection.

        @type data: str
        @param data: The data to be sent to the third-party server.
        """
        self.protocol = protocol
        self.data = data 
Example #21
Source File: test_session.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def __init__(self, processProtocol):
        """
        Initialize our instance variables.

        @param processProtocol: a C{ProcessProtocol} to connect to ourself.
        """
        self.proto = processProtocol
        self.closed = False
        self.data = ''
        processProtocol.makeConnection(self) 
Example #22
Source File: batch.py    From axiom with MIT License 5 votes vote down vote up
def outConnectionLost(self):
        log.msg("Standard out closed")
        protocol.ProcessProtocol.outConnectionLost(self) 
Example #23
Source File: batch.py    From axiom with MIT License 5 votes vote down vote up
def inConnectionLost(self):
        log.msg("Standard in closed")
        protocol.ProcessProtocol.inConnectionLost(self) 
Example #24
Source File: test_session.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def write(self, data):
        """
        We got some data.  Give it back to our C{ProcessProtocol} with
        a newline attached.  Disconnect if there's a null byte.
        """
        self.data += data
        self.proto.outReceived(data)
        self.proto.outReceived('\r\n')
        if '\x00' in data: # mimic 'exit' for the shell test
            self.loseConnection() 
Example #25
Source File: test_session.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def loseConnection(self):
        """
        If we're asked to disconnect (and we haven't already) shut down
        the C{ProcessProtocol} with a 0 exit code.
        """
        if self.closed:
            return
        self.closed = 1
        self.proto.inConnectionLost()
        self.proto.outConnectionLost()
        self.proto.errConnectionLost()
        self.proto.processEnded(failure.Failure(
                error.ProcessTerminated(0, None, None))) 
Example #26
Source File: test_stdio.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def _spawnProcess(self, proto, sibling, *args, **kw):
        """
        Launch a child Python process and communicate with it using the
        given ProcessProtocol.

        @param proto: A L{ProcessProtocol} instance which will be connected
        to the child process.

        @param sibling: The basename of a file containing the Python program
        to run in the child process.

        @param *args: strings which will be passed to the child process on
        the command line as C{argv[2:]}.

        @param **kw: additional arguments to pass to L{reactor.spawnProcess}.

        @return: The L{IProcessTransport} provider for the spawned process.
        """
        import twisted
        subenv = dict(os.environ)
        subenv['PYTHONPATH'] = os.pathsep.join(
            [os.path.abspath(
                    os.path.dirname(os.path.dirname(twisted.__file__))),
             subenv.get('PYTHONPATH', '')
             ])
        args = [sys.executable,
             filepath.FilePath(__file__).sibling(sibling).path,
             reactor.__class__.__module__] + list(args)
        return reactor.spawnProcess(
            proto,
            sys.executable,
            args,
            env=subenv,
            **kw) 
Example #27
Source File: test_process.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def test_stderr(self):
        """ProcessProtocol.transport.closeStderr actually closes the pipe."""
        d = self.doit(2)
        def _check(errput):
            # there should be no stderr open, so nothing for it to
            # write the error to.
            self.failUnlessEqual(errput, '')
        d.addCallback(_check)
        return d 
Example #28
Source File: test_process.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def test_stdout(self):
        """ProcessProtocol.transport.closeStdout actually closes the pipe."""
        d = self.doit(1)
        def _check(errput):
            self.failIfEqual(errput.find('OSError'), -1)
            if runtime.platform.getType() != 'win32':
                self.failIfEqual(errput.find('Broken pipe'), -1)
        d.addCallback(_check)
        return d 
Example #29
Source File: test_process.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_interface(self):
        """
        L{ProcessProtocol} implements L{IProcessProtocol}.
        """
        verifyObject(interfaces.IProcessProtocol, protocol.ProcessProtocol()) 
Example #30
Source File: test_process.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def test_outReceived(self):
        """
        Verify that when stdout is delivered to
        L{ProcessProtocol.childDataReceived}, it is forwarded to
        L{ProcessProtocol.outReceived}.
        """
        received = []
        class OutProtocol(StubProcessProtocol):
            def outReceived(self, data):
                received.append(data)

        bytes = "bytes"
        p = OutProtocol()
        p.childDataReceived(1, bytes)
        self.assertEqual(received, [bytes])