c840be6ea4177ee35cf303ba2fee0986a58477bd
1 #! /usr/bin/env python
2 #
3 # Copyright (c) 2003 Richard Jones (richard@mechanicalcat.net)
4 #
5 # $Id: demo.py,v 1.4 2003-07-27 23:16:33 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):
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 os.remove(os.path.join(home, 'detectors', 'nosyreaction.pyc'))
20 init.write_select_db(home, 'anydbm')
22 # figure basic params for server
23 hostname = socket.gethostname()
24 # pick a fairly odd, random port
25 port = 8917
26 while 1:
27 print 'Trying to set up web server on port %d ...'%port,
28 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
30 try:
31 s.connect((hostname, port))
32 except socket.error, e:
33 if not hasattr(e, 'args') or e.args[0] != errno.ECONNREFUSED:
34 raise
35 print 'should be ok.'
36 break
37 else:
38 s.close()
39 print 'already in use.'
40 port += 100
41 url = 'http://%s:%s/demo/'%(hostname, port)
43 # write the config
44 f = open(os.path.join(home, 'config.py'), 'r')
45 s = f.read().replace('http://tracker.example/cgi-bin/roundup.cgi/bugs/',
46 url)
47 f.close()
48 f = open(os.path.join(home, 'config.py'), 'w')
49 f.write(s)
50 f.close()
52 # initialise the database
53 init.initialise(home, 'admin')
55 # add the "demo" user
56 tracker = instance.open(home)
57 db = tracker.open('admin')
58 db.user.create(username='demo', password=password.Password('demo'),
59 realname='Demo User', roles='User')
60 db.commit()
61 db.close()
63 def run_demo():
64 ''' Run a demo server for users to play with for instant gratification.
66 Sets up the web service on localhost. Disables nosy lists.
67 '''
68 home = os.path.abspath('demo')
69 if not os.path.exists(home) or sys.argv[-1] == 'nuke':
70 install_demo(home)
72 f = open(os.path.join(home, 'config.py'), 'r')
73 url = re.search(r'^TRACKER_WEB\s*=\s*[\'"](http.+/)[\'"]$', f.read(),
74 re.M|re.I).group(1)
75 f.close()
76 hostname, port = urlparse.urlparse(url)[1].split(':')
77 port = int(port)
79 # ok, so start up the server
80 from roundup.scripts.roundup_server import RoundupRequestHandler
81 RoundupRequestHandler.TRACKER_HOMES = {'demo': home}
82 httpd = BaseHTTPServer.HTTPServer((hostname, port), RoundupRequestHandler)
83 print 'Server running - connect to:\n %s'%url
84 print '1. Log in as "demo"/"demo" or "admin"/"admin".'
85 print '2. Hit Control-C to stop the server.'
86 print '3. Re-start the server by running "python demo.py" again.'
87 print '4. Re-initialise the server by running "python demo.py nuke".'
88 try:
89 httpd.serve_forever()
90 except KeyboardInterrupt:
91 print 'Keyboard Interrupt: exiting'
93 if __name__ == '__main__':
94 run_demo()
96 # vim: set filetype=python ts=4 sw=4 et si