##// END OF EJS Templates
Added option to swap the db connection uri based on ENV TEST_DB string
marcink -
r2560:380d6d86 beta
parent child Browse files
Show More
@@ -1,97 +1,100
1 """Pylons environment configuration"""
1 """Pylons environment configuration"""
2
2
3 import os
3 import os
4 import logging
4 import logging
5 import rhodecode
5 import rhodecode
6
6
7 from mako.lookup import TemplateLookup
7 from mako.lookup import TemplateLookup
8 from pylons.configuration import PylonsConfig
8 from pylons.configuration import PylonsConfig
9 from pylons.error import handle_mako_error
9 from pylons.error import handle_mako_error
10
10
11 # don't remove this import it does magic for celery
11 # don't remove this import it does magic for celery
12 from rhodecode.lib import celerypylons
12 from rhodecode.lib import celerypylons
13
13
14 import rhodecode.lib.app_globals as app_globals
14 import rhodecode.lib.app_globals as app_globals
15
15
16 from rhodecode.config.routing import make_map
16 from rhodecode.config.routing import make_map
17
17
18 from rhodecode.lib import helpers
18 from rhodecode.lib import helpers
19 from rhodecode.lib.auth import set_available_permissions
19 from rhodecode.lib.auth import set_available_permissions
20 from rhodecode.lib.utils import repo2db_mapper, make_ui, set_rhodecode_config,\
20 from rhodecode.lib.utils import repo2db_mapper, make_ui, set_rhodecode_config,\
21 load_rcextensions
21 load_rcextensions
22 from rhodecode.lib.utils2 import engine_from_config, str2bool
22 from rhodecode.lib.utils2 import engine_from_config, str2bool
23 from rhodecode.model import init_model
23 from rhodecode.model import init_model
24 from rhodecode.model.scm import ScmModel
24 from rhodecode.model.scm import ScmModel
25
25
26 log = logging.getLogger(__name__)
26 log = logging.getLogger(__name__)
27
27
28
28
29 def load_environment(global_conf, app_conf, initial=False):
29 def load_environment(global_conf, app_conf, initial=False):
30 """
30 """
31 Configure the Pylons environment via the ``pylons.config``
31 Configure the Pylons environment via the ``pylons.config``
32 object
32 object
33 """
33 """
34 config = PylonsConfig()
34 config = PylonsConfig()
35
35
36 # Pylons paths
36 # Pylons paths
37 root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
37 root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
38 paths = dict(
38 paths = dict(
39 root=root,
39 root=root,
40 controllers=os.path.join(root, 'controllers'),
40 controllers=os.path.join(root, 'controllers'),
41 static_files=os.path.join(root, 'public'),
41 static_files=os.path.join(root, 'public'),
42 templates=[os.path.join(root, 'templates')]
42 templates=[os.path.join(root, 'templates')]
43 )
43 )
44
44
45 # Initialize config with the basic options
45 # Initialize config with the basic options
46 config.init_app(global_conf, app_conf, package='rhodecode', paths=paths)
46 config.init_app(global_conf, app_conf, package='rhodecode', paths=paths)
47
47
48 # store some globals into rhodecode
48 # store some globals into rhodecode
49 rhodecode.CELERY_ON = str2bool(config['app_conf'].get('use_celery'))
49 rhodecode.CELERY_ON = str2bool(config['app_conf'].get('use_celery'))
50 rhodecode.CELERY_EAGER = str2bool(config['app_conf'].get('celery.always.eager'))
50 rhodecode.CELERY_EAGER = str2bool(config['app_conf'].get('celery.always.eager'))
51
51
52 config['routes.map'] = make_map(config)
52 config['routes.map'] = make_map(config)
53 config['pylons.app_globals'] = app_globals.Globals(config)
53 config['pylons.app_globals'] = app_globals.Globals(config)
54 config['pylons.h'] = helpers
54 config['pylons.h'] = helpers
55 rhodecode.CONFIG = config
55 rhodecode.CONFIG = config
56
56
57 load_rcextensions(root_path=config['here'])
57 load_rcextensions(root_path=config['here'])
58
58
59 # Setup cache object as early as possible
59 # Setup cache object as early as possible
60 import pylons
60 import pylons
61 pylons.cache._push_object(config['pylons.app_globals'].cache)
61 pylons.cache._push_object(config['pylons.app_globals'].cache)
62
62
63 # Create the Mako TemplateLookup, with the default auto-escaping
63 # Create the Mako TemplateLookup, with the default auto-escaping
64 config['pylons.app_globals'].mako_lookup = TemplateLookup(
64 config['pylons.app_globals'].mako_lookup = TemplateLookup(
65 directories=paths['templates'],
65 directories=paths['templates'],
66 error_handler=handle_mako_error,
66 error_handler=handle_mako_error,
67 module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
67 module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
68 input_encoding='utf-8', default_filters=['escape'],
68 input_encoding='utf-8', default_filters=['escape'],
69 imports=['from webhelpers.html import escape'])
69 imports=['from webhelpers.html import escape'])
70
70
71 # sets the c attribute access when don't existing attribute are accessed
71 # sets the c attribute access when don't existing attribute are accessed
72 config['pylons.strict_tmpl_context'] = True
72 config['pylons.strict_tmpl_context'] = True
73 test = os.path.split(config['__file__'])[-1] == 'test.ini'
73 test = os.path.split(config['__file__'])[-1] == 'test.ini'
74 if test:
74 if test:
75 if os.environ.get('TEST_DB'):
76 # swap config if we pass enviroment variable
77 config['sqlalchemy.db1.url'] = os.environ.get('TEST_DB')
78
75 from rhodecode.lib.utils import create_test_env, create_test_index
79 from rhodecode.lib.utils import create_test_env, create_test_index
76 from rhodecode.tests import TESTS_TMP_PATH
80 from rhodecode.tests import TESTS_TMP_PATH
77 create_test_env(TESTS_TMP_PATH, config)
81 create_test_env(TESTS_TMP_PATH, config)
78 create_test_index(TESTS_TMP_PATH, config, True)
82 create_test_index(TESTS_TMP_PATH, config, True)
79
83
80 # MULTIPLE DB configs
84 # MULTIPLE DB configs
81 # Setup the SQLAlchemy database engine
85 # Setup the SQLAlchemy database engine
82 sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.')
86 sa_engine_db1 = engine_from_config(config, 'sqlalchemy.db1.')
83
84 init_model(sa_engine_db1)
87 init_model(sa_engine_db1)
85
88
86 repos_path = make_ui('db').configitems('paths')[0][1]
89 repos_path = make_ui('db').configitems('paths')[0][1]
87 repo2db_mapper(ScmModel().repo_scan(repos_path))
90 repo2db_mapper(ScmModel().repo_scan(repos_path))
88 set_available_permissions(config)
91 set_available_permissions(config)
89 config['base_path'] = repos_path
92 config['base_path'] = repos_path
90 set_rhodecode_config(config)
93 set_rhodecode_config(config)
91 # CONFIGURATION OPTIONS HERE (note: all config options will override
94 # CONFIGURATION OPTIONS HERE (note: all config options will override
92 # any Pylons config options)
95 # any Pylons config options)
93
96
94 # store config reference into our module to skip import magic of
97 # store config reference into our module to skip import magic of
95 # pylons
98 # pylons
96 rhodecode.CONFIG.update(config)
99 rhodecode.CONFIG.update(config)
97 return config
100 return config
General Comments 0
You need to be logged in to leave comments. Login now