~launchpad-pqm/launchpad/devel

« back to all changes in this revision

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

  • Committer: Launchpad Patch Queue Manager
  • Date: 2011-08-03 11:23:34 UTC
  • mfrom: (13457.6.16 upgrade-stderr)
  • Revision ID: launchpad@pqm.canonical.com-20110803112334-acnupsa7jmzmdeet
[r=stevenk][bug=819751] Fix the implementation of several methods in
 LoggingUIFactory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the
 
1
# Copyright 2009-2010 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.
5
5
 
6
6
The scripts provide an interactive prompt with the Launchpad Storm classes,
7
7
all interface classes and the zope3 CA-fu at your fingertips, connected to
8
 
launchpad_dev or the database specified on the command line.
 
8
launchpad_dev or your LP_DBNAME environment variable (if you have one set).
9
9
One uses Python, the other iPython.
10
10
"""
11
11
 
17
17
 
18
18
#
19
19
import os
20
 
import readline
21
 
import rlcompleter
22
20
import sys
23
21
 
24
22
from pytz import utc
25
 
from storm.expr import *
26
 
# Bring in useful bits of Storm.
27
 
from storm.locals import *
28
23
import transaction
 
24
 
29
25
from zope.component import getUtility
30
 
from zope.interface.verify import verifyObject
31
26
from zope.security.proxy import removeSecurityProxy
32
27
 
 
28
from canonical.launchpad.scripts import execute_zcml_for_scripts
 
29
from canonical.launchpad.webapp import canonical_url
 
30
 
33
31
from lp.answers.model.question import Question
34
32
from lp.blueprints.model.specification import Specification
35
33
from lp.bugs.model.bug import Bug
38
36
from lp.registry.model.person import Person
39
37
from lp.registry.model.product import Product
40
38
from lp.registry.model.projectgroup import ProjectGroup
41
 
from lp.services.scripts import execute_zcml_for_scripts
42
 
from lp.services.webapp import canonical_url
43
 
from lp.services.webapp.interfaces import (
44
 
    DEFAULT_FLAVOR,
45
 
    IStoreSelector,
46
 
    MAIN_STORE,
47
 
    MASTER_FLAVOR,
48
 
    SLAVE_FLAVOR,
49
 
    )
50
39
from lp.testing.factory import LaunchpadObjectFactory
51
40
 
 
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
 
52
62
 
53
63
def _get_locals():
54
64
    if len(sys.argv) > 1:
55
65
        dbuser = sys.argv[1]
56
66
    else:
57
67
        dbuser = None
 
68
    print 'execute_zcml_for_scripts()...'
58
69
    execute_zcml_for_scripts()
59
70
    readline.parse_and_bind('tab: complete')
60
 
    # Mimic the real interactive interpreter's loading of any
61
 
    # $PYTHONSTARTUP file.
 
71
    # Mimic the real interactive interpreter's loading of any $PYTHONSTARTUP file.
 
72
    print 'Reading $PYTHONSTARTUP...'
62
73
    startup = os.environ.get('PYTHONSTARTUP')
63
74
    if startup:
64
75
        execfile(startup)
 
76
    print 'Initializing storm...'
65
77
    store_selector = getUtility(IStoreSelector)
66
78
    store = store_selector.get(MAIN_STORE, MASTER_FLAVOR)
67
79
 
 
80
    # Let's get a few handy objects going.
68
81
    if dbuser == 'launchpad':
69
 
        # Create a few variables "in case they come in handy."
70
 
        # Do we really use these?  Are they worth carrying around?
 
82
        print 'Creating a few handy objects...'
71
83
        d = Distribution.get(1)
72
84
        p = Person.get(1)
73
85
        ds = DistroSeries.get(1)
79
91
        q = Question.get(1)
80
92
 
81
93
    # Having a factory instance is handy.
 
94
    print 'Creating the factory...'
82
95
    factory = LaunchpadObjectFactory()
83
96
    res = {}
84
97
    res.update(locals())