def run(components, log_level='info'):
    """
    High-level API to run a series of components.

    This will only return once all the components have stopped
    (including, possibly, after all re-connections have failed if you
    have re-connections enabled). Under the hood, this calls
    :meth:`twisted.internet.reactor.run` -- if you wish to manage the
    reactor loop yourself, use the
    :meth:`autobahn.twisted.component.Component.start` method to start
    each component yourself.

    :param components: the Component(s) you wish to run
    :type components: Component or list of Components

    :param log_level: a valid log-level (or None to avoid calling start_logging)
    :type log_level: string
    """
    # only for Twisted > 12
    # ...so this isn't in all Twisted versions we test against -- need
    # to do "something else" if we can't import .. :/ (or drop some
    # support)
    from twisted.internet.task import react

    # actually, should we even let people "not start" the logging? I'm
    # not sure that's wise... (double-check: if they already called
    # txaio.start_logging() what happens if we call it again?)
    if log_level is not None:
        txaio.start_logging(level=log_level)
    react(component._run, (components, ))
Пример #2
0
def main():
    """
    """
    logger = setLogger()
    currencies_responses = get_responses(200)
    logger.debug("Got responses")
    currencies_soups = collect_soups(currencies_responses)
    logger.debug("Got soups")
    curr_short_rate_names = collect_short_rate_names(currencies_soups)
    logger.debug("Collected short rate names")
    short_rate_names_to_parse = prepare_for_parse(curr_short_rate_names)
    logger.debug("Got short rate names to parse")
    short_rate_names_responses = get_short_rate_names_responses(
        short_rate_names_to_parse)
    logger.debug("Got short rate names responses")
    short_rate_names_trees = collect_short_rate_names_trees(
        short_rate_names_responses)
    logger.debug("Collected short rate names trees")
    hash_table = create_hash_table(short_rate_names_trees,
                                   short_rate_names_to_parse)
    logger.debug("Created hash_table")
    create_currencies_tables('userdb',
                             curr_short_rate_names,
                             hash_table,
                             )
    logger.debug("Created currencies table")
    task.react(graph_id_parser, (curr_short_rate_names,))
Пример #3
0
    def run(self):
        """
        Schedules the startRequests method and gracefully exits the program when either
        all of the deferred objects fire successfully or fail
        """

        task.react(self._startRequests)
Пример #4
0
def test():
    transports = [
        {
            'type': 'rawsocket',
            'serializer': 'msgpack',
            'endpoint': {
                'type': 'unix',
                'path': '/tmp/cb1.sock'
            }
        },
        {
            'type': 'websocket',
            'url': 'ws://127.0.0.1:8080/ws',
            'endpoint': {
                'type': 'tcp',
                'host': '127.0.0.1',
                'port': 8080
            }
        }
    ]

    connection1 = Connection(main1, transports=transports[0])
    yield react(connection1.start)

    connection2 = Connection(main2, transports=transports[1])
    yield react(connection2.start)
Пример #5
0
	def reactOn(self,d):
#		def onEnd(response):
#			reactor.stop()
#		d.addBoth(onEnd)
#		reactor.run()
		def main(r):
			return d
		react(main)
Пример #6
0
def run():
    assert sys.version_info < (3,), u"Tahoe-LAFS does not run under Python 3. Please use Python 2.7.x."

    if sys.platform == "win32":
        from allmydata.windows.fixups import initialize
        initialize()
    # doesn't return: calls sys.exit(rc)
    task.react(_run_with_reactor)
Пример #7
0
def cli(user, probes, results, verbose):
    if False:
        # writes a 'txtorcon.log' with debugging information
        from txtorcon.log import debug_logging
        debug_logging()

    args = (user, probes, results, verbose)
    react(main, args)
Пример #8
0
def main(argv=None, _abort_for_test=False):
    # This function is referenced by the setup.py entry point definition as well as the name=__main__ test below.
    def go(reactor):
        return _main_async(reactor, argv, _abort_for_test)
        
    if _abort_for_test:
        return go(singleton_reactor)
    else:
        react(go)
Пример #9
0
 def main(self, args=None):
     parser = self.build_parser()
     args = parser.parse_args(args)
     self.verbose = args.verbose
     action = self.find_action(args)
     if not action:
         parser.print_help()
         sys.exit(2)
     action_method = getattr(self, action + '_action')
     task.react(action_method, [args])
Пример #10
0
 def test_runsUntilSyncCallback(self):
     """
     L{react} returns quickly if the L{Deferred} returned by the function it
     is passed has already been called back at the time it is returned.
     """
     def main(reactor):
         return defer.succeed(None)
     r = _FakeReactor()
     task.react(main, [], _reactor=r)
     self.assertEqual(r.seconds(), 0)
Пример #11
0
def _main():
    """
    This is a magic name for `python -m autobahn`, and specified as
    our entry_point in setup.py
    """
    react(
        lambda reactor: ensureDeferred(
            _real_main(reactor)
        )
    )
Пример #12
0
def react_app(app, main, argv=()):

    @defer.inlineCallbacks
    def appless_main(reactor):
        yield defer.maybeDeferred(service.IService(app).startService)
        try:
            yield defer.maybeDeferred(main, *argv)
        finally:
            yield defer.maybeDeferred(service.IService(app).stopService)

    task.react(appless_main)
Пример #13
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("hls_playlist")
    parser.add_argument("-v", action="store_true")
    parser.add_argument("-d", action="store_true")
    parser.add_argument("--dump-durations", action="store_true")
    parser.add_argument("--referer")
    parser.add_argument("-o");
    args = parser.parse_args()

    react(runProxy, [args])
Пример #14
0
def main(argv=sys.argv[1:]):
    parser = argparse.ArgumentParser(description='Yarrharr fetch debug utility')
    parser.add_argument('url')
    args = parser.parse_args()

    os.environ['DJANGO_SETTINGS_MODULE'] = 'yarrharr.settings'
    import yarrharr.application
    yarrharr.application

    # TODO config logging
    task.react(_txmain, [args.url])
Пример #15
0
def main(argv=sys.argv):
    parser = argparse.ArgumentParser()
    _curvecpm.addLogArguments(parser)
    parser.add_argument('-n', '--name')
    parser.add_argument('-e', '--server-extension', default='0' * 32)
    parser.add_argument('keydir', type=Keydir)
    parser.add_argument('port', type=int)
    parser.add_argument('program')
    parser.add_argument('argv', nargs='*')

    react(twistedMain, [parser.parse_args(argv[1:])])
Пример #16
0
 def test_arguments(self):
     """
     L{react} passes the elements of the list it is passed as positional
     arguments to the function it is passed.
     """
     args = []
     def main(reactor, x, y, z):
         args.extend((x, y, z))
         return defer.succeed(None)
     r = _FakeReactor()
     task.react(main, [1, 2, 3], _reactor=r)
     self.assertEqual(args, [1, 2, 3])
Пример #17
0
    def write(self, logentry):
        for i in list(logentry.keys()):
            # Remove twisted 15 legacy keys
            if i.startswith('log_'):
                del logentry[i]
            elif i == "time":
                del logentry[i]

        logging.basicConfig(
            format='%(name)s %(levelname)s %(message)s',
            level=logging.INFO,
        )
        task.react(produce, json.dumps(logentry))
Пример #18
0
 def test_singleStopCallback(self):
     """
     L{react} doesn't try to stop the reactor if the L{Deferred} the function
     it is passed is called back after the reactor has already been stopped.
     """
     def main(reactor):
         reactor.callLater(1, reactor.stop)
         finished = defer.Deferred()
         reactor.addSystemEventTrigger(
             'during', 'shutdown', finished.callback, None)
         return finished
     r = _FakeReactor()
     task.react(main, [], _reactor=r)
     self.assertEqual(r.seconds(), 1)
Пример #19
0
 def run(self, runner):
     """
     Start the runner and block on the reactor.
     """
     @defer.inlineCallbacks
     def _(_):
         yield runner.run()
         if self.report or self.report_only:
             self.printReport()
     try:
         task.react(_)
     except (SystemExit, KeyboardInterrupt):
         # clean up terminating line upon exit
         print "\033[1F"
Пример #20
0
    def test_defaultReactor(self):
        """
        L{twisted.internet.reactor} is used if no reactor argument is passed to
        L{task.react}.
        """
        def main(reactor):
            self.passedReactor = reactor
            return defer.succeed(None)

        reactor = _FakeReactor()
        with NoReactor():
            installReactor(reactor)
            task.react(main, [])
        self.assertIdentical(reactor, self.passedReactor)
Пример #21
0
 def test_runsUntilAsyncCallback(self):
     """
     L{react} runs the reactor until the L{Deferred} returned by the function
     it is passed is called back, then stops it.
     """
     timePassed = []
     def main(reactor):
         finished = defer.Deferred()
         reactor.callLater(1, timePassed.append, True)
         reactor.callLater(2, finished.callback, None)
         return finished
     r = _FakeReactor()
     task.react(main, [], _reactor=r)
     self.assertEqual(timePassed, [True])
     self.assertEqual(r.seconds(), 2)
Пример #22
0
    def test_runsUntilSyncErrback(self):
        """
        L{react} returns quickly if the L{Deferred} returned by the function it
        is passed has already been errbacked at the time it is returned.
        """
        class ExpectedException(Exception):
            pass

        def main(reactor):
            return defer.fail(ExpectedException())
        r = _FakeReactor()
        task.react(main, [], _reactor=r)
        self.assertEqual(r.seconds(), 0)
        errors = self.flushLoggedErrors(ExpectedException)
        self.assertEqual(len(errors), 1)
def agent_main(collector):
    to_file(sys.stdout)
    startLogging(sys.stdout)
    return react(
        run_agent, [
            environ.get(
                "FLOCKER_CONFIGURATION_PATH",
                "/etc/flocker",
            ).decode("ascii"),
            environ.get(
                "CATALOG_FIREHOSE_PROTOCOL",
                DEFAULT_FIREHOSE_PROTOCOL,
            ).decode("ascii"),
            environ.get(
                "CATALOG_FIREHOSE_HOSTNAME",
                DEFAULT_FIREHOSE_HOSTNAME,
            ).decode("ascii"),
            int(
                environ.get(
                    "CATALOG_FIREHOSE_PORT",
                    unicode(DEFAULT_FIREHOSE_PORT).encode("ascii"),
                ).decode("ascii")
            ),
            # Base64 encoded
            environ["CATALOG_FIREHOSE_SECRET"].decode("ascii"),
            collector,
        ],
    )
Пример #24
0
def main(argv):
    """
    Start logging to a temporary file and then run the main loop.
    """
    startLogging(file('imaginary-debug.log', 'w'))
    withSavedTerminalSettings(sys.__stdin__.fileno(),
                              lambda: react(runTextServer, argv))
Пример #25
0
    def test_runsUntilAsyncErrback(self):
        """
        L{react} runs the reactor until the L{Deferred} returned by the function
        it is passed is errbacked, then it stops the reactor and reports the
        error.
        """
        class ExpectedException(Exception):
            pass

        def main(reactor):
            finished = defer.Deferred()
            reactor.callLater(1, finished.errback, ExpectedException())
            return finished
        r = _FakeReactor()
        task.react(main, [], _reactor=r)
        errors = self.flushLoggedErrors(ExpectedException)
        self.assertEqual(len(errors), 1)
Пример #26
0
def import_porting_db(redis_uri, prefix, logfile, debug, header, file):
    from .portia import Portia
    from twisted.internet.task import react
    from twisted.python import log
    from txredisapi import Connection

    log.startLogging(logfile)

    d = Connection(redis_uri.hostname, int(redis_uri.port or 6379),
                   int(redis_uri.path[1:]))
    d.addCallback(lambda redis: Portia(redis, prefix=prefix))
    d.addCallback(lambda portia: portia.import_porting_file(file, header))
    d.addCallback(
        lambda msisdns: [
            log.msg('Imported %s' % (msisdn,)) for msisdn in msisdns])

    react(lambda _reactor: d)
Пример #27
0
def run():
    assert sys.version_info < (3,), ur"Tahoe-LAFS does not run under Python 3. Please use Python 2.7.x."

    if sys.platform == "win32":
        from allmydata.windows.fixups import initialize
        initialize()
    d = defer.maybeDeferred(parse_or_exit_with_explanation, sys.argv[1:])
    d.addCallback(dispatch)
    def _show_exception(f):
        # when task.react() notices a non-SystemExit exception, it does
        # log.err() with the failure and then exits with rc=1. We want this
        # to actually print the exception to stderr, like it would do if we
        # weren't using react().
        if f.check(SystemExit):
            return f # dispatch function handled it
        f.printTraceback(file=sys.stderr)
        sys.exit(1)
    d.addErrback(_show_exception)
    task.react(lambda _reactor: d) # doesn't return: calls sys.exit(rc)
Пример #28
0
    def test_singleStopErrback(self):
        """
        L{react} doesn't try to stop the reactor if the L{Deferred} the
        function it is passed is errbacked after the reactor has already been
        stopped.
        """
        class ExpectedException(Exception):
            pass

        def main(reactor):
            reactor.callLater(1, reactor.stop)
            finished = defer.Deferred()
            reactor.addSystemEventTrigger(
                'during', 'shutdown', finished.errback, ExpectedException())
            return finished
        r = _FakeReactor()
        task.react(main, [], _reactor=r)
        self.assertEqual(r.seconds(), 1)
        errors = self.flushLoggedErrors(ExpectedException)
        self.assertEqual(len(errors), 1)
Пример #29
0
    def _actual_send(self, pkg):
        def _reactor_send(reactor, pkg):
            require_trans_sec = self.smtp_server_.require_transport_security_
            require_auth = self.smtp_server_.require_authentication_
            log.startLogging(sys.stdout)
            log.msg('To addresses:', pkg.to_addresses_)
            d = sendmail(self.smtp_server_.host_,
                         self.from_address_,
                         pkg.to_addresses_,
                         pkg.shared_content_,
                         port=self.smtp_server_.port_,
                         username=self.account_.user_name_,
                         password=self.account_.password_,
                         requireAuthentication=require_auth,
                         requireTransportSecurity=require_trans_sec)

            #  d.addBoth(print)
            return d

        react(_reactor_send, (pkg, ))
Пример #30
0
def send(cfg, what, text, code, zeromode):
    """Send a text message, file, or directory"""
    with cfg.timing.add("import", which="cmd_send"):
        from . import cmd_send
    cfg.what = what
    cfg.text = text
    cfg.zeromode = zeromode
    cfg.code = code

    # note: react() does not return
    return react(_dispatch_command, (cfg, lambda: cmd_send.send(cfg)))
Пример #31
0
    def run_context(self, ctx, command=None):

        # cfg contains the command lines options and arguments that
        # click collected for us
        cfg = ctx.obj
        cmd = ctx.command.name

        self.log.info('{klass}.run_context: running shell command "{cmd}"',
                      klass=self.__class__.__name__,
                      cmd=cmd)

        yes_to_all = cfg.yes_to_all if hasattr(cfg, 'yes_to_all') else False

        # if cmd not in ['auth', 'shell']:
        #    raise click.ClickException('"{}" command can only be run in shell'.format(cmd))

        if self._output_verbosity == Application.OUTPUT_VERBOSITY_VERBOSE:
            click.echo('Crossbar.io Shell: {}'.format(
                style_ok('v{}'.format(__version__))))

        # load user profile and key for given profile name
        key, profile = self.load_profile(dotdir=cfg.dotdir,
                                         profile=cfg.profile,
                                         yes_to_all=yes_to_all,
                                         verbose=(ctx.command.name == 'init'))

        if ctx.command.name == 'init':
            return

        # set the Fabric URL to connect to from the profile or default
        url = profile.url or 'wss://fabric.crossbario.com'

        # users always authenticate with the user_id from the key, which
        # filled from the email the user provided
        authid = key.user_id

        # the realm can be set from command line, env var, the profile
        # or can be None, which means the user will be joined to the global
        # Crossbar.io users realm ('com.crossbario.fabric')
        realm = cfg.realm or profile.realm or None

        # the authrole can be set from command line, env var, the profile
        # or can be None, in which case the role is chosen automatically
        # from the list of roles the user us authorized for
        authrole = cfg.role or profile.role or None

        # this will be fired when the ShellClient below actually has joined
        # the respective realm on Crossbar.io (either the global users
        # realm, or a management realm the user has a role on)
        done = txaio.create_future()

        url_is_secure, _, _, _, _, _ = parse_url(url)

        extra = {
            # these are forward on the actual client connection
            'authid': authid,
            'authrole': authrole,

            # these are native Python object and only used client-side
            'key': key.key,
            'done': done,
            'command': command,

            # WAMP-cryptosign authentication: TLS channel binding
            'channel_binding': 'tls-unique' if url_is_secure else None,
        }

        cert_options = None
        if profile.tls_hostname:
            self.log.info(
                'Setting up TLS context (server CA/intermediate certificates, etc) from profile:'
            )
            tls_config = {
                'hostname': profile.tls_hostname,
                'ca_certificates': profile.tls_certificates
            }
            cert_options = _create_tls_client_context(tls_config, '.crossbar',
                                                      self.log)

        # for the "auth" command, forward additional command line options
        if ctx.command.name == 'auth':
            # user provides authentication code to verify
            extra['activation_code'] = cfg.code

            # user requests sending of a new authentication code (while an old one is still pending)
            extra['request_new_activation_code'] = cfg.new_code

        # this is the WAMP ApplicationSession that connects the CLI to Crossbar.io
        self.session = client.ShellClient(ComponentConfig(realm, extra))

        runner = ApplicationRunner(url, realm, ssl=cert_options)

        if self._output_verbosity == Application.OUTPUT_VERBOSITY_VERBOSE:
            click.echo('Connecting to {} ..'.format(url))

        connect_done = runner.run(self.session, start_reactor=False)

        def on_connect_success(res):
            self.log.info('{klass}.on_connect_success(res={res})',
                          klass=self.__class__.__name__,
                          res=pformat(res))

        def on_connect_error(err):
            self.log.warn('{klass}.on_connect_error(err={err})',
                          klass=self.__class__.__name__,
                          err=err)

            if isinstance(err, Failure):
                err = err.value

            txaio.reject(done, err)

            # raise SystemExit(1)

        txaio.add_callbacks(connect_done, on_connect_success, on_connect_error)

        def on_success(res):
            self.log.info('{klass}.on_success(res={res})',
                          klass=self.__class__.__name__,
                          res=pformat(res))

            session_details, result = res

            if cmd == 'auth':

                self._print_welcome(url, session_details)

            elif cmd == 'shell':

                # click.clear()
                self._print_welcome(url, session_details)

                # FIXME:

                # prompt_kwargs = {
                #     'history': self._history,
                # }
                #
                # from crossbar.shell import repl
                #
                # shell_task = loop.create_task(
                #     repl.repl(
                #         ctx,
                #         get_bottom_toolbar_tokens=self._get_bottom_toolbar_tokens,
                #         # get_prompt_tokens=self._get_prompt_tokens,
                #         style=self._style,
                #         prompt_kwargs=prompt_kwargs))
                #
                # try:
                #     loop.run_until_complete(shell_task)
                # except Exception as e:
                #     print(e)

            else:
                if result:
                    self._output_result(result)

        def on_error(err):
            self.log.warn('{klass}.on_error(err={err})',
                          klass=self.__class__.__name__,
                          err=err)

            if isinstance(err, Failure):
                err = err.value

            if isinstance(err, ApplicationError):

                self.log.warn('{message} - {error}',
                              message=err.args[0] if err.args else '',
                              error=err.error)

                # some ApplicationErrors are actually signaling progress
                # in the authentication flow, some are real errors

                exit_code = None

                if err.error.startswith('fabric.auth-failed.'):
                    error = err.error.split('.')[2]
                    message = err.args[0]

                    if error == 'new-user-auth-code-sent':

                        click.echo(
                            '\nThanks for registering! {}'.format(message))
                        click.echo(
                            style_ok(
                                'Please check your inbox and run "crossbar shell auth --code <THE CODE YOU GOT BY EMAIL>.\n'
                            ))

                    elif error == 'registered-user-auth-code-sent':

                        click.echo('\nWelcome back! {}'.format(message))
                        click.echo(
                            style_ok(
                                'Please check your inbox and run "crossbar shell auth --code <THE CODE YOU GOT BY EMAIL>.\n'
                            ))

                    elif error == 'pending-activation':

                        click.echo()
                        click.echo(style_ok(message))
                        click.echo()
                        click.echo(
                            'Tip: to activate, run "crossbar shell auth --code <THE CODE YOU GOT BY EMAIL>"'
                        )
                        click.echo(
                            'Tip: you can request sending a new code with "crossbar shell auth --new-code"'
                        )
                        click.echo()

                    elif error == 'no-pending-activation':

                        exit_code = 1
                        click.echo()
                        click.echo(
                            style_error('{} [{}]'.format(message, err.error)))
                        click.echo()

                    elif error == 'email-failure':

                        exit_code = 1
                        click.echo()
                        click.echo(
                            style_error('{} [{}]'.format(message, err.error)))
                        click.echo()

                    elif error == 'invalid-activation-code':

                        exit_code = 1
                        click.echo()
                        click.echo(
                            style_error('{} [{}]'.format(message, err.error)))
                        click.echo()

                    else:

                        exit_code = 1
                        click.echo(style_error('{}'.format(error)))
                        click.echo(style_error(message))

                elif err.error.startswith('crossbar.error.'):

                    error = err.error.split('.')[2]
                    message = err.args[0]

                    if error == 'invalid_configuration':

                        click.echo()
                        click.echo(
                            style_error('{} [{}]'.format(message, err.error)))
                        click.echo()
                    else:

                        exit_code = 1
                        click.echo(
                            style_error('{} [{}]'.format(message, err.error)))

                else:

                    click.echo(style_error('{}'.format(err)))
                    exit_code = 1

                if exit_code:
                    raise SystemExit(exit_code)

            else:
                click.echo(style_error('{}'.format(err)))
                raise SystemExit(1)

        txaio.add_callbacks(done, on_success, on_error)

        def doit(reactor):
            return done

        react(doit)
Пример #32
0
    def add(line):
        if line in already:
            return
        already.add(line)
        warnings.append(line)

    pp.stdout.seek(0)
    for line in pp.stdout.readlines():
        if match(line):
            add(line)  # includes newline

    pp.stderr.seek(0)
    for line in pp.stderr.readlines():
        if match(line):
            add(line)

    if warnings:
        if config["warnings"]:
            with open(config["warnings"], "wb") as f:
                print("".join(warnings), file=f)
        print("ERROR: %d deprecation warnings found" % len(warnings))
        sys.exit(1)

    print("no deprecation warnings")
    if signal:
        sys.exit(signal)
    sys.exit(rc)


task.react(run_command)
Пример #33
0
    def connectionMade(self):
        self.sendLine(b"plain text")
        self.sendLine(b"STARTTLS")

    def lineReceived(self, line):
        print("received: ", line)
        if line == b"READY":
            self.transport.startTLS(self.factory.options)
            self.sendLine(b"secure text")
            self.transport.loseConnection()


@defer.inlineCallbacks
def main(reactor):
    factory = protocol.Factory.forProtocol(StartTLSClient)
    certData = getModule(__name__).filePath.sibling("server.pem").getContent()
    factory.options = ssl.optionsForClientTLS(
        "example.com", ssl.PrivateCertificate.loadPEM(certData))
    endpoint = endpoints.HostnameEndpoint(reactor, "localhost", 8000)
    startTLSClient = yield endpoint.connect(factory)

    done = defer.Deferred()
    startTLSClient.connectionLost = lambda reason: done.callback(None)
    yield done


if __name__ == "__main__":
    import starttls_client

    task.react(starttls_client.main)
Пример #34
0
        #    print("Send back the ANNOTATED info took {} seconds.".format(
        #        time.time() - start))
        #    start = time.time()

        if args.verbose:
            print("One frame took {} seconds. fps= {}".format(
                time.time() - framestart, 1 / (time.time() - framestart)))
            print(
                "=================================================================="
            )

        msg = {
            "type": "ANNOTATED",
            "content": NameAndBB,
            "fps": 1 / (time.time() - framestart)
        }
        self.sendMessage(json.dumps(msg))


def main(reactor):
    log.startLogging(sys.stdout)
    factory = WebSocketServerFactory()
    factory.protocol = OpenFaceServerProtocol
    ctx_factory = DefaultOpenSSLContextFactory(tls_key, tls_crt)
    reactor.listenSSL(args.port, factory, ctx_factory)
    return defer.Deferred()


if __name__ == '__main__':
    task.react(main)

@inlineCallbacks
def main(reactor, context):
    try:
        kubeconfig = environ["KUBECONFIG"]
    except KeyError:
        config_paths = [None]
    else:
        config_paths = kubeconfig.split(":")

    kubernetes = None
    for config_path in config_paths:
        try:
            kubernetes = network_kubernetes_from_context(reactor, context)
        except PyKubeError:
            continue
        else:
            break
    if kubernetes is None:
        raise SystemExit("Cannot find Kubernetes context {}".format(context))

    client = yield kubernetes.client()
    model = client.model
    cfg = model.iobject_to_raw(configmap(model, dashboard()))
    print(dumps(cfg))


if __name__ == '__main__':
    react(main, argv[1:])
Пример #36
0
mime_msg = MIMEMultipart('alternative')
message["Subject"] = "Twisted is great!"
message["From"] = me
message["To"] = ", ".join(to)
mime_msg.attach(message)

def main(reactor):
    d = sendmail(b"exchange.profires.cz",
                 b"*****@*****.**",
                 to,
                 message)

    d.addBoth(print)
    return d

react(main)


class Attachment(object):

    def __init__(self, name, path):
        pass


class Email(object):

    HTML_TEMPLATE = ''
    TXT_TEMPLATE = ''

    def __init__(
        self,
Пример #37
0
##   <h1 style="valign:center;">async def, await</h1>

from twisted.internet.defer import ensureDeferred, Deferred
from twisted.internet.task import react


async def main(reactor):

    d = Deferred()

    reactor.callLater(1, d.callback, None)
    print("scheduled callback")
    await d
    print("done")


if __name__ == '__main__':

    def _main(reactor):
        return ensureDeferred(main(reactor))

    react(_main)

## show-output
Пример #38
0
def success(args, hostname):
    errorStatus, errorIndex, varBinds = args

    if errorStatus:
        print('%s: %s at %s' %
              (hostname, errorStatus.prettyPrint(),
               errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))

    else:
        for varBind in varBinds:
            print(' = '.join([x.prettyPrint() for x in varBind]))


def failure(errorIndication, hostname):
    print('%s failure: %s' % (hostname, errorIndication))


# noinspection PyUnusedLocal
def getSysDescr(reactor, hostname):
    deferred = getCmd(SnmpEngine(), CommunityData('public', mpModel=0),
                      UdpTransportTarget((hostname, 161)), ContextData(),
                      ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0)))

    deferred.addCallback(success, hostname).addErrback(failure, hostname)

    return deferred


react(getSysDescr, ['demo.snmplabs.com'])
Пример #39
0
def dispatch(args=None):
    """
    this is the main program; see __main__.py
    """

    if args is None:
        args = sys.argv

    global _log_observer
    options = Options()

    try:
        options.parseOptions(args[1:])

    except (usage.UsageError, RuntimeError) as e:
        print(options.getUsage())
        print(util.colors.red('Error: ') + str(e), file=sys.stderr)
        sys.exit(128)

    except Exception as e:
        print('Unknown error:', e)
        sys.exit(200)

    if options['color'] == 'never' or options['no-color'] or \
            (options['color'] == 'auto' and not sys.stdin.isatty()):
        util.turn_off_color()

    if options.subCommand is None:
        print(options)
        return

    sub = options.commands[options.subCommand]

    try:
        sub.validate(options.subOptions, options)
    except Exception as e:
        print(options.getUsage())
        print(util.colors.red('Error: ') + str(e), file=sys.stderr)
        if options['debug']:
            raise e
        return

    build_state = sub.build_state
    show_general_info = options['info']

    endpoint = options['connect']
    try:
        endpoint = endpoints.clientFromString(reactor, options['connect'])
    except ValueError:
        try:
            endpoint = endpoints.clientFromString(reactor,
                                                  'tcp:' + options['connect'])
        except TypeError:
            endpoint = endpoints.clientFromString(
                reactor, 'tcp:localhost:' + options['connect'])

    if options['timestamps']:
        _log_observer.timestamp = True

    if sub.controller_connection:
        d = txtorcon.build_tor_connection(endpoint, build_state=build_state)
    elif sub.build_state:
        raise RuntimeError(
            "Internal error: subcommand can't set build_state=True with controller_connection=False"
        )
    else:
        d = defer.succeed(None)
        show_general_info = False

    if show_general_info:
        d.addCallback(general_information, True)

    d.addCallback(
        lambda arg: ICarmlCommand(sub).run(options.subOptions, options, arg))
    d.addErrback(setup_failed, options['debug'])

    if options['debug']:

        def dump_heap():
            from guppy import hpy
            print(hpy().heap())

        d.addCallback(lambda _: dump_heap())

    # task.react needs a function that returns a Deferred
    task.react(lambda _: d)
Пример #40
0
#!/usr/bin/env python
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
from __future__ import print_function

if __name__ == '__main__':
    import sys
    import echoclient_ssh
    from twisted.internet.task import react
    react(echoclient_ssh.main, sys.argv[1:])

import os, getpass

from twisted.python.filepath import FilePath
from twisted.python.usage import Options
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import UNIXClientEndpoint
from twisted.conch.ssh.keys import EncryptedKeyError, Key
from twisted.conch.client.knownhosts import KnownHostsFile
from twisted.conch.endpoints import SSHCommandClientEndpoint


class EchoOptions(Options):
    optParameters = [
        ("host", "h", "localhost",
         "hostname of the SSH server to which to connect"),
        ("port", "p", 22, "port number of SSH server to which to connect",
         int),
        ("username", "u", getpass.getuser(),
         "username with which to authenticate with the SSH server"),
Пример #41
0
def _main():
    react(main, sys.argv[1:])
Пример #42
0
    if errorStatus:
        print('%s: %s at %s' % (hostname,
                                errorStatus.prettyPrint(),
                                errorIndex and varBindTable[0][int(errorIndex) - 1][0] or '?'))
    else:
        for varBindRow in varBindTable:
            for varBind in varBindRow:
                print(' = '.join([x.prettyPrint() for x in varBind]))

        if not isEndOfMib(varBindTable[-1]):
            return getbulk(reactor, snmpEngine, *varBindTable[-1])


def failure(errorIndication):
    print(errorIndication)


def getbulk(reactor, snmpEngine, varBinds):
    d = bulkCmd(snmpEngine,
                UsmUserData('usr-none-none'),
                UdpTransportTarget(('demo.snmplabs.com', 161)),
                ContextData(),
                0, 50,
                varBinds)
    d.addCallback(success, reactor, snmpEngine).addErrback(failure)
    return d


react(getbulk, [SnmpEngine(), ObjectType(ObjectIdentity('SNMPv2-MIB', 'system'))])
Пример #43
0
        print("Authenticated.")

        presence = domish.Element((None, 'presence'))
        xs.send(presence)

        self.reactor.callLater(5, xs.sendFooter)


    def init_failed(self, failure):
        print("Initialization failed.")
        print(failure)

        self.xmlstream.sendFooter()



def main(reactor, jid, secret):
    """
    Connect to the given Jabber ID and return a L{Deferred} which will be
    called back when the connection is over.

    @param reactor: The reactor to use for the connection.
    @param jid: A L{JID} to connect to.
    @param secret: A C{str}
    """
    return Client(reactor, JID(jid), secret).finished


if __name__ == '__main__':
    react(main, sys.argv[1:])
Пример #44
0
    parser.add_argument(
        "endpoint",
        action="store",
        help="The Active Directory service endpoint. See "
        "https://twistedmatrix.com/documents/current/core/howto/endpoints.html#clients"
    )
    parser.add_argument("bind_dn",
                        action="store",
                        help="The DN to BIND to the service as.")
    parser.add_argument(
        "passwd_file",
        action="store",
        type=argparse.FileType('r'),
        help="A file containing the password used to log into the service.")
    parser.add_argument("base_dn",
                        action="store",
                        help="The base DN to start from when searching.")
    parser.add_argument("-f", "--filter", action='store', help='LDAP filter')
    parser.add_argument("-p",
                        "--page-size",
                        type=int,
                        action='store',
                        default=100,
                        help='Page size (default 100).')
    parser.add_argument(
        "--start-tls",
        action="store_true",
        help="Request StartTLS after connecting to the service.")
    args = parser.parse_args()
    react(main, [args])
Пример #45
0
        for varBindRow in varBindTable:
            for varBind in varBindRow:
                print(' = '.join([x.prettyPrint() for x in varBind]))

        if not isEndOfMib(varBindTable[-1]):
            return getbulk(reactor, snmpEngine, hostname, *varBindTable[-1])


def failure(errorIndication):
    print(errorIndication)


def getbulk(reactor, snmpEngine, hostname, varBinds):
    d = bulkCmd(snmpEngine, UsmUserData('usr-md5-none', 'authkey1'),
                UdpTransportTarget(hostname), ContextData(), 0, 25, varBinds)
    d.addCallback(success, reactor, snmpEngine, hostname).addErrback(failure)
    return d


def getall(reactor, hostnames):
    snmpEngine = SnmpEngine()

    return DeferredList([
        getbulk(reactor, snmpEngine, hostname,
                ObjectType(ObjectIdentity('SNMPv2-MIB', 'system')))
        for hostname in hostnames
    ])


react(getall, [(('demo.snmplabs.com', 161), ('demo.snmplabs.com', 1161))])
Пример #46
0
def go(f, cfg):
    # note: react() does not return
    return react(_dispatch_command, (cfg, lambda: f(cfg)))
Пример #47
0
from txkoji import Connection
from twisted.internet import defer
from twisted.internet.task import react


@defer.inlineCallbacks
def example(reactor):
    koji = Connection('fedora')
    name = yield koji.cache.tag_name(197)
    # "rawhide"
    print('tag name: %s' % name)


if __name__ == '__main__':
    react(example)
Пример #48
0
from otter.util.http import append_segments, check_success, headers


def get_tenant_ids(token, catalog):
    endpoint = public_endpoint_url(catalog, "cloudMetrics", "IAD")
    d = treq.get(append_segments(endpoint, "metrics", "search"),
                 headers=headers(token),
                 params={"query": "*.*.desired"})
    d.addCallback(check_success, [200])
    d.addCallback(treq.json_content)
    d.addCallback(lambda body: [item["metric"].split(".")[1] for item in body])
    return d


@inlineCallbacks
def main(reactor):
    conf = json.load(open(sys.argv[1]))
    conf = conf["default_attributes"]["otter"]["config"]
    conf["identity"]["strategy"] = "single_tenant"
    conf["identity"]["username"] = os.environ["OTTERPROD_UNAME"]
    conf["identity"]["password"] = os.environ["OTTERPROD_PWD"]
    authenticator = generate_authenticator(reactor, conf["identity"])
    token, catalog = yield authenticator.authenticate_tenant("764771")
    all_tenants = set((yield get_tenant_ids(token, catalog)))
    worker_tenants = all_tenants - set(conf["convergence-tenants"])
    print(*worker_tenants, sep='\n')


if __name__ == '__main__':
    task.react(main, ())
Пример #49
0
def main():
    # logging.basicConfig(level=logging.DEBUG)
    task.react(Producer().run)
Пример #50
0
def start(config_options):
    parser = argparse.ArgumentParser(description="Synapse Admin Command")
    HomeServerConfig.add_arguments_to_parser(parser)

    subparser = parser.add_subparsers(
        title="Admin Commands",
        required=True,
        dest="command",
        metavar="<admin_command>",
        help="The admin command to perform.",
    )
    export_data_parser = subparser.add_parser(
        "export-data", help="Export all data for a user")
    export_data_parser.add_argument("user_id", help="User to extra data from")
    export_data_parser.add_argument(
        "--output-directory",
        action="store",
        metavar="DIRECTORY",
        required=False,
        help=
        "The directory to store the exported data in. Must be empty. Defaults"
        " to creating a temp directory.",
    )
    export_data_parser.set_defaults(func=export_data_command)

    try:
        config, args = HomeServerConfig.load_config_with_parser(
            parser, config_options)
    except ConfigError as e:
        sys.stderr.write("\n" + str(e) + "\n")
        sys.exit(1)

    if config.worker_app is not None:
        assert config.worker_app == "synapse.app.admin_cmd"

    # Update the config with some basic overrides so that don't have to specify
    # a full worker config.
    config.worker_app = "synapse.app.admin_cmd"

    if (not config.worker_daemonize and not config.worker_log_file
            and not config.worker_log_config):
        # Since we're meant to be run as a "command" let's not redirect stdio
        # unless we've actually set log config.
        config.no_redirect_stdio = True

    # Explicitly disable background processes
    config.update_user_directory = False
    config.run_background_tasks = False
    config.start_pushers = False
    config.pusher_shard_config.instances = []
    config.send_federation = False
    config.federation_shard_config.instances = []

    synapse.events.USE_FROZEN_DICTS = config.use_frozen_dicts

    ss = AdminCmdServer(
        config.server_name,
        config=config,
        version_string="Synapse/" + get_version_string(synapse),
    )

    setup_logging(ss, config, use_worker_options=True)

    ss.setup()

    # We use task.react as the basic run command as it correctly handles tearing
    # down the reactor when the deferreds resolve and setting the return value.
    # We also make sure that `_base.start` gets run before we actually run the
    # command.
    async def run():
        with LoggingContext("command"):
            _base.start(ss)
            await args.func(ss, args)

    _base.start_worker_reactor(
        "synapse-admin-cmd",
        config,
        run_command=lambda: task.react(lambda _reactor: defer.ensureDeferred(
            run())),
    )
Пример #51
0
def entry():
    """This is used by a setuptools entry_point. When invoked this way,
    setuptools has already put the installed package on sys.path ."""
    react(run,
          (sys.argv[1:], os.getcwd(), sys.stdout, sys.stderr, sys.argv[0]))
Пример #52
0
def _main():
    """
    This is a magic name for `python -m autobahn`, and specified as
    our entry_point in setup.py
    """
    react(_real_main)
Пример #53
0
                print("FAILED:", e)
                success = False

        for p in [front_proto, back_proto]:
            try:
                p.transport.signalProcess('KILL')
            except ProcessExitedAlready:
                pass

        if not success:
            break
        yield sleep(1)

    print("Killing crossbar")
    try:
        cb_proto.transport.signalProcess('KILL')
        yield cb_proto.all_done
    except:
        pass
    if success:
        print()
        print("Success!")
        print("  ...all the examples neither crashed nor burned...")
        returnValue(0)
    returnValue(5)


from twisted.internet.task import react
if __name__ == '__main__':
    sys.exit(react(main))
Пример #54
0
#!/usr/bin/env python
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

from twisted.internet import ssl, task, protocol, endpoints, defer
from twisted.python.modules import getModule

import echoclient


@defer.inlineCallbacks
def main(reactor):
    factory = protocol.Factory.forProtocol(echoclient.EchoClient)
    certData = getModule(__name__).filePath.sibling("public.pem").getContent()
    authority = ssl.Certificate.loadPEM(certData)
    options = ssl.optionsForClientTLS("example.com", authority)
    endpoint = endpoints.SSL4ClientEndpoint(reactor, "localhost", 8000, options)
    echoClient = yield endpoint.connect(factory)

    done = defer.Deferred()
    echoClient.connectionLost = lambda reason: done.callback(None)
    yield done


if __name__ == "__main__":
    import echoclient_ssl

    task.react(echoclient_ssl.main)
Пример #55
0
        def add2(a, b):
            return a + b

        yield self.register(add2, 'com.example.add2')

        try:
            res = yield self.call('com.example.add2', 2, 3)
            print("result: {}".format(res))
        except Exception as e:
            print("error: {}".format(e))
        finally:
            print('leaving ..')
            #self.leave()


    def on_leave(self, details):
        print('on_leave xx: {}'.format(details))
        self.disconnect()

    def on_disconnect(self):
        print('on_disconnect')


if __name__ == '__main__':

    transports = 'ws://localhost:8080/ws'

    connection = Connection(transports=transports)
    connection.session = MySession
    react(connection.start)
Пример #56
0
def main():
    react(_main, sys.argv[1:])
Пример #57
0

# ==============================================================================
if __name__ == '__main__':
    from threaded_ping_server import start_ping_servers, stop_ping_servers

    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s |%(name)40s |%(message)s',
        datefmt='%H:%M:%S',
        stream=sys.stderr,
    )
    address = ('localhost', 5670)
    servers = start_ping_servers([(address, '10.0.2.15')])
    try:
        task.react(main, argv=[address])
    finally:
        stop_ping_servers(servers)
'''
LOG OUTPUT

10:53:39 |         threaded.ping.tcp-server(5670) |Ping Sim started at tcp://localhost:5670
10:53:39 |               moler.runner.thread-pool |created
10:53:39 |               moler.runner.thread-pool |created own executor <concurrent.futures.thread.ThreadPoolExecutor object at 0x7fde953e93c8>
10:53:39 |                    moler.user.app-code |waiting for data to observe
10:53:39 |                 twisted.tcp-connection |... connected to tcp://127.0.0.1:5670
10:53:39 |threaded.ping.tcp-server(5670 -> 35996) |connection accepted - client at tcp://127.0.0.1:35996
10:53:39 |                 twisted.tcp-connection |<<< b'\n'
10:53:39 |                     moler.7fde953e95c0 |
10:53:40 |                 twisted.tcp-connection |<<< b'greg@debian:~$ ping 10.0.2.15\n'
10:53:40 |                     moler.7fde953e95c0 |greg@debian:~$ ping 10.0.2.15
Пример #58
0
                          params={
                              'foo': 'bar',
                              'baz': 'bax'
                          })
    content = yield treq.content(resp)
    print content

    print 'Multi value dictionary'
    resp = yield treq.get('http://httpbin.org/get',
                          params={'foo': ['bar', 'baz', 'bax']})
    content = yield treq.content(resp)
    print content

    print 'Mixed value dictionary'
    resp = yield treq.get('http://httpbin.org/get',
                          params={
                              'foo': ['bar', 'baz'],
                              'bax': 'quux'
                          })
    content = yield treq.content(resp)
    print content

    print 'Preserved query parameters'
    resp = yield treq.get('http://httpbin.org/get?foo=bar',
                          params={'baz': 'bax'})
    content = yield treq.content(resp)
    print content


react(main, [])
Пример #59
0
def run():
    task.react(oonireport)
Пример #60
0
def change(job):
    print job


def showResult(job):
    print 'Result of %r: %r' % (job, job.result)


def main(reactor, amp_endpoint):
    log.startLogging(sys.stdout)
    pool = MinerPool()

    # start the AMP server
    amp_factory = ServerFactory(pool)
    amp_ep = endpoints.serverFromString(reactor, amp_endpoint)
    amp_ep.listen(amp_factory)

    jobs = [
        Job('a' * 40, 1, 1000),
        Job('b' * 40, 1, 10000),
        Job('c' * 40, 1, 10000),
    ]
    for job in jobs:
        job.notifyOfChange(change)
        job.done.addCallback(showResult)

    return defer.gatherResults(map(pool.mine, jobs))


task.react(main, sys.argv[1:])