1 #! /usr/bin/env python
2 #
3 # Copyright (c) 2003 Richard Jones (richard@mechanicalcat.net)
4 #
5 # $Id: demo.py,v 1.7 2003-11-06 14:24:57 jlgijsbers Exp $
7 import sys, os, string, re, urlparse
8 import shutil, socket, errno, BaseHTTPServer
9 from glob import glob
11 def install_demo(home):
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, 'anydbm')
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 f = open(os.path.join(home, 'config.py'), 'w')
53 f.write(s)
54 f.close()
56 # initialise the database
57 init.initialise(home, 'admin')
59 # add the "demo" user
60 tracker = instance.open(home)
61 db = tracker.open('admin')
62 db.user.create(username='demo', password=password.Password('demo'),
63 realname='Demo User', roles='User')
64 db.commit()
65 db.close()
67 def run_demo():
68 ''' Run a demo server for users to play with for instant gratification.
70 Sets up the web service on localhost. Disables nosy lists.
71 '''
72 home = os.path.abspath('demo')
73 if not os.path.exists(home) or sys.argv[-1] == 'nuke':
74 install_demo(home)
76 f = open(os.path.join(home, 'config.py'), 'r')
77 url = re.search(r'^TRACKER_WEB\s*=\s*[\'"](http.+/)[\'"]$', f.read(),
78 re.M|re.I).group(1)
79 f.close()
80 hostname, port = urlparse.urlparse(url)[1].split(':')
81 port = int(port)
83 # ok, so start up the server
84 from roundup.scripts import roundup_server
85 roundup_server.RoundupRequestHandler.TRACKER_HOMES = {'demo': home}
87 success_message = '''Server running - connect to:
88 %s
89 1. Log in as "demo"/"demo" or "admin"/"admin".
90 2. Hit Control-C to stop the server.
91 3. Re-start the server by running "python demo.py" again.
92 4. Re-initialise the server by running "python demo.py nuke".''' % url
94 sys.argv = sys.argv[:1]
95 roundup_server.run(port, success_message)
97 if __name__ == '__main__':
98 run_demo()
100 # vim: set filetype=python ts=4 sw=4 et si