~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/canonical/database/harness.py

  • Committer: Jelmer Vernooij
  • Date: 2011-09-21 14:28:02 UTC
  • mfrom: (14006 devel)
  • mto: This revision was merged to the branch mainline in revision 14010.
  • Revision ID: jelmer@canonical.com-20110921142802-7ggkc204igsy532w
MergeĀ lp:launchpad

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009-2010 Canonical Ltd.  This software is licensed under the
 
1
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
 
4
4
"""Scripts for starting a Python prompt with Launchpad initialized.
17
17
 
18
18
#
19
19
import os
 
20
import readline
 
21
import rlcompleter
20
22
import sys
21
23
 
22
24
from pytz import utc
 
25
from storm.expr import *
 
26
# Bring in useful bits of Storm.
 
27
from storm.locals import *
23
28
import transaction
24
 
 
25
29
from zope.component import getUtility
 
30
from zope.interface.verify import verifyObject
26
31
from zope.security.proxy import removeSecurityProxy
27
32
 
28
33
from canonical.launchpad.scripts import execute_zcml_for_scripts
29
34
from canonical.launchpad.webapp import canonical_url
30
 
 
 
35
from canonical.launchpad.webapp.interfaces import (
 
36
    DEFAULT_FLAVOR,
 
37
    IStoreSelector,
 
38
    MAIN_STORE,
 
39
    MASTER_FLAVOR,
 
40
    SLAVE_FLAVOR,
 
41
    )
31
42
from lp.answers.model.question import Question
32
43
from lp.blueprints.model.specification import Specification
33
44
from lp.bugs.model.bug import Bug
38
49
from lp.registry.model.projectgroup import ProjectGroup
39
50
from lp.testing.factory import LaunchpadObjectFactory
40
51
 
41
 
from zope.interface.verify import verifyObject
42
 
 
43
 
import readline
44
 
import rlcompleter
45
 
 
46
 
# Bring in useful bits of Storm.
47
 
from storm.locals import *
48
 
from storm.expr import *
49
 
from canonical.launchpad.webapp.interfaces import (
50
 
    IStoreSelector, MAIN_STORE, MASTER_FLAVOR, SLAVE_FLAVOR, DEFAULT_FLAVOR)
51
 
 
52
 
 
53
 
def switch_db_user(dbuser, commit_first=True):
54
 
    global transactionmgr
55
 
    if commit_first:
56
 
        transactionmgr.commit()
57
 
    else:
58
 
        transactionmgr.abort()
59
 
    transactionmgr.uninstall()
60
 
    transactionmgr = initZopeless(dbuser=dbuser)
61
 
 
62
52
 
63
53
def _get_locals():
64
54
    if len(sys.argv) > 1:
65
55
        dbuser = sys.argv[1]
66
56
    else:
67
57
        dbuser = None
68
 
    print 'execute_zcml_for_scripts()...'
69
58
    execute_zcml_for_scripts()
70
59
    readline.parse_and_bind('tab: complete')
71
 
    # Mimic the real interactive interpreter's loading of any $PYTHONSTARTUP file.
72
 
    print 'Reading $PYTHONSTARTUP...'
 
60
    # Mimic the real interactive interpreter's loading of any
 
61
    # $PYTHONSTARTUP file.
73
62
    startup = os.environ.get('PYTHONSTARTUP')
74
63
    if startup:
75
64
        execfile(startup)
76
 
    print 'Initializing storm...'
77
65
    store_selector = getUtility(IStoreSelector)
78
66
    store = store_selector.get(MAIN_STORE, MASTER_FLAVOR)
79
67
 
80
 
    # Let's get a few handy objects going.
81
68
    if dbuser == 'launchpad':
82
 
        print 'Creating a few handy objects...'
 
69
        # Create a few variables "in case they come in handy."
 
70
        # Do we really use these?  Are they worth carrying around?
83
71
        d = Distribution.get(1)
84
72
        p = Person.get(1)
85
73
        ds = DistroSeries.get(1)
91
79
        q = Question.get(1)
92
80
 
93
81
    # Having a factory instance is handy.
94
 
    print 'Creating the factory...'
95
82
    factory = LaunchpadObjectFactory()
96
83
    res = {}
97
84
    res.update(locals())