Commit | Line | Data |
---|---|---|
1b579e18 E |
1 | # GNU MediaGoblin -- federated, autonomous media hosting |
2 | # Copyright (C) 2011 Free Software Foundation, Inc | |
3 | # | |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
42ef819c E |
16 | |
17 | import jinja2 | |
c85c9dc7 | 18 | from mediagoblin import staticdirect |
fe289be4 E |
19 | from mediagoblin.init.config import ( |
20 | read_mediagoblin_config, generate_validation_report) | |
7664b4db | 21 | from mediagoblin import mg_globals |
cca5d55d | 22 | from mediagoblin.mg_globals import setup_globals |
3f4b5e4a E |
23 | from mediagoblin.db.open import setup_connection_and_db_from_config |
24 | from mediagoblin.db.util import MigrationManager | |
7664b4db | 25 | from mediagoblin.workbench import WorkbenchManager |
dccef262 | 26 | from mediagoblin.storage import storage_system_from_config |
c85c9dc7 E |
27 | |
28 | ||
29 | class Error(Exception): pass | |
30 | class ImproperlyConfigured(Error): pass | |
42ef819c E |
31 | |
32 | ||
fe289be4 E |
33 | def setup_global_and_app_config(config_path): |
34 | global_config, validation_result = read_mediagoblin_config(config_path) | |
35 | app_config = global_config['mediagoblin'] | |
36 | # report errors if necessary | |
37 | validation_report = generate_validation_report( | |
38 | global_config, validation_result) | |
39 | if validation_report: | |
40 | raise ImproperlyConfigured(validation_report) | |
41 | ||
cca5d55d E |
42 | setup_globals( |
43 | app_config=app_config, | |
44 | global_config=global_config) | |
45 | ||
fe289be4 E |
46 | return global_config, app_config |
47 | ||
3f4b5e4a E |
48 | |
49 | def setup_database(): | |
50 | app_config = mg_globals.app_config | |
51 | ||
52 | # This MUST be imported so as to set up the appropriate migrations! | |
53 | from mediagoblin.db import migrations | |
54 | ||
55 | # Set up the database | |
56 | connection, db = setup_connection_and_db_from_config(app_config) | |
57 | ||
58 | # Init the migration number if necessary | |
59 | migration_manager = MigrationManager(db) | |
60 | migration_manager.install_migration_version_if_missing() | |
61 | ||
62 | # Tiny hack to warn user if our migration is out of date | |
63 | if not migration_manager.database_at_latest_migration(): | |
64 | print ( | |
65 | "*WARNING:* Your migrations are out of date, " | |
66 | "maybe run ./bin/gmg migrate?") | |
67 | ||
68 | setup_globals( | |
69 | db_connection = connection, | |
70 | database = db) | |
71 | ||
72 | return connection, db | |
73 | ||
74 | ||
42ef819c E |
75 | def get_jinja_loader(user_template_path=None): |
76 | """ | |
77 | Set up the Jinja template loaders, possibly allowing for user | |
78 | overridden templates. | |
79 | ||
80 | (In the future we may have another system for providing theming; | |
81 | for now this is good enough.) | |
82 | """ | |
83 | if user_template_path: | |
84 | return jinja2.ChoiceLoader( | |
85 | [jinja2.FileSystemLoader(user_template_path), | |
86 | jinja2.PackageLoader('mediagoblin', 'templates')]) | |
87 | else: | |
88 | return jinja2.PackageLoader('mediagoblin', 'templates') | |
c85c9dc7 E |
89 | |
90 | ||
91 | def get_staticdirector(app_config): | |
92 | if app_config.has_key('direct_remote_path'): | |
93 | return staticdirect.RemoteStaticDirect( | |
94 | app_config['direct_remote_path'].strip()) | |
95 | elif app_config.has_key('direct_remote_paths'): | |
96 | direct_remote_path_lines = app_config[ | |
97 | 'direct_remote_paths'].strip().splitlines() | |
98 | return staticdirect.MultiRemoteStaticDirect( | |
99 | dict([line.strip().split(' ', 1) | |
100 | for line in direct_remote_path_lines])) | |
101 | else: | |
102 | raise ImproperlyConfigured( | |
103 | "One of direct_remote_path or " | |
104 | "direct_remote_paths must be provided") | |
7664b4db E |
105 | |
106 | ||
dccef262 E |
107 | def setup_storage(): |
108 | app_config = mg_globals.app_config | |
109 | ||
110 | public_store = storage_system_from_config(app_config, 'publicstore') | |
111 | queue_store = storage_system_from_config(app_config, 'queuestore') | |
112 | ||
113 | setup_globals( | |
114 | public_store = public_store, | |
115 | queue_store = queue_store) | |
116 | ||
117 | return public_store, queue_store | |
118 | ||
119 | ||
7664b4db E |
120 | def setup_workbench(): |
121 | app_config = mg_globals.app_config | |
122 | ||
123 | workbench_manager = WorkbenchManager(app_config['workbench_path']) | |
124 | ||
125 | setup_globals(workbench_manager = workbench_manager) |