Example #1
0
 def basic_setup(self):
     global log, M
     if self.args[0]:
         # Probably being called from the command line - load the config file
         self.config = conf = appconfig('config:%s' % self.args[0],
                                        relative_to=os.getcwd())
         # Configure logging
         try:
             # ... logging does not understand section#subsection syntax
             logging_config = self.args[0].split('#')[0]
             logging.config.fileConfig(logging_config,
                                       disable_existing_loggers=False)
         except Exception:  # pragma no cover
             print >> sys.stderr, (
                 'Could not configure logging with config file %s' %
                 self.args[0])
         log = logging.getLogger('allura.command')
         log.info('Initialize command with config %r', self.args[0])
         load_environment(conf.global_conf, conf.local_conf)
         self.setup_globals()
         from allura import model
         M = model
         ming.configure(**conf)
         pylons.c.user = M.User.anonymous()
     else:
         # Probably being called from another script (websetup, perhaps?)
         log = logging.getLogger('allura.command')
         conf = pylons.config
     self.tools = pylons.g.entry_points['tool'].values()
     for ep in iter_entry_points('allura.command_init'):
         log.info('Running command_init for %s', ep.name)
         ep.load()(conf)
     log.info('Loaded tools')
Example #2
0
 def basic_setup(self):
     global log, M
     if self.args[0]:
         # Probably being called from the command line - load the config
         # file
         self.config = conf = appconfig('config:%s' %
                                        self.args[0], relative_to=os.getcwd())
         # ... logging does not understand section#subsection syntax
         logging_config = self.args[0].split('#')[0]
         logging.config.fileConfig(
             logging_config, disable_existing_loggers=False)
         log = logging.getLogger('allura.command')
         log.info('Initialize command with config %r', self.args[0])
         load_environment(conf.global_conf, conf.local_conf)
         self.setup_globals()
         from allura import model
         M = model
         ming.configure(**conf)
         if asbool(conf.get('activitystream.recording.enabled', False)):
             activitystream.configure(**h.convert_bools(conf, prefix='activitystream.'))
         pylons.tmpl_context.user = M.User.anonymous()
     else:
         # Probably being called from another script (websetup, perhaps?)
         log = logging.getLogger('allura.command')
         conf = pylons.config
     self.tools = pylons.app_globals.entry_points['tool'].values()
     for ep in h.iter_entry_points('allura.command_init'):
         log.info('Running command_init for %s', ep.name)
         ep.load()(conf)
     log.info('Loaded tools')
Example #3
0
    def basic_setup(self):
        global log, M
        if self.args[0]:
            # Probably being called from the command line - load the config file
            self.config = conf = appconfig("config:%s" % self.args[0], relative_to=os.getcwd())
            # ... logging does not understand section#subsection syntax
            logging_config = self.args[0].split("#")[0]
            logging.config.fileConfig(logging_config, disable_existing_loggers=False)
            log = logging.getLogger("allura.command")
            log.info("Initialize command with config %r", self.args[0])
            load_environment(conf.global_conf, conf.local_conf)
            self.setup_globals()
            from allura import model

            M = model
            ming.configure(**conf)
            activitystream.configure(**conf)
            pylons.tmpl_context.user = M.User.anonymous()
        else:
            # Probably being called from another script (websetup, perhaps?)
            log = logging.getLogger("allura.command")
            conf = pylons.config
        self.tools = pylons.app_globals.entry_points["tool"].values()
        for ep in iter_entry_points("allura.command_init"):
            log.info("Running command_init for %s", ep.name)
            ep.load()(conf)
        log.info("Loaded tools")
Example #4
0
def _make_core_app(root, global_conf, full_stack=True, **app_conf):
    """
    Set allura up with the settings found in the PasteDeploy configuration
    file used.

    :param root: The controller module containing the TG root
    :param global_conf: The global settings for allura (those
        defined under the ``[DEFAULT]`` section).
    :type global_conf: dict
    :param full_stack: Should the whole TG2 stack be set up?
    :type full_stack: str or bool
    :return: The allura application with all the relevant middleware
        loaded.

    This is the PasteDeploy factory for the allura application.

    ``app_conf`` contains all the application-specific settings (those defined
    under ``[app:main]``.


    """
    # Run all the initialization code here
    mimetypes.init([pkg_resources.resource_filename('allura', 'etc/mime.types')] + mimetypes.knownfiles)

    # Configure MongoDB
    ming.configure(**app_conf)

    # Configure ActivityStream
    if asbool(app_conf.get('activitystream.recording.enabled', False)):
        activitystream.configure(**h.convert_bools(app_conf, prefix='activitystream.'))

    # Configure EW variable provider
    ew.render.TemplateEngine.register_variable_provider(get_tg_vars)

    # Set FormEncode language to english, as we don't support any other locales
    formencode.api.set_stdtranslation(domain='FormEncode', languages=['en'])

    # Create base app
    base_config = ForgeConfig(root)
    load_environment = base_config.make_load_environment()

    # Code adapted from tg.configuration, replacing the following lines:
    #     make_base_app = base_config.setup_tg_wsgi_app(load_environment)
    #     app = make_base_app(global_conf, full_stack=True, **app_conf)

    # Configure the TG environment
    load_environment(global_conf, app_conf)

    app = tg.TGApp()

    for mw_ep in h.iter_entry_points('allura.middleware'):
        Middleware = mw_ep.load()
        if getattr(Middleware, 'when', 'inner') == 'inner':
            app = Middleware(app, config)

    # Required for sessions
    app = SessionMiddleware(app, config, data_serializer=BeakerPickleSerializerWithLatin1())
    # Handle "Remember me" functionality
    app = RememberLoginMiddleware(app, config)
    # Redirect 401 to the login page
    app = LoginRedirectMiddleware(app)
    # Add instrumentation
    app = AlluraTimerMiddleware(app, app_conf)
    # Clear cookies when the CSRF field isn't posted
    if not app_conf.get('disable_csrf_protection'):
        app = CSRFMiddleware(app, '_session_id')
    if asbool(config.get('cors.enabled', False)):
        # Handle CORS requests
        allowed_methods = aslist(config.get('cors.methods'))
        allowed_headers = aslist(config.get('cors.headers'))
        cache_duration = asint(config.get('cors.cache_duration', 0))
        app = CORSMiddleware(app, allowed_methods, allowed_headers, cache_duration)
    # Setup the allura SOPs
    app = allura_globals_middleware(app)
    # Ensure http and https used per config
    if config.get('override_root') != 'task':
        app = SSLMiddleware(app, app_conf.get('no_redirect.pattern'),
                            app_conf.get('force_ssl.pattern'),
                            app_conf.get('force_ssl.logged_in'))
    # Setup resource manager, widget context SOP
    app = ew.WidgetMiddleware(
        app,
        compress=True,
        use_cache=not asbool(global_conf['debug']),
        script_name=app_conf.get('ew.script_name', '/_ew_resources/'),
        url_base=app_conf.get('ew.url_base', '/_ew_resources/'),
        extra_headers=ast.literal_eval(app_conf.get('ew.extra_headers', '[]')),
        cache_max_age=asint(app_conf.get('ew.cache_header_seconds', 60*60*24*365)),

        # settings to pass through to jinja Environment for EW core widgets
        # these are for the easywidgets' own [easy_widgets.engines] entry point
        # (the Allura [easy_widgets.engines] entry point is named "jinja" (not jinja2) but it doesn't need
        #  any settings since it is a class that uses the same jinja env as the rest of allura)
        **{
            'jinja2.auto_reload': asbool(config['auto_reload_templates']),
            'jinja2.bytecode_cache': AlluraJinjaRenderer._setup_bytecode_cache(),
            'jinja2.cache_size': asint(config.get('jinja_cache_size', -1)),
        }
    )
    # Handle static files (by tool)
    app = StaticFilesMiddleware(app, app_conf.get('static.script_name'))
    # Handle setup and flushing of Ming ORM sessions
    app = MingMiddleware(app)
    # Set up the registry for stacked object proxies (SOPs).
    #    streaming=true ensures they won't be cleaned up till
    #    the WSGI application's iterator is exhausted
    app = RegistryManager(app, streaming=True)

    # "task" wsgi would get a 2nd request to /error/document if we used this middleware
    if config.get('override_root') not in ('task', 'basetest_project_root'):
        if asbool(config['debug']):
            # Converts exceptions to HTTP errors, shows traceback in debug mode
            # don't use TG footer with extra CSS & images that take time to load
            tg.error.footer_html = '<!-- %s %s -->'
            app = tg.error.ErrorHandler(app, global_conf, **config['tg.errorware'])
        else:
            app = ErrorMiddleware(app, config, **config['tg.errorware'])

        app = SetRequestHostFromConfig(app, config)

        # Redirect some status codes to /error/document
        if asbool(config['debug']):
            app = StatusCodeRedirect(app, base_config.handle_status_codes)
        else:
            app = StatusCodeRedirect(
                app, base_config.handle_status_codes + [500])

    for mw_ep in h.iter_entry_points('allura.middleware'):
        Middleware = mw_ep.load()
        if getattr(Middleware, 'when', 'inner') == 'outer':
            app = Middleware(app, config)

    return app
Example #5
0
def _make_core_app(root, global_conf, full_stack=True, **app_conf):
    """
    Set allura up with the settings found in the PasteDeploy configuration
    file used.

    :param root: The controller module containing the TG root
    :param global_conf: The global settings for allura (those
        defined under the ``[DEFAULT]`` section).
    :type global_conf: dict
    :param full_stack: Should the whole TG2 stack be set up?
    :type full_stack: str or bool
    :return: The allura application with all the relevant middleware
        loaded.

    This is the PasteDeploy factory for the allura application.

    ``app_conf`` contains all the application-specific settings (those defined
    under ``[app:main]``.


    """
    # Run all the initialization code here
    mimetypes.init([pkg_resources.resource_filename('allura', 'etc/mime.types')] + mimetypes.knownfiles)

    # Configure MongoDB
    ming.configure(**app_conf)

    # Configure ActivityStream
    if asbool(app_conf.get('activitystream.recording.enabled', False)):
        activitystream.configure(**h.convert_bools(app_conf, prefix='activitystream.'))

    # Configure EW variable provider
    ew.render.TemplateEngine.register_variable_provider(get_tg_vars)

    # Set FormEncode language to english, as we don't support any other locales
    formencode.api.set_stdtranslation(domain='FormEncode', languages=['en'])

    # Create base app
    base_config = ForgeConfig(root)
    load_environment = base_config.make_load_environment()

    # Code adapted from tg.configuration, replacing the following lines:
    #     make_base_app = base_config.setup_tg_wsgi_app(load_environment)
    #     app = make_base_app(global_conf, full_stack=True, **app_conf)

    # Configure the Pylons environment
    load_environment(global_conf, app_conf)

    app = tg.TGApp()

    for mw_ep in h.iter_entry_points('allura.middleware'):
        Middleware = mw_ep.load()
        if getattr(Middleware, 'when', 'inner') == 'inner':
            app = Middleware(app, config)

    # Required for pylons
    app = RoutesMiddleware(app, config['routes.map'])
    # Required for sessions
    app = SessionMiddleware(app, config)
    # Handle "Remember me" functionality
    app = RememberLoginMiddleware(app, config)
    # Redirect 401 to the login page
    app = LoginRedirectMiddleware(app)
    # Add instrumentation
    app = AlluraTimerMiddleware(app, app_conf)
    # Clear cookies when the CSRF field isn't posted
    if not app_conf.get('disable_csrf_protection'):
        app = CSRFMiddleware(app, '_session_id')
    if asbool(config.get('cors.enabled', False)):
        # Handle CORS requests
        allowed_methods = aslist(config.get('cors.methods'))
        allowed_headers = aslist(config.get('cors.headers'))
        cache_duration = asint(config.get('cors.cache_duration', 0))
        app = CORSMiddleware(app, allowed_methods, allowed_headers, cache_duration)
    # Setup the allura SOPs
    app = allura_globals_middleware(app)
    # Ensure http and https used per config
    if config.get('override_root') != 'task':
        app = SSLMiddleware(app, app_conf.get('no_redirect.pattern'),
                            app_conf.get('force_ssl.pattern'),
                            app_conf.get('force_ssl.logged_in'))
    # Setup resource manager, widget context SOP
    app = ew.WidgetMiddleware(
        app,
        compress=not asbool(global_conf['debug']),
        # compress=True,
        script_name=app_conf.get('ew.script_name', '/_ew_resources/'),
        url_base=app_conf.get('ew.url_base', '/_ew_resources/'),
        extra_headers=eval(app_conf.get('ew.extra_headers', 'None')),
        cache_max_age=asint(app_conf.get('ew.cache_header_seconds', 60*60*24*365)),
    )
    # Handle static files (by tool)
    app = StaticFilesMiddleware(app, app_conf.get('static.script_name'))
    # Handle setup and flushing of Ming ORM sessions
    app = MingMiddleware(app)
    # Set up the registry for stacked object proxies (SOPs).
    #    streaming=true ensures they won't be cleaned up till
    #    the WSGI application's iterator is exhausted
    app = RegistryManager(app, streaming=True)

    # "task" wsgi would get a 2nd request to /error/document if we used this middleware
    if config.get('override_root') != 'task':
        # Converts exceptions to HTTP errors, shows traceback in debug mode
        # don't use TG footer with extra CSS & images that take time to load
        tg.error.footer_html = '<!-- %s %s -->'
        app = tg.error.ErrorHandler(app, global_conf, **config['pylons.errorware'])

        app = SetRequestHostFromConfig(app, config)

        # Redirect some status codes to /error/document
        if asbool(config['debug']):
            app = StatusCodeRedirect(app, base_config.handle_status_codes)
        else:
            app = StatusCodeRedirect(
                app, base_config.handle_status_codes + [500])

    for mw_ep in h.iter_entry_points('allura.middleware'):
        Middleware = mw_ep.load()
        if getattr(Middleware, 'when', 'inner') == 'outer':
            app = Middleware(app, config)

    return app
Example #6
0
def setup_app(command, conf, vars):
    """Place any commands to setup allura here"""
    load_environment(conf.global_conf, conf.local_conf)
    setup_schema(command, conf, vars)
    bootstrap.bootstrap(command, conf, vars)
Example #7
0
def _make_core_app(root, global_conf, full_stack=True, **app_conf):
    """
    Set allura up with the settings found in the PasteDeploy configuration
    file used.

    :param root: The controller module containing the TG root
    :param global_conf: The global settings for allura (those
        defined under the ``[DEFAULT]`` section).
    :type global_conf: dict
    :param full_stack: Should the whole TG2 stack be set up?
    :type full_stack: str or bool
    :return: The allura application with all the relevant middleware
        loaded.

    This is the PasteDeploy factory for the allura application.

    ``app_conf`` contains all the application-specific settings (those defined
    under ``[app:main]``.


    """
    # Run all the initialization code here
    mimetypes.init([pkg_resources.resource_filename("allura", "etc/mime.types")] + mimetypes.knownfiles)
    patches.apply()
    # Configure MongoDB
    ming.configure(**app_conf)

    # Configure ActivityStream
    activitystream.configure(**app_conf)

    # Configure EW variable provider
    ew.render.TemplateEngine.register_variable_provider(get_tg_vars)

    # Create base app
    base_config = ForgeConfig(root)
    load_environment = base_config.make_load_environment()

    # Code adapted from tg.configuration, replacing the following lines:
    #     make_base_app = base_config.setup_tg_wsgi_app(load_environment)
    #     app = make_base_app(global_conf, full_stack=True, **app_conf)

    # Configure the Pylons environment
    load_environment(global_conf, app_conf)

    if config.get("zarkov.host"):
        try:
            import zmq
        except ImportError:
            raise ImportError, "Unable to import the zmq library. Please" " check that zeromq is installed or comment out" " the zarkov.host setting in your ini file."

    app = tg.TGApp()
    if asbool(config.get("auth.method", "local") == "sfx"):
        import sfx.middleware

        d = h.config_with_prefix(config, "auth.")
        d.update(h.config_with_prefix(config, "sfx."))
        app = sfx.middleware.SfxMiddleware(app, d)
    # Required for pylons
    app = RoutesMiddleware(app, config["routes.map"])
    # Required for sessions
    app = SessionMiddleware(app, config)
    # Converts exceptions to HTTP errors, shows traceback in debug mode
    app = tg.error.ErrorHandler(app, global_conf, **config["pylons.errorware"])
    # Redirect some status codes to /error/document
    if asbool(config["debug"]):
        app = StatusCodeRedirect(app, base_config.handle_status_codes)
    else:
        app = StatusCodeRedirect(app, base_config.handle_status_codes + [500])
    # Redirect 401 to the login page
    app = LoginRedirectMiddleware(app)
    # Add instrumentation
    app = AlluraTimerMiddleware(app, app_conf)
    # Clear cookies when the CSRF field isn't posted
    if not app_conf.get("disable_csrf_protection"):
        app = CSRFMiddleware(app, "_session_id")
    # Setup the allura SOPs
    app = allura_globals_middleware(app)
    # Ensure https for logged in users, http for anonymous ones
    if asbool(app_conf.get("auth.method", "local") == "sfx"):
        app = SSLMiddleware(app, app_conf.get("no_redirect.pattern"))
    # Setup resource manager, widget context SOP
    app = ew.WidgetMiddleware(
        app,
        compress=not asbool(global_conf["debug"]),
        # compress=True,
        script_name=app_conf.get("ew.script_name", "/_ew_resources/"),
        url_base=app_conf.get("ew.url_base", "/_ew_resources/"),
        extra_headers=eval(app_conf.get("ew.extra_headers", "None")),
    )
    # Make sure that the wsgi.scheme is set appropriately when we
    # have the funky HTTP_X_SFINC_SSL  environ var
    if asbool(app_conf.get("auth.method", "local") == "sfx"):
        app = set_scheme_middleware(app)
    # Handle static files (by tool)
    app = StaticFilesMiddleware(app, app_conf.get("static.script_name"))
    # Handle setup and flushing of Ming ORM sessions
    app = MingMiddleware(app)
    # Set up the registry for stacked object proxies (SOPs).
    #    streaming=true ensures they won't be cleaned up till
    #    the WSGI application's iterator is exhausted
    app = RegistryManager(app, streaming=True)
    return app
Example #8
0
def setup_app(command, conf, vars):
    """Place any commands to setup allura here"""
    load_environment(conf.global_conf, conf.local_conf)
    setup_schema(command, conf, vars)
    bootstrap.bootstrap(command, conf, vars)
Example #9
0
def _make_core_app(root, global_conf, full_stack=True, **app_conf):
    """
    Set allura up with the settings found in the PasteDeploy configuration
    file used.

    :param root: The controller module containing the TG root
    :param global_conf: The global settings for allura (those
        defined under the ``[DEFAULT]`` section).
    :type global_conf: dict
    :param full_stack: Should the whole TG2 stack be set up?
    :type full_stack: str or bool
    :return: The allura application with all the relevant middleware
        loaded.

    This is the PasteDeploy factory for the allura application.

    ``app_conf`` contains all the application-specific settings (those defined
    under ``[app:main]``.


    """
    # Run all the initialization code here
    mimetypes.init(
        [pkg_resources.resource_filename('allura', 'etc/mime.types')]
        + mimetypes.knownfiles)
    patches.apply()
    try:
        import newrelic
    except ImportError:
        pass
    else:
        patches.newrelic()
    # Configure MongoDB
    ming.configure(**app_conf)

    # Configure ActivityStream
    if asbool(app_conf.get('activitystream.recording.enabled', False)):
        activitystream.configure(**app_conf)

    # Configure EW variable provider
    ew.render.TemplateEngine.register_variable_provider(get_tg_vars)
    
    # Set FormEncode language to english, as we don't support any other locales
    formencode.api.set_stdtranslation(domain='FormEncode', languages=['en'])

    # Create base app
    base_config = ForgeConfig(root)
    load_environment = base_config.make_load_environment()

    # Code adapted from tg.configuration, replacing the following lines:
    #     make_base_app = base_config.setup_tg_wsgi_app(load_environment)
    #     app = make_base_app(global_conf, full_stack=True, **app_conf)

    # Configure the Pylons environment
    load_environment(global_conf, app_conf)

    if config.get('zarkov.host'):
        try:
            import zmq
        except ImportError:
            raise ImportError, "Unable to import the zmq library. Please"\
                               " check that zeromq is installed or comment out"\
                               " the zarkov.host setting in your ini file."

    app = tg.TGApp()
    if asbool(config.get('auth.method', 'local')=='sfx'):
        import sfx.middleware
        d = h.config_with_prefix(config, 'auth.')
        d.update(h.config_with_prefix(config, 'sfx.'))
        app = sfx.middleware.SfxMiddleware(app, d)
    # Required for pylons
    app = RoutesMiddleware(app, config['routes.map'])
    # Required for sessions
    app = SessionMiddleware(app, config)
    # Redirect 401 to the login page
    app = LoginRedirectMiddleware(app)
    # Add instrumentation
    app = AlluraTimerMiddleware(app, app_conf)
    # Clear cookies when the CSRF field isn't posted
    if not app_conf.get('disable_csrf_protection'):
        app = CSRFMiddleware(app, '_session_id')
    # Setup the allura SOPs
    app = allura_globals_middleware(app)
    # Ensure https for logged in users, http for anonymous ones
    if asbool(app_conf.get('auth.method', 'local')=='sfx'):
        app = SSLMiddleware(app, app_conf.get('no_redirect.pattern'))
    # Setup resource manager, widget context SOP
    app = ew.WidgetMiddleware(
        app,
        compress=not asbool(global_conf['debug']),
        # compress=True,
        script_name=app_conf.get('ew.script_name', '/_ew_resources/'),
        url_base=app_conf.get('ew.url_base', '/_ew_resources/'),
        extra_headers=eval(app_conf.get('ew.extra_headers', 'None')))
    # Handle static files (by tool)
    app = StaticFilesMiddleware(app, app_conf.get('static.script_name'))
    # Handle setup and flushing of Ming ORM sessions
    app = MingMiddleware(app)
    # Set up the registry for stacked object proxies (SOPs).
    #    streaming=true ensures they won't be cleaned up till
    #    the WSGI application's iterator is exhausted
    app = RegistryManager(app, streaming=True)
    # Converts exceptions to HTTP errors, shows traceback in debug mode
    tg.error.footer_html = '<!-- %s %s -->'  # don't use TG footer with extra CSS & images that take time to load
    app = tg.error.ErrorHandler(app, global_conf, **config['pylons.errorware'])
    # Make sure that the wsgi.scheme is set appropriately when we
    # have the funky HTTP_X_SFINC_SSL  environ var
    if asbool(app_conf.get('auth.method', 'local')=='sfx'):
        app = set_scheme_middleware(app)
    # Redirect some status codes to /error/document
    if config.get('override_root') != 'task':
        # "task" wsgi would get a 2nd request to /error/document if we used this middleware
        if asbool(config['debug']):
            app = StatusCodeRedirect(app, base_config.handle_status_codes)
        else:
            app = StatusCodeRedirect(app, base_config.handle_status_codes + [500])
    return app