Example #1
0
    def setUpClass(cls):
        class RaiseExceptionHandler(Component):
            implements(IRequestHandler)

            def match_request(self, req):
                if req.path_info.startswith('/raise-exception'):
                    return True

            def process_request(self, req):
                if req.args.get('type') == 'tracerror':
                    raise TracError("The TracError message")
                else:
                    raise Exception("The Exception message")

        cls.components = [RaiseExceptionHandler]
        cls.env_path = os.path.join(mkdtemp(), 'env')
        env = trac.env.Environment(path=cls.env_path, create=True)
        PermissionSystem(env).grant_permission('admin', 'TRAC_ADMIN')
        env.shutdown()
Example #2
0
def open_environment(env_path=None, use_cache=False):
    # XXX copy and paste the god-damned code 
    # might as well write FORTRAN at this point
    """Open an existing environment object, and verify that the database is up
    to date.

    @param env_path: absolute path to the environment directory; if ommitted,
                     the value of the `TRAC_ENV` environment variable is used
    @param use_cache: whether the environment should be cached for subsequent
                      invocations of this function
    @return: the `Environment` object
    """
    global env_cache, env_cache_lock

    if not env_path:
        env_path = os.getenv('TRAC_ENV')
    if not env_path:
        raise TracError(_('Missing environment variable "TRAC_ENV". '
                          'Trac requires this variable to point to a valid '
                          'Trac environment.'))

    env_path = os.path.normcase(os.path.normpath(env_path))
    if use_cache:
        env_cache_lock.acquire()
        try:
            env = env_cache.get(env_path)
            if env and env.config.parse_if_needed():
                # The environment configuration has changed, so shut it down
                # and remove it from the cache so that it gets reinitialized
                env.log.info('Reloading environment due to configuration '
                             'change')
                env.shutdown()
                if hasattr(env.log, '_trac_handler'):
                    hdlr = env.log._trac_handler
                    env.log.removeHandler(hdlr)
                    hdlr.close()
                del env_cache[env_path]
                env = None
            if env is None:
                env = env_cache.setdefault(env_path, open_environment(env_path))
        finally:
            env_cache_lock.release()
    else:
        env = Environment(env_path)
        needs_upgrade = False
        try:
            needs_upgrade = env.needs_upgrade()
        except Exception, e: # e.g. no database connection
            env.log.error("Exception caught while checking for upgrade: %s",
                          exception_to_unicode(e, traceback=True))
        if needs_upgrade:

            if env.is_component_enabled(AutoUpgrade):
                try:
                    env.upgrade(backup=True)
                except TracError, e:
                    env.upgrade()
            else:
                raise TracError(_('The Trac Environment needs to be upgraded.\n\n'
                                  'Run "trac-admin %(path)s upgrade"',
                                  path=env_path))