1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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.
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.
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/>.
18 from mediagoblin
import staticdirect
19 from mediagoblin
.init
.config
import (
20 read_mediagoblin_config
, generate_validation_report
)
21 from mediagoblin
import mg_globals
22 from mediagoblin
.mg_globals
import setup_globals
23 from mediagoblin
.db
.open import setup_connection_and_db_from_config
24 from mediagoblin
.db
.util
import MigrationManager
25 from mediagoblin
.workbench
import WorkbenchManager
26 from mediagoblin
.storage
import storage_system_from_config
29 class Error(Exception): pass
30 class ImproperlyConfigured(Error
): pass
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
)
40 raise ImproperlyConfigured(validation_report
)
43 app_config
=app_config
,
44 global_config
=global_config
)
46 return global_config
, app_config
50 app_config
= mg_globals
.app_config
52 # This MUST be imported so as to set up the appropriate migrations!
53 from mediagoblin
.db
import migrations
56 connection
, db
= setup_connection_and_db_from_config(app_config
)
58 # Init the migration number if necessary
59 migration_manager
= MigrationManager(db
)
60 migration_manager
.install_migration_version_if_missing()
62 # Tiny hack to warn user if our migration is out of date
63 if not migration_manager
.database_at_latest_migration():
64 db_migration_num
= migration_manager
.database_current_migration()
65 latest_migration_num
= migration_manager
.latest_migration()
66 if db_migration_num
< latest_migration_num
:
68 "*WARNING:* Your migrations are out of date, "
69 "maybe run ./bin/gmg migrate?")
70 elif db_migration_num
> latest_migration_num
:
72 "*WARNING:* Your migrations are out of date... "
73 "in fact they appear to be from the future?!")
76 db_connection
= connection
,
82 def get_jinja_loader(user_template_path
=None):
84 Set up the Jinja template loaders, possibly allowing for user
87 (In the future we may have another system for providing theming;
88 for now this is good enough.)
90 if user_template_path
:
91 return jinja2
.ChoiceLoader(
92 [jinja2
.FileSystemLoader(user_template_path
),
93 jinja2
.PackageLoader('mediagoblin', 'templates')])
95 return jinja2
.PackageLoader('mediagoblin', 'templates')
98 def get_staticdirector(app_config
):
99 if app_config
.has_key('direct_remote_path'):
100 return staticdirect
.RemoteStaticDirect(
101 app_config
['direct_remote_path'].strip())
102 elif app_config
.has_key('direct_remote_paths'):
103 direct_remote_path_lines
= app_config
[
104 'direct_remote_paths'].strip().splitlines()
105 return staticdirect
.MultiRemoteStaticDirect(
106 dict([line
.strip().split(' ', 1)
107 for line
in direct_remote_path_lines
]))
109 raise ImproperlyConfigured(
110 "One of direct_remote_path or "
111 "direct_remote_paths must be provided")
115 app_config
= mg_globals
.app_config
117 public_store
= storage_system_from_config(app_config
, 'publicstore')
118 queue_store
= storage_system_from_config(app_config
, 'queuestore')
121 public_store
= public_store
,
122 queue_store
= queue_store
)
124 return public_store
, queue_store
127 def setup_workbench():
128 app_config
= mg_globals
.app_config
130 workbench_manager
= WorkbenchManager(app_config
['workbench_path'])
132 setup_globals(workbench_manager
= workbench_manager
)