508. Updates copyright/license information
[mediagoblin.git] / mediagoblin / init / __init__.py
CommitLineData
1b579e18 1# GNU MediaGoblin -- federated, autonomous media hosting
12a100e4 2# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
1b579e18
E
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
17import jinja2
c85c9dc7 18from mediagoblin import staticdirect
fe289be4
E
19from mediagoblin.init.config import (
20 read_mediagoblin_config, generate_validation_report)
7664b4db 21from mediagoblin import mg_globals
cca5d55d 22from mediagoblin.mg_globals import setup_globals
3f4b5e4a
E
23from mediagoblin.db.open import setup_connection_and_db_from_config
24from mediagoblin.db.util import MigrationManager
7664b4db 25from mediagoblin.workbench import WorkbenchManager
dccef262 26from mediagoblin.storage import storage_system_from_config
c85c9dc7
E
27
28
29class Error(Exception): pass
30class ImproperlyConfigured(Error): pass
42ef819c
E
31
32
fe289be4
E
33def 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
49def 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():
482d53cd
CAW
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:
67 print (
68 "*WARNING:* Your migrations are out of date, "
69 "maybe run ./bin/gmg migrate?")
70 elif db_migration_num > latest_migration_num:
71 print (
72 "*WARNING:* Your migrations are out of date... "
73 "in fact they appear to be from the future?!")
3f4b5e4a
E
74
75 setup_globals(
76 db_connection = connection,
77 database = db)
78
79 return connection, db
80
81
42ef819c
E
82def get_jinja_loader(user_template_path=None):
83 """
84 Set up the Jinja template loaders, possibly allowing for user
85 overridden templates.
86
87 (In the future we may have another system for providing theming;
88 for now this is good enough.)
89 """
90 if user_template_path:
91 return jinja2.ChoiceLoader(
92 [jinja2.FileSystemLoader(user_template_path),
93 jinja2.PackageLoader('mediagoblin', 'templates')])
94 else:
95 return jinja2.PackageLoader('mediagoblin', 'templates')
c85c9dc7
E
96
97
98def 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]))
108 else:
109 raise ImproperlyConfigured(
110 "One of direct_remote_path or "
111 "direct_remote_paths must be provided")
7664b4db
E
112
113
dccef262 114def setup_storage():
ed797069 115 global_config = mg_globals.global_config
dccef262 116
ed797069
E
117 key_short = 'publicstore'
118 key_long = "storage:" + key_short
56fc7186 119 public_store = storage_system_from_config(global_config[key_long])
ed797069
E
120
121 key_short = 'queuestore'
122 key_long = "storage:" + key_short
56fc7186 123 queue_store = storage_system_from_config(global_config[key_long])
dccef262
E
124
125 setup_globals(
126 public_store = public_store,
127 queue_store = queue_store)
128
129 return public_store, queue_store
130
131
7664b4db
E
132def setup_workbench():
133 app_config = mg_globals.app_config
134
135 workbench_manager = WorkbenchManager(app_config['workbench_path'])
136
137 setup_globals(workbench_manager = workbench_manager)