| 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/>. |
| 16 | |
| 17 | import jinja2 |
| 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 |
| 27 | |
| 28 | |
| 29 | class Error(Exception): pass |
| 30 | class ImproperlyConfigured(Error): pass |
| 31 | |
| 32 | |
| 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 | |
| 42 | setup_globals( |
| 43 | app_config=app_config, |
| 44 | global_config=global_config) |
| 45 | |
| 46 | return global_config, app_config |
| 47 | |
| 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 | 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?!") |
| 74 | |
| 75 | setup_globals( |
| 76 | db_connection = connection, |
| 77 | database = db) |
| 78 | |
| 79 | return connection, db |
| 80 | |
| 81 | |
| 82 | def 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') |
| 96 | |
| 97 | |
| 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])) |
| 108 | else: |
| 109 | raise ImproperlyConfigured( |
| 110 | "One of direct_remote_path or " |
| 111 | "direct_remote_paths must be provided") |
| 112 | |
| 113 | |
| 114 | def setup_storage(): |
| 115 | global_config = mg_globals.global_config |
| 116 | |
| 117 | key_short = 'publicstore' |
| 118 | key_long = "storage:" + key_short |
| 119 | public_store = storage_system_from_config(global_config[key_long]) |
| 120 | |
| 121 | key_short = 'queuestore' |
| 122 | key_long = "storage:" + key_short |
| 123 | queue_store = storage_system_from_config(global_config[key_long]) |
| 124 | |
| 125 | setup_globals( |
| 126 | public_store = public_store, |
| 127 | queue_store = queue_store) |
| 128 | |
| 129 | return public_store, queue_store |
| 130 | |
| 131 | |
| 132 | def 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) |