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