1 #! /usr/bin/env python
2 #
3 # Copyright (c) 2003 Richard Jones (richard@mechanicalcat.net)
4 #
5 # $Id: demo.py,v 1.26 2007-08-28 22:37:45 jpend Exp $
7 import errno
8 import os
9 import socket
10 import sys
11 import urlparse
12 from glob import glob
14 from roundup import configuration
15 from roundup.scripts import roundup_server
17 def install_demo(home, backend, template):
18 """Install a demo tracker
20 Parameters:
21 home:
22 tracker home directory path
23 backend:
24 database backend name
25 template:
26 full path to the tracker template directory
28 """
29 from roundup import init, instance, password, backends
31 # set up the config for this tracker
32 config = configuration.CoreConfig()
33 config['TRACKER_HOME'] = home
34 config['MAIL_DOMAIN'] = 'localhost'
35 config['DATABASE'] = 'db'
36 config['WEB_DEBUG'] = True
37 if backend in ('mysql', 'postgresql'):
38 config['RDBMS_HOST'] = 'localhost'
39 config['RDBMS_USER'] = 'rounduptest'
40 config['RDBMS_PASSWORD'] = 'rounduptest'
41 config['RDBMS_NAME'] = 'rounduptest'
43 # see if we have further db nuking to perform
44 module = backends.get_backend(backend)
45 if module.db_exists(config):
46 module.db_nuke(config)
48 init.install(home, template)
49 # don't have email flying around
50 os.remove(os.path.join(home, 'detectors', 'nosyreaction.py'))
51 try:
52 os.remove(os.path.join(home, 'detectors', 'nosyreaction.pyc'))
53 except os.error, error:
54 if error.errno != errno.ENOENT:
55 raise
56 init.write_select_db(home, backend)
58 # figure basic params for server
59 hostname = 'localhost'
60 # pick a fairly odd, random port
61 port = 8917
62 while 1:
63 print 'Trying to set up web server on port %d ...'%port,
64 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
65 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
66 try:
67 s.connect((hostname, port))
68 except socket.error, e:
69 if not hasattr(e, 'args') or e.args[0] != errno.ECONNREFUSED:
70 raise
71 print 'should be ok.'
72 break
73 else:
74 s.close()
75 print 'already in use.'
76 port += 100
77 config['TRACKER_WEB'] = 'http://%s:%s/demo/'%(hostname, port)
79 # write the config
80 config['INSTANT_REGISTRATION'] = 1
81 config.save(os.path.join(home, config.INI_FILE))
83 # open the tracker and initialise
84 tracker = instance.open(home)
85 tracker.init(password.Password('admin'))
87 # add the "demo" user
88 db = tracker.open('admin')
89 db.user.create(username='demo', password=password.Password('demo'),
90 realname='Demo User', roles='User')
91 db.commit()
92 db.close()
94 def run_demo(home):
95 """Run the demo tracker installed in ``home``"""
96 cfg = configuration.CoreConfig(home)
97 url = cfg["TRACKER_WEB"]
98 hostname, port = urlparse.urlparse(url)[1].split(':')
99 port = int(port)
100 success_message = '''Server running - connect to:
101 %s
102 1. Log in as "demo"/"demo" or "admin"/"admin".
103 2. Hit Control-C to stop the server.
104 3. Re-start the server by running "roundup-demo" again.
105 4. Re-initialise the server by running "roundup-demo nuke".
107 Demo tracker is set up to be accessed by localhost browser. If you
108 run demo on a server host, please stop the demo, open file
109 "demo/config.ini" with your editor, change the host name in the "web"
110 option in section "[tracker]", save the file, then re-run the demo
111 program.
113 ''' % url
115 # disable command line processing in roundup_server
116 sys.argv = sys.argv[:1] + ['-p', str(port), 'demo=' + home]
117 roundup_server.run(success_message=success_message)
119 def demo_main():
120 """Run a demo server for users to play with for instant gratification.
122 Sets up the web service on localhost. Disables nosy lists.
123 """
124 home = os.path.abspath('demo')
125 if not os.path.exists(home) or (sys.argv[-1] == 'nuke'):
126 if len(sys.argv) > 2:
127 backend = sys.argv[-2]
128 else:
129 backend = 'anydbm'
130 install_demo(home, backend, os.path.join('share', 'roundup', 'templates', 'classic'))
131 run_demo(home)
133 if __name__ == '__main__':
134 demo_main()
136 # vim: set filetype=python sts=4 sw=4 et si :