1 #! /usr/bin/env python
2 #
3 # Copyright (c) 2003 Richard Jones (richard@mechanicalcat.net)
4 #
5 # $Id: demo.py,v 1.10 2004-03-31 23:07:51 richard Exp $
7 import sys, os, string, re, urlparse
8 import shutil, socket, errno, BaseHTTPServer
9 from glob import glob
11 def install_demo(home, backend):
12 # create the instance
13 if os.path.exists(home):
14 shutil.rmtree(home)
15 from roundup import init, instance, password, backends
17 # see if we have further db nuking to perform
18 module = getattr(backends, backend)
19 if backend == 'mysql':
20 class config:
21 MYSQL_DBHOST = 'localhost'
22 MYSQL_DBUSER = 'rounduptest'
23 MYSQL_DBPASSWORD = 'rounduptest'
24 MYSQL_DBNAME = 'rounduptest'
25 DATABASE = 'home'
26 module.db_nuke(config)
27 elif backend == 'postgresql':
28 class config:
29 POSTGRESQL_DATABASE = {'database': 'rounduptest'}
30 DATABASE = 'home'
31 module.db_nuke(config, 1)
33 init.install(home, os.path.join('templates', 'classic'))
34 # don't have email flying around
35 os.remove(os.path.join(home, 'detectors', 'nosyreaction.py'))
36 try:
37 os.remove(os.path.join(home, 'detectors', 'nosyreaction.pyc'))
38 except os.error, error:
39 if error.errno != errno.ENOENT:
40 raise
41 init.write_select_db(home, backend)
43 # figure basic params for server
44 hostname = socket.gethostname()
45 # pick a fairly odd, random port
46 port = 8917
47 while 1:
48 print 'Trying to set up web server on port %d ...'%port,
49 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
50 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
51 try:
52 s.connect((hostname, port))
53 except socket.error, e:
54 if not hasattr(e, 'args') or e.args[0] != errno.ECONNREFUSED:
55 raise
56 print 'should be ok.'
57 break
58 else:
59 s.close()
60 print 'already in use.'
61 port += 100
62 url = 'http://%s:%s/demo/'%(hostname, port)
64 # write the config
65 f = open(os.path.join(home, 'config.py'), 'r')
66 s = f.read().replace('http://tracker.example/cgi-bin/roundup.cgi/bugs/',
67 url)
68 f.close()
69 # DB connection stuff for mysql and postgresql
70 s = s + """
71 MYSQL_DBHOST = 'localhost'
72 MYSQL_DBUSER = 'rounduptest'
73 MYSQL_DBPASSWORD = 'rounduptest'
74 MYSQL_DBNAME = 'rounduptest'
75 MYSQL_DATABASE = (MYSQL_DBHOST, MYSQL_DBUSER, MYSQL_DBPASSWORD, MYSQL_DBNAME)
76 POSTGRESQL_DATABASE = {'database': 'rounduptest'}
77 """
78 f = open(os.path.join(home, 'config.py'), 'w')
79 f.write(s)
80 f.close()
82 # initialise the database
83 init.initialise(home, 'admin')
85 # add the "demo" user
86 tracker = instance.open(home)
87 db = tracker.open('admin')
88 db.user.create(username='demo', password=password.Password('demo'),
89 realname='Demo User', roles='User')
90 db.commit()
91 db.close()
93 def run_demo():
94 ''' Run a demo server for users to play with for instant gratification.
96 Sets up the web service on localhost. Disables nosy lists.
97 '''
98 home = os.path.abspath('demo')
99 backend = 'anydbm'
100 if not os.path.exists(home) or sys.argv[-1] == 'nuke':
101 if len(sys.argv) > 2:
102 backend = sys.argv[1]
103 install_demo(home, backend)
105 f = open(os.path.join(home, 'config.py'), 'r')
106 url = re.search(r'^TRACKER_WEB\s*=\s*[\'"](http.+/)[\'"]$', f.read(),
107 re.M|re.I).group(1)
108 f.close()
109 hostname, port = urlparse.urlparse(url)[1].split(':')
110 port = int(port)
112 # ok, so start up the server
113 from roundup.scripts import roundup_server
114 roundup_server.RoundupRequestHandler.TRACKER_HOMES = {'demo': home}
116 success_message = '''Server running - connect to:
117 %s
118 1. Log in as "demo"/"demo" or "admin"/"admin".
119 2. Hit Control-C to stop the server.
120 3. Re-start the server by running "python demo.py" again.
121 4. Re-initialise the server by running "python demo.py nuke".''' % url
123 sys.argv = sys.argv[:1]
124 roundup_server.run(port, success_message)
126 if __name__ == '__main__':
127 run_demo()
129 # vim: set filetype=python ts=4 sw=4 et si