Exemplo n.º 1
0
 def _create_api(self, opts, args):
     """Create an API, as requested by the command."""
     options = {}
     options['documentroot'] = opts.docroot
     options['configfile'] = opts.cfgfile
     options['logfile'] = opts.logfile
     options = init_options(options)
     init_logger(options)
     init_codecs(options)
     api = API()
     if 'options' in self.require_api:
         api.options = options
     if 'logger' in self.require_api:
         api.logger = logging.getLogger('draco2.site')
     if 'config' in self.require_api:
         api.config = Config._create(api)
     if 'loader' in self.require_api:
         api.loader = Loader._create(api)
     if 'events' in self.require_api:
         api.events = EventManager._create(api)
     if 'database' in self.require_api:
         api.database = DatabaseManager._create(api)
         if opts.dsn:
             api.database._set_dsn(dsn)
     if 'models' in self.require_api:
         api.models = ModelManager._create(api)
     return api
Exemplo n.º 2
0
 def setup_method(cls, method):
     tempdir = tempfile.gettempdir()
     subdir = 'dracotest_%d' % os.getpid()
     docroot = os.path.join(tempdir, subdir)
     os.mkdir(docroot)
     cls.docroot = docroot
     cls.files = []
     cls.directories = [docroot]
     options = { 'documentroot': docroot }
     options = init_options(options)
     config = Config(options)
     config.add_file(os.environ['TESTCONFIG'])
     cls.config = config
     loader = Loader()
     loader.add_scope('__docroot__', docroot)
     cls.loader = loader
Exemplo n.º 3
0
def handle_request(iface):
    """Handle a request."""
    # Acquire the read-write lock. This lock is held by all threads in
    # read mode when serving a Draco request. If one of the global Draco
    # objects needs to be reloaded, the lock is upgraded to write mode
    # by the thread that detected the reload condition. All threads will
    # then serialize and the reload can continue.
    rwlock.acquire_read()
    try:
        # Once off initialization. No user code can be run from this, and
        # there's no option of recording errors.
        initlock.acquire()
        try:
            global initialized, options
            if not initialized:
                options = init_options(iface.options())
                init_logger(options)
                init_codecs(options)
                initialized = True
        finally:
            initlock.release()

        # Dispatch the request. Errors are handled from now on.
        debug = options.get('debug')
        profile = options.get('profile')
        logger = logging.getLogger('draco2.core.dispatch')
        try:
            agent = iface.headers_in().get('user-agent', [''])[0]
            logger.debug('Request: %s (%s)' % (iface.uri(), agent))
            t1 = time.time()
            if profile:
                import lsprof
                stats = lsprof.profile(dispatch_request, iface)
                stats.sort('inlinetime')
                io = StringIO()
                stats.pprint(top=20, file=io)
                logger.debug(io.getvalue())
                stats.sort('totaltime')
                io = StringIO()
                stats.pprint(top=20, file=io)
                logger.debug(io.getvalue())
            else:
                dispatch_request(iface)
            t2 = time.time()
            logger.debug('Total time spent: %.2f (%s)' % ((t2 - t1), iface.uri()))
            return

        except HTTPResponse, exc:
            status = exc.status
            headers = exc.headers
            message = http.http_reason_strings[status]
            if not hasattr(exc, 'backtrace') or not exc.backtrace:
                exc.backtrace = get_backtrace()
            errorname = 'error_response_%03d' % status
            exception = exc

        except (StandardError, DracoError), exc:
            status = http.HTTP_INTERNAL_SERVER_ERROR
            headers = {}
            if not hasattr(exc, 'backtrace') or not exc.backtrace:
                exc.backtrace = get_backtrace()
            if debug:
                message = exc.backtrace
            else:
                message = http.http_reason_strings[status]
            errorname = 'uncaught_exception'
            exception = exc
            logger.error('A uncaught exception occurred. Backtrace follows.')
            logger.error(exc.backtrace)