~benoit.pierre/sloecode/incubation

« back to all changes in this revision

Viewing changes to sloecode/config/environment.py

  • Committer: Thomi Richards
  • Date: 2011-06-04 08:02:32 UTC
  • mfrom: (102.1.15 database-migration)
  • Revision ID: thomir@gmail.com-20110604080232-zorlwo9u65fl9nhr
Merged database migration work.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
"""Pylons environment configuration"""
2
2
import os
3
3
 
 
4
from ConfigParser import RawConfigParser
4
5
from jinja2 import ChoiceLoader, Environment, FileSystemLoader
 
6
import os.path
5
7
import pylons
6
8
from pylons.configuration import PylonsConfig
7
 
from sqlalchemy import engine_from_config
 
9
from sqlalchemy import create_engine
8
10
 
9
11
from sloecode.bzr.factory import BazaarRepositoryFactory
10
12
import sloecode.lib.app_globals as app_globals
45
47
    config['pylons.strict_tmpl_context'] = True
46
48
 
47
49
    # Setup the SQLAlchemy database engine
48
 
    engine = engine_from_config(config, 'sqlalchemy.')
 
50
    # do this by creating the engine url from components:
 
51
    db_config = RawConfigParser()
 
52
    db_config.read(config['db_config_file'])
 
53
    
 
54
    dialect = db_config.get('database', 'dbtype')
 
55
    # translate from dbconfig-common terminology
 
56
    if dialect == 'sqlite3':
 
57
        dialect = 'sqlite'
 
58
    username = db_config.get('database', 'dbuser')
 
59
    password = db_config.get('database', 'dbpass')
 
60
    schema_name = db_config.get('database', 'dbname')
 
61
    db_server = db_config.get('database', 'dbserver')
 
62
    db_port = db_config.get('database', 'dbport')
 
63
    db_path = db_config.get('database', 'dbpath')
 
64
    
 
65
    # we support sqlite:// and mysql://
 
66
    engine_url = dialect + '://'
 
67
    # sqlite needs 4 '/''s, and needs special path options:
 
68
    if dialect == 'sqlite':
 
69
        engine_url += '/' + os.path.join(db_path, schema_name)
 
70
    else:
 
71
        engine_url += "%s:%s@%s/%s" % (username, password, db_server, schema_name)
 
72
    
 
73
    engine = create_engine(engine_url)
49
74
    init_model(engine)
50
75
 
51
 
    # CONFIGURATION OPTIONS HERE (note: all config options will override
52
 
    # any Pylons config options)
 
76
    # create the Bazaar factory:
53
77
    config['bzr.factory'] = BazaarRepositoryFactory(
54
78
        global_conf['bzr_person_root'],
55
79
        global_conf['bzr_project_root'],
56
80
        global_conf['bzr_deleted_person_root'],
57
81
        global_conf['bzr_deleted_project_root'],
58
82
        )
 
83
        
 
84
    # This is a bit of a nasty hack: In "developer mode", we explicitly call:
 
85
    #
 
86
    # `paster setup-app /path/to/config.ini`
 
87
    #
 
88
    # Which creates all the database tables, and also created the admin user and
 
89
    # creates the repository for the admin user - this works fine.
 
90
    #
 
91
    # However, in release mode, the database schema is created by dbconfig-common
 
92
    # and upgraded from one version to the next by the same. We never get a chance
 
93
    # to create the Bazaar repository for the admin user, which results in crashes.
 
94
    #
 
95
    # The following code checks to see if the repository has been created for the
 
96
    # admin user, and creates it if it hasn't:
 
97
    from bzrlib.errors import NotBranchError
 
98
 
 
99
    try:
 
100
        repo = config['bzr.factory'].get_repository_for_person('admin')
 
101
    except NotBranchError:
 
102
        config['bzr.factory'].create_shared_repository_for_person('admin')
 
103
 
59
104
 
60
105
    return config