comparison roundup/demo.py @ 5247:7f00a47b3559

Make demo.py run under either Python 2 or 3.
author Eric S. Raymond <esr@thyrsus.com>
date Thu, 24 Aug 2017 17:55:02 -0400
parents 04e48cfc91da
children d8d046749b5b
comparison
equal deleted inserted replaced
5246:c5dd7b151ec2 5247:7f00a47b3559
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright (c) 2003 Richard Jones (richard@mechanicalcat.net) 3 # Copyright (c) 2003 Richard Jones (richard@mechanicalcat.net)
4 # 4 #
5 from __future__ import print_function
5 6
6 import errno 7 import errno
7 import os 8 import os
8 import shutil 9 import shutil
9 import socket 10 import socket
10 import sys 11 import sys
11 import urlparse
12 import getopt 12 import getopt
13
14 try:
15 import urlparse
16 except ImportError:
17 import urllib.parse as urlparse
13 18
14 from roundup import configuration 19 from roundup import configuration
15 from roundup.scripts import roundup_server 20 from roundup.scripts import roundup_server
16 21
17 # Path where demo instance files will be stored 22 # Path where demo instance files will be stored
49 # see if we need to clean up existing directory 54 # see if we need to clean up existing directory
50 if os.path.exists(home): 55 if os.path.exists(home):
51 if os.path.exists(home + '/config.ini'): 56 if os.path.exists(home + '/config.ini'):
52 # clear everything out to avoid conflicts with former 57 # clear everything out to avoid conflicts with former
53 # extensions and detectors 58 # extensions and detectors
54 print "Nuking directory left from the previous demo instance." 59 print("Nuking directory left from the previous demo instance.")
55 shutil.rmtree(home) 60 shutil.rmtree(home)
56 else: 61 else:
57 print "Error: Refusing to nuke non-tracker directory:" 62 print("Error: Refusing to nuke non-tracker directory:")
58 print " %s" % home 63 print(" %s" % home)
59 sys.exit(1) 64 sys.exit(1)
60 65
61 template_dir = os.path.join('share', 'roundup', 'templates', template) 66 template_dir = os.path.join('share', 'roundup', 'templates', template)
62 init.install(home, template_dir) 67 init.install(home, template_dir)
63 # don't have email flying around 68 # don't have email flying around
71 # figure basic params for server 76 # figure basic params for server
72 hostname = 'localhost' 77 hostname = 'localhost'
73 # pick a fairly odd, random port 78 # pick a fairly odd, random port
74 port = 8917 79 port = 8917
75 while 1: 80 while 1:
76 print 'Trying to set up web server on port %d ...'%port, 81 print('Trying to set up web server on port %d ...'%port,)
77 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 82 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
78 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 83 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
79 try: 84 try:
80 s.connect((hostname, port)) 85 s.connect((hostname, port))
81 except socket.error, e: 86 except socket.error as e:
82 if not hasattr(e, 'args') or e.args[0] != errno.ECONNREFUSED: 87 if not hasattr(e, 'args') or e.args[0] != errno.ECONNREFUSED:
83 raise 88 raise
84 print 'should be ok.' 89 print('should be ok.')
85 break 90 break
86 else: 91 else:
87 s.close() 92 s.close()
88 print 'already in use.' 93 print('already in use.')
89 port += 100 94 port += 100
90 config['TRACKER_WEB'] = 'http://%s:%s/demo/'%(hostname, port) 95 config['TRACKER_WEB'] = 'http://%s:%s/demo/'%(hostname, port)
91 96
92 # write the config 97 # write the config
93 config['INSTANT_REGISTRATION'] = 1 98 config['INSTANT_REGISTRATION'] = 1
115 db.commit() 120 db.commit()
116 db.close() 121 db.close()
117 122
118 def run_demo(home): 123 def run_demo(home):
119 """Run the demo tracker instance from its ``home`` directory""" 124 """Run the demo tracker instance from its ``home`` directory"""
120 print "Demo Tracker Home:", home 125 print("Demo Tracker Home:", home)
121 126
122 cfg = configuration.CoreConfig(home) 127 cfg = configuration.CoreConfig(home)
123 url = cfg["TRACKER_WEB"] 128 url = cfg["TRACKER_WEB"]
124 hostname, port = urlparse.urlparse(url)[1].split(':') 129 hostname, port = urlparse.urlparse(url)[1].split(':')
125 port = int(port) 130 port = int(port)
143 roundup_server.run(success_message=success_message) 148 roundup_server.run(success_message=success_message)
144 149
145 150
146 def usage(msg = ''): 151 def usage(msg = ''):
147 if msg: 152 if msg:
148 print msg 153 print(msg)
149 print """\ 154 print("""\
150 Usage: %(script)s [options] [nuke] 155 Usage: %(script)s [options] [nuke]
151 156
152 Run a demo server. Config and database files are created in 157 Run a demo server. Config and database files are created in
153 %(datadir)s subdirectory of %(script)s dir. 158 %(datadir)s subdirectory of %(script)s dir.
154 159
158 163
159 Options: 164 Options:
160 -h -- print this help message 165 -h -- print this help message
161 -t template -- specify the tracker template to use 166 -t template -- specify the tracker template to use
162 -b backend -- specify the database backend to use 167 -b backend -- specify the database backend to use
163 """ % dict(script=sys.argv[0], datadir=TRACKER_HOME+os.sep) 168 """ % dict(script=sys.argv[0], datadir=TRACKER_HOME+os.sep))
164 169
165 170
166 def main(): 171 def main():
167 """Run a demo server for users to play with for instant gratification. 172 """Run a demo server for users to play with for instant gratification.
168 173
169 Sets up the web service on localhost. Disables nosy lists. 174 Sets up the web service on localhost. Disables nosy lists.
170 """ 175 """
171 176
172 try: 177 try:
173 opts, args = getopt.getopt(sys.argv[1:], 't:b:h') 178 opts, args = getopt.getopt(sys.argv[1:], 't:b:h')
174 except getopt.GetoptError, e: 179 except getopt.GetoptError as e:
175 usage(str(e)) 180 usage(str(e))
176 return 1 181 return 1
177 for opt, arg in opts: 182 for opt, arg in opts:
178 if opt == '-h': 183 if opt == '-h':
179 usage() 184 usage()
192 if (len(args) > 1 or 197 if (len(args) > 1 or
193 (len(args) == 1 and args[0] != 'nuke')): 198 (len(args) == 1 and args[0] != 'nuke')):
194 usage() 199 usage()
195 return 1 200 return 1
196 201
197 print "Initializing demo instance in:\n %s" % home 202 print("Initializing demo instance in:\n %s" % home)
198 install_demo(home, backend, template) 203 install_demo(home, backend, template)
199 elif opts: 204 elif opts:
200 print "Error: Arguments are not allowed when running an existing demo." 205 print("Error: Arguments are not allowed when running an existing demo.")
201 print " Use the 'nuke' command to start over." 206 print(" Use the 'nuke' command to start over.")
202 sys.exit(1) 207 sys.exit(1)
203 208
204 run_demo(home) 209 run_demo(home)
205 210
206 211

Roundup Issue Tracker: http://roundup-tracker.org/