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