~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/lp/__init__.py

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-09-13 08:44:05 UTC
  • mfrom: (7675.1045.788 db-devel)
  • Revision ID: launchpad@pqm.canonical.com-20110913084405-t6b62nt98rcmp6wg
Merging db-stable at revno 10971

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
__metaclass__ = type
13
13
 
14
 
import os
15
 
 
16
14
from canonical.database.postgresql import ConnectionString
17
15
from canonical.config import dbconfig
18
16
from canonical.database.sqlbase import (
20
18
 
21
19
 
22
20
__all__ = [
23
 
    'get_dbname', 'dbhost', 'dbuser', 'isZopeless', 'initZopeless',
 
21
    'isZopeless', 'initZopeless',
24
22
    ]
25
23
 
26
 
# SQLObject compatibility - dbname, dbhost and dbuser are DEPRECATED.
27
 
#
28
 
# Allow override by environment variables for backwards compatibility.
29
 
# This was needed to allow tests to propagate settings to spawned processes.
30
 
# However, now we just have a single environment variable (LAUNCHPAD_CONF)
31
 
# which specifies which section of the config file to use instead,
32
 
# Note that an empty host is different to 'localhost', as the latter
33
 
# connects via TCP/IP instead of a Unix domain socket. Also note that
34
 
# if the host is empty it can be overridden by the standard PostgreSQL
35
 
# environment variables, this feature currently required by Async's
36
 
# office environment.
37
 
dbhost = os.environ.get('LP_DBHOST', None)
38
 
dbuser = os.environ.get('LP_DBUSER', None)
39
 
dbport = os.environ.get('LP_DBPORT', None)
40
 
dbname_override = os.environ.get('LP_DBNAME', None)
41
 
 
42
 
 
43
 
def get_dbname():
44
 
    """Get the DB Name for scripts: deprecated.
45
 
 
46
 
    :return: The dbname for scripts.
47
 
    """
48
 
    if dbname_override is not None:
49
 
        return dbname_override
50
 
    dbname = ConnectionString(dbconfig.main_master).dbname
51
 
    assert dbname is not None, 'Invalid main_master connection string'
52
 
    return  dbname
53
 
 
54
 
 
55
 
if dbhost is None:
56
 
    dbhost = ConnectionString(dbconfig.main_master).host
57
 
if dbport is None:
58
 
    dbport = ConnectionString(dbconfig.main_master).port
59
 
 
60
 
if dbuser is None:
61
 
    dbuser = dbconfig.dbuser
62
 
 
63
24
 
64
25
def isZopeless():
65
26
    """Returns True if we are running in the Zopeless environment"""
67
28
    return ZopelessTransactionManager._installed is not None
68
29
 
69
30
 
70
 
_IGNORED = object()
71
 
 
72
 
 
73
 
def initZopeless(dbname=None, dbhost=None, dbuser=None,
74
 
                 isolation=ISOLATION_LEVEL_DEFAULT):
 
31
def initZopeless(dbuser=None, isolation=ISOLATION_LEVEL_DEFAULT):
75
32
    """Initialize the Zopeless environment."""
76
33
    if dbuser is None:
77
 
        # Nothing calling initZopeless should be connecting as the
78
 
        # 'launchpad' user, which is the default.
79
 
        # StuartBishop 20050923
80
 
        # warnings.warn(
81
 
        #        "Passing dbuser parameter to initZopeless will soon "
82
 
        #        "be mandatory", DeprecationWarning, stacklevel=2
83
 
        #        )
84
 
        pass  # Disabled. Bug #3050
85
 
    if dbname is None:
86
 
        dbname = get_dbname()
87
 
    if dbhost is None:
88
 
        dbhost = globals()['dbhost']
89
 
    if dbuser is None:
90
 
        dbuser = globals()['dbuser']
 
34
        dbuser = (
 
35
            ConnectionString(dbconfig.main_master).user or dbconfig.dbuser)
91
36
 
92
37
    return ZopelessTransactionManager.initZopeless(
93
 
        dbname=dbname, dbhost=dbhost, dbuser=dbuser, isolation=isolation)
 
38
        dbuser=dbuser, isolation=isolation)