Example #1
0
 def _redis_store(self):
     store = RedisStore()
     store.rc = mockredis.mock_redis_client()
     return store
Example #2
0
    loglevel = logging.WARN
    logfile = None
    modules = []
    for o, a in opts:
        if o in ('-h', '--help'):
            print __doc__
            sys.exit(0)
        elif o in '--loglevel':
            loglevel = getattr(logging, a.upper(), None)
            if not isinstance(loglevel, int):
                raise ValueError('Invalid log level: %s' % a)
        elif o in '--logfile':
            logfile = a
        elif o in '-R':
            from pyff.store import RedisStore
            store = RedisStore()
        elif o in ('-m', '--module'):
            modules.append(a)
        elif o in '--version':
            print "pyff version %s" % __version__
            sys.exit(0)

    log_args = {'level': loglevel}
    if logfile is not None:
        log_args['filename'] = logfile
    logging.basicConfig(**log_args)

    modules.append('pyff.builtins')
    for mn in modules:
        importlib.import_module(mn)
Example #3
0
def main():
    """
    The main entrypoint for the pyffd command.
    """
    try:
        opts, args = getopt.getopt(sys.argv[1:],
                                   'hP:p:H:CfaA:l:Rm:',
                                   ['help', 'loglevel=', 'log=', 'access-log=', 'error-log=',
                                    'port=', 'host=', 'no-caching', 'autoreload', 'frequency=', 'modules=',
                                    'alias=', 'dir=', 'version', 'proxy', 'allow_shutdown'])
    except getopt.error as msg:
        print(msg)
        print(__doc__)
        sys.exit(2)

    if config.store is None:
        config.store = MemoryStore()

    if config.loglevel is None:
        config.loglevel = logging.INFO

    if config.aliases is None:
        config.aliases = dict()

    if config.modules is None:
        config.modules = []

    try:  # pragma: nocover
        for o, a in opts:
            if o in ('-h', '--help'):
                print(__doc__)
                sys.exit(0)
            elif o == '--loglevel':
                config.loglevel = getattr(logging, a.upper(), None)
                if not isinstance(config.loglevel, int):
                    raise ValueError('Invalid log level: %s' % config.loglevel)
            elif o in ('--log', '-l'):
                config.error_log = a
                config.access_log = a
            elif o in '--error-log':
                config.error_log = a
            elif o in '--access-log':
                config.access_log = a
            elif o in ('--host', '-H'):
                config.bind_address = a
            elif o in ('--port', '-P'):
                config.port = int(a)
            elif o in ('--pidfile', '-p'):
                config.pid_file = a
            elif o in '-R':
                config.store = RedisStore()
            elif o in ('--no-caching', '-C'):
                config.caching_enabled = False
            elif o in ('--caching-delay', 'D'):
                config.caching_delay = int(o)
            elif o in ('--foreground', '-f'):
                config.daemonize = False
            elif o in ('--autoreload', '-a'):
                config.autoreload = True
            elif o in '--frequency':
                config.frequency = int(a)
            elif o in ('-A', '--alias'):
                (a, colon, uri) = a.partition(':')
                assert (colon == ':')
                if a and uri:
                    config.aliases[a] = uri
            elif o in '--dir':
                config.base_dir = a
            elif o in '--proxy':
                config.proxy = True
            elif o in '--allow_shutdown':
                config.allow_shutdown = True
            elif o in ('-m', '--module'):
                config.modules.append(a)
            elif o in '--version':
                print("pyffd version {} (cherrypy version {})".format(pyff_version, cherrypy.__version__))
                sys.exit(0)
            else:
                raise ValueError("Unknown option '%s'" % o)

    except Exception as ex:
        print(ex)
        print(__doc__)
        sys.exit(3)

    engine = cherrypy.engine
    plugins = cherrypy.process.plugins

    if config.daemonize:
        cherrypy.config.update({'environment': 'production'})
        cherrypy.config.update({'log.screen': False})
        if config.error_log is None:
            config.error_log = 'syslog:daemon'
        if config.access_log is None:
            config.access_log = 'syslog:daemon'
        plugins.Daemonizer(engine).subscribe()

    if config.base_dir is not None:
        DirPlugin(engine, config.base_dir).subscribe()

    if config.pid_file:
        plugins.PIDFile(engine, config.pid_file).subscribe()

    def _b64(p):
        if p:
            return "{base64}%s" % p.encode('base64')
        else:
            return ""

    def error_page(code, **kwargs):
        return render_template("%d.html" % code, **kwargs)

    observers = []

    if config.loglevel == logging.DEBUG:
        observers.append(debug_observer)

    config.modules.append('pyff.builtins')
    for mn in config.modules:
        importlib.import_module(mn)

    server = MDServer(pipes=args, observers=observers)

    pfx = ["/entities", "/metadata"] + ["/" + x for x in server.aliases.keys()]
    cfg = {
        'global': {
            'tools.encode.encoding': 'UTF-8',
            'server.socket_port': config.port,
            'server.socket_host': config.bind_address,
            'tools.caching.on': config.caching_enabled,
            'tools.caching.debug': config.caching_enabled,
            'tools.trailing_slash.on': True,
            'tools.caching.maxobj_size': 1000000000000,  # effectively infinite
            'tools.caching.maxsize': 1000000000000,
            'tools.caching.antistampede_timeout': 30,
            'tools.caching.delay': 3600,  # this is how long we keep static stuff
            'tools.cpstats.on': True,
            'tools.proxy.on': config.proxy,
            'allow_shutdown': config.allow_shutdown,
            'error_page.404': lambda **kwargs: error_page(404, _=_, **kwargs),
            'error_page.503': lambda **kwargs: error_page(503, _=_, **kwargs),
            'error_page.500': lambda **kwargs: error_page(500, _=_, **kwargs),
            'error_page.400': lambda **kwargs: error_page(400, _=_, **kwargs)
        },
        '/': {
            'tools.caching.delay': config.caching_delay,
            'tools.cpstats.on': True,
            'tools.proxy.on': config.proxy,
            'request.dispatch': EncodingDispatcher(pfx, _b64).dispatch,
            'request.dispatpch.debug': True,
        },
        '/static': {
            'tools.cpstats.on': True,
            'tools.caching.on': config.caching_enabled,
            'tools.caching.delay': config.caching_delay,
            'tools.proxy.on': config.proxy
        },
        '/shutdown': {
            'allow_shutdown': config.allow_shutdown
        }
    }
    cherrypy.config.update(cfg)

    if config.error_log is not None:
        cherrypy.config.update({'log.screen': False})

    root = MDRoot(server)
    app = cherrypy.tree.mount(root, config=cfg)
    if config.error_log is not None:
        if config.error_log.startswith('syslog:'):
            facility = config.error_log[7:]
            h = SysLogLibHandler(facility=facility)
            app.log.error_log.addHandler(h)
            cherrypy.config.update({'log.error_file': ''})
        else:
            cherrypy.config.update({'log.error_file': config.error_log})

    if config.access_log is not None:
        if config.access_log.startswith('syslog:'):
            facility = config.error_log[7:]
            h = SysLogLibHandler(facility=facility)
            app.log.access_log.addHandler(h)
            cherrypy.config.update({'log.access_file': ''})
        else:
            cherrypy.config.update({'log.access_file': config.access_log})

    app.log.error_log.setLevel(config.loglevel)

    engine.signals.subscribe()
    try:
        engine.start()
    except Exception as ex:
        logging.error(ex)
        sys.exit(1)
    else:
        engine.block()
Example #4
0
 def _redis_store(self):
     store = RedisStore()
     store.rc = mockredis.mock_redis_client()
     return store
Example #5
0
File: md.py Project: lhoekenga/pyFF
def main():
    """
    The main entrypoint for the pyFF cmdline tool.
    """

    opts = None
    args = None
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], 'hRm',
            ['help', 'loglevel=', 'logfile=', 'version', 'module'])
    except getopt.error as msg:
        print(msg)
        print(__doc__)
        sys.exit(2)

    if config.store is None:
        config.store = MemoryStore()

    if config.loglevel is None:
        config.loglevel = logging.WARN

    if config.modules is None:
        config.modules = []

    for o, a in opts:
        if o in ('-h', '--help'):
            print(__doc__)
            sys.exit(0)
        elif o in '--loglevel':
            config.loglevel = getattr(logging, a.upper(), None)
            if not isinstance(config.loglevel, int):
                raise ValueError('Invalid log level: %s' % a)
        elif o in '--logfile':
            config.logfile = a
        elif o in '-R':
            from pyff.store import RedisStore
            config.store = RedisStore()
        elif o in ('-m', '--module'):
            config.modules.append(a)
        elif o in '--version':
            print("pyff version {}".format(__version__))
            sys.exit(0)

    log_args = {'level': config.loglevel}
    if config.logfile is not None:
        log_args['filename'] = config.logfile
    logging.basicConfig(**log_args)

    config.modules.append('pyff.builtins')
    for mn in config.modules:
        importlib.import_module(mn)

    try:
        md = MDRepository(store=config.store)
        for p in args:
            plumbing(p).process(md, state={'batch': True, 'stats': {}})
        sys.exit(0)
    except Exception as ex:
        if logging.getLogger().isEnabledFor(logging.DEBUG):
            traceback.print_exc()
        logging.error(ex)
        sys.exit(-1)