Skip to content

Commit 459e3e8

Browse files
committed
Move demo.py to roundup.demo where it appears when installed.
This removes part of distutils building magic and brings Roundup sources closer to the state how it works when installed. Previously I've introduced a bug where I've made "demo" tracker home configurable and by default created in the dir the demo.py script, which became directory of roundup.demo module after installation. This will be fixed in the next commit. Simplicity matters.
1 parent b3637c0 commit 459e3e8

File tree

5 files changed

+208
-230
lines changed

5 files changed

+208
-230
lines changed

2to3-done.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ TODO
4242
./roundup/dist/command/bdist_rpm.py
4343
./roundup/dist/command/build.py
4444
./roundup/dist/command/build_doc.py
45-
./roundup/dist/command/build_py.py
4645
./roundup/dist/command/build_scripts.py
4746
./roundup/exceptions.py
4847
./roundup/hyperdb.py

demo.py

Lines changed: 6 additions & 202 deletions
Original file line numberDiff line numberDiff line change
@@ -1,202 +1,6 @@
1-
#!/usr/bin/env python
2-
#
3-
# Copyright (c) 2003 Richard Jones ([email protected])
4-
#
5-
6-
import errno
7-
import os
8-
import shutil
9-
import socket
10-
import sys
11-
import urlparse
12-
import getopt
13-
14-
from roundup import configuration
15-
from roundup.scripts import roundup_server
16-
17-
# Path where demo instance files will be stored
18-
TRACKER_HOME = os.path.join(os.path.dirname(__file__), 'demo')
19-
20-
def install_demo(home, backend, template):
21-
"""Install a demo tracker
22-
23-
Parameters:
24-
home:
25-
tracker home directory path
26-
backend:
27-
database backend name
28-
template:
29-
tracker template
30-
31-
"""
32-
33-
from roundup import init, instance, password, backends
34-
35-
# set up the config for this tracker
36-
config = configuration.CoreConfig()
37-
config['TRACKER_HOME'] = home
38-
config['MAIL_DOMAIN'] = 'localhost'
39-
config['DATABASE'] = 'db'
40-
config['WEB_DEBUG'] = True
41-
if backend in ('mysql', 'postgresql'):
42-
config['RDBMS_HOST'] = 'localhost'
43-
config['RDBMS_USER'] = 'rounduptest'
44-
config['RDBMS_PASSWORD'] = 'rounduptest'
45-
config['RDBMS_NAME'] = 'rounduptest'
46-
47-
# see if we need to clean up existing directory
48-
if os.path.exists(home):
49-
if os.path.exists(home + '/config.ini'):
50-
# clear everything out to avoid conflicts with former
51-
# extensions and detectors
52-
print "Nuking directory left from the previous demo instance."
53-
shutil.rmtree(home)
54-
else:
55-
print "Error: Refusing to nuke non-tracker directory:"
56-
print " %s" % home
57-
sys.exit(1)
58-
59-
template_dir = os.path.join('share', 'roundup', 'templates', template)
60-
init.install(home, template_dir)
61-
# don't have email flying around
62-
nosyreaction = os.path.join(home, 'detectors', 'nosyreaction.py')
63-
if os.path.exists(nosyreaction):
64-
os.remove(nosyreaction)
65-
nosyreaction += 'c'
66-
if os.path.exists(nosyreaction):
67-
os.remove(nosyreaction)
68-
init.write_select_db(home, backend)
69-
70-
# figure basic params for server
71-
hostname = 'localhost'
72-
# pick a fairly odd, random port
73-
port = 8917
74-
while 1:
75-
print 'Trying to set up web server on port %d ...'%port,
76-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
77-
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
78-
try:
79-
s.connect((hostname, port))
80-
except socket.error, e:
81-
if not hasattr(e, 'args') or e.args[0] != errno.ECONNREFUSED:
82-
raise
83-
print 'should be ok.'
84-
break
85-
else:
86-
s.close()
87-
print 'already in use.'
88-
port += 100
89-
config['TRACKER_WEB'] = 'http://%s:%s/demo/'%(hostname, port)
90-
91-
# write the config
92-
config['INSTANT_REGISTRATION'] = 1
93-
# FIXME: Move template-specific demo initialization into the templates.
94-
if template == 'responsive':
95-
config['STATIC_FILES'] = "static"
96-
if template == 'jinja2':
97-
config['TEMPLATE_ENGINE'] = 'jinja2'
98-
config['STATIC_FILES'] = "static"
99-
config.save(os.path.join(home, config.INI_FILE))
100-
101-
# open the tracker and initialise
102-
tracker = instance.open(home)
103-
tracker.init(password.Password('admin'))
104-
105-
# add the "demo" user
106-
db = tracker.open('admin')
107-
# FIXME: Move tracker-specific demo initialization into the tracker templates.
108-
if template == 'minimal':
109-
db.user.create(username='demo', password=password.Password('demo'),
110-
roles='User')
111-
else:
112-
db.user.create(username='demo', password=password.Password('demo'),
113-
realname='Demo User', roles='User')
114-
db.commit()
115-
db.close()
116-
117-
def run_demo(home):
118-
"""Run the demo tracker instance from its ``home`` directory"""
119-
cfg = configuration.CoreConfig(home)
120-
url = cfg["TRACKER_WEB"]
121-
hostname, port = urlparse.urlparse(url)[1].split(':')
122-
port = int(port)
123-
success_message = '''Server running - connect to:
124-
%(url)s
125-
1. Log in as "demo"/"demo" or "admin"/"admin".
126-
2. Hit Control-C to stop the server.
127-
3. Re-start the server by running "%(script)s" again.
128-
4. Re-initialise the server by running "%(script)s nuke".
129-
130-
Demo tracker is set up to be accessed by localhost browser. If you
131-
run demo on a server host, please stop the demo, open file
132-
"%(datadir)s/config.ini" with your editor, change the host name in the "web"
133-
option in section "[tracker]", save the file, then re-run the demo
134-
program. If you want to change backend types, you must use "nuke".
135-
136-
''' % dict(url=url, script=sys.argv[0], datadir=TRACKER_HOME)
137-
138-
# disable command line processing in roundup_server
139-
sys.argv = sys.argv[:1] + ['-p', str(port), '-n', hostname, 'demo=' + home]
140-
roundup_server.run(success_message=success_message)
141-
142-
143-
def usage(msg = ''):
144-
145-
if msg: print msg
146-
print 'Usage: %s [options] [nuke]'%sys.argv[0]
147-
print """
148-
Run a demo server. Config and database files are created
149-
in %(datadir)s/ subdirectory of %(script)s dir.
150-
151-
Options:
152-
-h -- print this help message
153-
-t template -- specify the tracker template to use
154-
-b backend -- specify the database backend to use
155-
""" % dict(script=sys.argv[0], datadir=TRACKER_HOME)
156-
157-
158-
def main():
159-
"""Run a demo server for users to play with for instant gratification.
160-
161-
Sets up the web service on localhost. Disables nosy lists.
162-
"""
163-
164-
try:
165-
opts, args = getopt.getopt(sys.argv[1:], 't:b:h')
166-
except getopt.GetoptError, e:
167-
usage(str(e))
168-
return 1
169-
for opt, arg in opts:
170-
if opt == '-h':
171-
usage()
172-
return 0
173-
174-
home = os.path.abspath(TRACKER_HOME)
175-
nuke = args and args[0] == 'nuke'
176-
if not os.path.exists(home) or nuke:
177-
backend = 'anydbm'
178-
template = 'classic'
179-
for opt, arg in opts:
180-
if opt == '-t':
181-
template = arg
182-
elif opt == '-b':
183-
backend = arg
184-
if (len(args) > 1 or
185-
(len(args) == 1 and args[0] != 'nuke')):
186-
usage()
187-
return 1
188-
189-
print "Initializing demo instance in:\n %s" % home
190-
install_demo(home, backend, template)
191-
elif opts:
192-
print "Error: Arguments are not allowed when running an existing demo."
193-
print " Use the 'nuke' command to start over."
194-
sys.exit(1)
195-
196-
run_demo(home)
197-
198-
199-
if __name__ == '__main__':
200-
sys.exit(main())
201-
202-
# vim: set filetype=python sts=4 sw=4 et si :
1+
import sys
2+
import roundup
3+
4+
from roundup.demo import main
5+
6+
sys.exit(main())

0 commit comments

Comments
 (0)