Show More
@@ -1,165 +1,169 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | # This program is free software: you can redistribute it and/or modify |
|
2 | # This program is free software: you can redistribute it and/or modify | |
3 | # it under the terms of the GNU General Public License as published by |
|
3 | # it under the terms of the GNU General Public License as published by | |
4 | # the Free Software Foundation, either version 3 of the License, or |
|
4 | # the Free Software Foundation, either version 3 of the License, or | |
5 | # (at your option) any later version. |
|
5 | # (at your option) any later version. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU General Public License |
|
12 | # You should have received a copy of the GNU General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | """ |
|
14 | """ | |
15 | Global configuration file for TurboGears2 specific settings in Kallithea. |
|
15 | Global configuration file for TurboGears2 specific settings in Kallithea. | |
16 |
|
16 | |||
17 | This file complements the .ini file. |
|
17 | This file complements the .ini file. | |
18 | """ |
|
18 | """ | |
19 |
|
19 | |||
20 | import logging |
|
20 | import logging | |
21 | import os |
|
21 | import os | |
22 | import platform |
|
22 | import platform | |
23 | import sys |
|
23 | import sys | |
24 |
|
24 | |||
25 | import alembic.config |
|
25 | import alembic.config | |
26 | import mercurial |
|
26 | import mercurial | |
27 | import tg |
|
27 | import tg | |
28 | from alembic.migration import MigrationContext |
|
28 | from alembic.migration import MigrationContext | |
29 | from alembic.script.base import ScriptDirectory |
|
29 | from alembic.script.base import ScriptDirectory | |
30 | from sqlalchemy import create_engine |
|
30 | from sqlalchemy import create_engine | |
31 | from tg import FullStackApplicationConfigurator |
|
31 | from tg import FullStackApplicationConfigurator | |
32 |
|
32 | |||
33 | import kallithea.lib.locales |
|
33 | import kallithea.lib.locales | |
34 | import kallithea.model.base |
|
34 | import kallithea.model.base | |
35 | import kallithea.model.meta |
|
35 | import kallithea.model.meta | |
36 | from kallithea.lib import celery_app |
|
36 | from kallithea.lib import celery_app | |
37 | from kallithea.lib.utils import load_extensions, set_app_settings, set_indexer_config, set_vcs_config |
|
37 | from kallithea.lib.utils import load_extensions, set_app_settings, set_indexer_config, set_vcs_config | |
38 | from kallithea.lib.utils2 import asbool, check_git_version |
|
38 | from kallithea.lib.utils2 import asbool, check_git_version | |
39 | from kallithea.model import db |
|
39 | from kallithea.model import db | |
40 |
|
40 | |||
41 |
|
41 | |||
42 | log = logging.getLogger(__name__) |
|
42 | log = logging.getLogger(__name__) | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | base_config = FullStackApplicationConfigurator() |
|
45 | base_config = FullStackApplicationConfigurator() | |
46 |
|
46 | |||
47 | base_config.update_blueprint({ |
|
47 | base_config.update_blueprint({ | |
48 | 'package': kallithea, |
|
48 | 'package': kallithea, | |
49 |
|
49 | |||
50 | # Rendering Engines Configuration |
|
50 | # Rendering Engines Configuration | |
51 | 'renderers': [ |
|
51 | 'renderers': [ | |
52 | 'json', |
|
52 | 'json', | |
53 | 'mako', |
|
53 | 'mako', | |
54 | ], |
|
54 | ], | |
55 | 'default_renderer': 'mako', |
|
55 | 'default_renderer': 'mako', | |
56 | 'use_dotted_templatenames': False, |
|
56 | 'use_dotted_templatenames': False, | |
57 |
|
57 | |||
58 | # Configure Sessions, store data as JSON to avoid pickle security issues |
|
58 | # Configure Sessions, store data as JSON to avoid pickle security issues | |
59 | 'session.enabled': True, |
|
59 | 'session.enabled': True, | |
60 | 'session.data_serializer': 'json', |
|
60 | 'session.data_serializer': 'json', | |
61 |
|
61 | |||
62 | # Configure the base SQLALchemy Setup |
|
62 | # Configure the base SQLALchemy Setup | |
63 | 'use_sqlalchemy': True, |
|
63 | 'use_sqlalchemy': True, | |
64 | 'model': kallithea.model.base, |
|
64 | 'model': kallithea.model.base, | |
65 | 'DBSession': kallithea.model.meta.Session, |
|
65 | 'DBSession': kallithea.model.meta.Session, | |
66 |
|
66 | |||
67 | # Configure App without an authentication backend. |
|
67 | # Configure App without an authentication backend. | |
68 | 'auth_backend': None, |
|
68 | 'auth_backend': None, | |
69 |
|
69 | |||
70 | # Use custom error page for these errors. By default, Turbogears2 does not add |
|
70 | # Use custom error page for these errors. By default, Turbogears2 does not add | |
71 | # 400 in this list. |
|
71 | # 400 in this list. | |
72 | # Explicitly listing all is considered more robust than appending to defaults, |
|
72 | # Explicitly listing all is considered more robust than appending to defaults, | |
73 | # in light of possible future framework changes. |
|
73 | # in light of possible future framework changes. | |
74 | 'errorpage.status_codes': [400, 401, 403, 404], |
|
74 | 'errorpage.status_codes': [400, 401, 403, 404], | |
75 |
|
75 | |||
76 | # Disable transaction manager -- currently Kallithea takes care of transactions itself |
|
76 | # Disable transaction manager -- currently Kallithea takes care of transactions itself | |
77 | 'tm.enabled': False, |
|
77 | 'tm.enabled': False, | |
78 |
|
78 | |||
79 | # Set the default i18n source language so TG doesn't search beyond 'en' in Accept-Language. |
|
79 | # Set the default i18n source language so TG doesn't search beyond 'en' in Accept-Language. | |
80 | 'i18n.lang': 'en', |
|
80 | 'i18n.lang': 'en', | |
|
81 | ||||
|
82 | # For TurboGears 2.4.3, define the native language for translations to | |||
|
83 | # prevent fall-through to languages requested with lower priority. | |||
|
84 | 'i18n.native': ['en', 'en_US', 'en_GB'], | |||
81 | }) |
|
85 | }) | |
82 |
|
86 | |||
83 | # DebugBar, a debug toolbar for TurboGears2. |
|
87 | # DebugBar, a debug toolbar for TurboGears2. | |
84 | # (https://github.com/TurboGears/tgext.debugbar) |
|
88 | # (https://github.com/TurboGears/tgext.debugbar) | |
85 | # To enable it, install 'tgext.debugbar' and 'kajiki', and run Kallithea with |
|
89 | # To enable it, install 'tgext.debugbar' and 'kajiki', and run Kallithea with | |
86 | # 'debug = true' (not in production!) |
|
90 | # 'debug = true' (not in production!) | |
87 | # See the Kallithea documentation for more information. |
|
91 | # See the Kallithea documentation for more information. | |
88 | try: |
|
92 | try: | |
89 | import kajiki # only to check its existence |
|
93 | import kajiki # only to check its existence | |
90 | from tgext.debugbar import enable_debugbar |
|
94 | from tgext.debugbar import enable_debugbar | |
91 | assert kajiki |
|
95 | assert kajiki | |
92 | except ImportError: |
|
96 | except ImportError: | |
93 | pass |
|
97 | pass | |
94 | else: |
|
98 | else: | |
95 | base_config.get_blueprint_value('renderers').append('kajiki') |
|
99 | base_config.get_blueprint_value('renderers').append('kajiki') | |
96 | enable_debugbar(base_config) |
|
100 | enable_debugbar(base_config) | |
97 |
|
101 | |||
98 |
|
102 | |||
99 | def setup_configuration(app): |
|
103 | def setup_configuration(app): | |
100 | config = app.config |
|
104 | config = app.config | |
101 |
|
105 | |||
102 | if not kallithea.lib.locales.current_locale_is_valid(): |
|
106 | if not kallithea.lib.locales.current_locale_is_valid(): | |
103 | log.error("Terminating ...") |
|
107 | log.error("Terminating ...") | |
104 | sys.exit(1) |
|
108 | sys.exit(1) | |
105 |
|
109 | |||
106 | # Mercurial sets encoding at module import time, so we have to monkey patch it |
|
110 | # Mercurial sets encoding at module import time, so we have to monkey patch it | |
107 | hgencoding = config.get('hgencoding') |
|
111 | hgencoding = config.get('hgencoding') | |
108 | if hgencoding: |
|
112 | if hgencoding: | |
109 | mercurial.encoding.encoding = hgencoding |
|
113 | mercurial.encoding.encoding = hgencoding | |
110 |
|
114 | |||
111 | if config.get('ignore_alembic_revision', False): |
|
115 | if config.get('ignore_alembic_revision', False): | |
112 | log.warning('database alembic revision checking is disabled') |
|
116 | log.warning('database alembic revision checking is disabled') | |
113 | else: |
|
117 | else: | |
114 | dbconf = config['sqlalchemy.url'] |
|
118 | dbconf = config['sqlalchemy.url'] | |
115 | alembic_cfg = alembic.config.Config() |
|
119 | alembic_cfg = alembic.config.Config() | |
116 | alembic_cfg.set_main_option('script_location', 'kallithea:alembic') |
|
120 | alembic_cfg.set_main_option('script_location', 'kallithea:alembic') | |
117 | alembic_cfg.set_main_option('sqlalchemy.url', dbconf) |
|
121 | alembic_cfg.set_main_option('sqlalchemy.url', dbconf) | |
118 | script_dir = ScriptDirectory.from_config(alembic_cfg) |
|
122 | script_dir = ScriptDirectory.from_config(alembic_cfg) | |
119 | available_heads = sorted(script_dir.get_heads()) |
|
123 | available_heads = sorted(script_dir.get_heads()) | |
120 |
|
124 | |||
121 | engine = create_engine(dbconf) |
|
125 | engine = create_engine(dbconf) | |
122 | with engine.connect() as conn: |
|
126 | with engine.connect() as conn: | |
123 | context = MigrationContext.configure(conn) |
|
127 | context = MigrationContext.configure(conn) | |
124 | current_heads = sorted(str(s) for s in context.get_current_heads()) |
|
128 | current_heads = sorted(str(s) for s in context.get_current_heads()) | |
125 | if current_heads != available_heads: |
|
129 | if current_heads != available_heads: | |
126 | log.error('Failed to run Kallithea:\n\n' |
|
130 | log.error('Failed to run Kallithea:\n\n' | |
127 | 'The database version does not match the Kallithea version.\n' |
|
131 | 'The database version does not match the Kallithea version.\n' | |
128 | 'Please read the documentation on how to upgrade or downgrade the database.\n' |
|
132 | 'Please read the documentation on how to upgrade or downgrade the database.\n' | |
129 | 'Current database version id(s): %s\n' |
|
133 | 'Current database version id(s): %s\n' | |
130 | 'Expected database version id(s): %s\n' |
|
134 | 'Expected database version id(s): %s\n' | |
131 | 'If you are a developer and you know what you are doing, you can add `ignore_alembic_revision = True` ' |
|
135 | 'If you are a developer and you know what you are doing, you can add `ignore_alembic_revision = True` ' | |
132 | 'to your .ini file to skip the check.\n' % (' '.join(current_heads), ' '.join(available_heads))) |
|
136 | 'to your .ini file to skip the check.\n' % (' '.join(current_heads), ' '.join(available_heads))) | |
133 | sys.exit(1) |
|
137 | sys.exit(1) | |
134 |
|
138 | |||
135 | # store some globals into kallithea |
|
139 | # store some globals into kallithea | |
136 | kallithea.DEFAULT_USER_ID = db.User.get_default_user().user_id |
|
140 | kallithea.DEFAULT_USER_ID = db.User.get_default_user().user_id | |
137 |
|
141 | |||
138 | if asbool(config.get('use_celery')) and not kallithea.CELERY_APP.finalized: |
|
142 | if asbool(config.get('use_celery')) and not kallithea.CELERY_APP.finalized: | |
139 | kallithea.CELERY_APP.config_from_object(celery_app.make_celery_config(config)) |
|
143 | kallithea.CELERY_APP.config_from_object(celery_app.make_celery_config(config)) | |
140 | kallithea.CONFIG = config |
|
144 | kallithea.CONFIG = config | |
141 |
|
145 | |||
142 | load_extensions(root_path=config['here']) |
|
146 | load_extensions(root_path=config['here']) | |
143 |
|
147 | |||
144 | set_app_settings(config) |
|
148 | set_app_settings(config) | |
145 |
|
149 | |||
146 | instance_id = kallithea.CONFIG.get('instance_id', '*') |
|
150 | instance_id = kallithea.CONFIG.get('instance_id', '*') | |
147 | if instance_id == '*': |
|
151 | if instance_id == '*': | |
148 | instance_id = '%s-%s' % (platform.uname()[1], os.getpid()) |
|
152 | instance_id = '%s-%s' % (platform.uname()[1], os.getpid()) | |
149 | kallithea.CONFIG['instance_id'] = instance_id |
|
153 | kallithea.CONFIG['instance_id'] = instance_id | |
150 |
|
154 | |||
151 | # update kallithea.CONFIG with the meanwhile changed 'config' |
|
155 | # update kallithea.CONFIG with the meanwhile changed 'config' | |
152 | kallithea.CONFIG.update(config) |
|
156 | kallithea.CONFIG.update(config) | |
153 |
|
157 | |||
154 | # configure vcs and indexer libraries (they are supposed to be independent |
|
158 | # configure vcs and indexer libraries (they are supposed to be independent | |
155 | # as much as possible and thus avoid importing tg.config or |
|
159 | # as much as possible and thus avoid importing tg.config or | |
156 | # kallithea.CONFIG). |
|
160 | # kallithea.CONFIG). | |
157 | set_vcs_config(kallithea.CONFIG) |
|
161 | set_vcs_config(kallithea.CONFIG) | |
158 | set_indexer_config(kallithea.CONFIG) |
|
162 | set_indexer_config(kallithea.CONFIG) | |
159 |
|
163 | |||
160 | check_git_version() |
|
164 | check_git_version() | |
161 |
|
165 | |||
162 | kallithea.model.meta.Session.remove() |
|
166 | kallithea.model.meta.Session.remove() | |
163 |
|
167 | |||
164 |
|
168 | |||
165 | tg.hooks.register('configure_new_app', setup_configuration) |
|
169 | tg.hooks.register('configure_new_app', setup_configuration) |
General Comments 0
You need to be logged in to leave comments.
Login now