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).
4
4
"""Scripts for starting a Python prompt with Launchpad initialized.
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.
24
22
from pytz import utc
25
from storm.expr import *
26
# Bring in useful bits of Storm.
27
from storm.locals import *
29
25
from zope.component import getUtility
30
from zope.interface.verify import verifyObject
31
26
from zope.security.proxy import removeSecurityProxy
28
from canonical.launchpad.scripts import execute_zcml_for_scripts
29
from canonical.launchpad.webapp import canonical_url
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 (
50
39
from lp.testing.factory import LaunchpadObjectFactory
41
from zope.interface.verify import verifyObject
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)
53
def switch_db_user(dbuser, commit_first=True):
56
transactionmgr.commit()
58
transactionmgr.abort()
59
transactionmgr.uninstall()
60
transactionmgr = initZopeless(dbuser=dbuser)
54
64
if len(sys.argv) > 1:
55
65
dbuser = sys.argv[1]
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')
76
print 'Initializing storm...'
65
77
store_selector = getUtility(IStoreSelector)
66
78
store = store_selector.get(MAIN_STORE, MASTER_FLAVOR)
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)
73
85
ds = DistroSeries.get(1)