Code

When debugging mail (debug = <filename> setting in [mail] section of
[roundup.git] / demo.py
diff --git a/demo.py b/demo.py
index 0c601dcb72061ac39e39cb502039f004c5f48ee3..0cc4a38af39abf37d96675c7b351d4528ecc3a99 100644 (file)
--- a/demo.py
+++ b/demo.py
@@ -1,29 +1,64 @@
 #! /usr/bin/env python
 #
 # Copyright (c) 2003 Richard Jones (richard@mechanicalcat.net)
-# 
-# $Id: demo.py,v 1.2 2003-05-11 07:33:55 richard Exp $
+#
 
-import sys, os, string, re, urlparse
-import shutil, socket, errno, BaseHTTPServer
+import errno
+import os
+import socket
+import sys
+import urlparse
 from glob import glob
+import getopt
 
-def install_demo(home):
-    # create the instance
-    try:
-        if os.path.exists(home):
-            shutil.rmtree(home)
-    except os.error, error:
-        if error.errno != errno.ENOENT:
-            raise
-    from roundup import init, instance, password
-    init.install(home, os.path.join('templates', 'classic'))
+from roundup import configuration
+from roundup.scripts import roundup_server
+
+def install_demo(home, backend, template):
+    """Install a demo tracker
+
+    Parameters:
+        home:
+            tracker home directory path
+        backend:
+            database backend name
+        template:
+            tracker template
+
+    """
+
+    from roundup import init, instance, password, backends
+
+    # set up the config for this tracker
+    config = configuration.CoreConfig()
+    config['TRACKER_HOME'] = home
+    config['MAIL_DOMAIN'] = 'localhost'
+    config['DATABASE'] = 'db'
+    config['WEB_DEBUG'] = True
+    if backend in ('mysql', 'postgresql'):
+        config['RDBMS_HOST'] = 'localhost'
+        config['RDBMS_USER'] = 'rounduptest'
+        config['RDBMS_PASSWORD'] = 'rounduptest'
+        config['RDBMS_NAME'] = 'rounduptest'
+
+    # see if we have further db nuking to perform
+    module = backends.get_backend(backend)
+    if module.db_exists(config):
+        module.db_nuke(config)
+
+    template_dir = os.path.join('share', 'roundup', 'templates', template)
+    init.install(home, template_dir)
     # don't have email flying around
-    os.remove(os.path.join(home, 'detectors', 'nosyreaction.py'))
-    init.write_select_db(home, 'anydbm')
+    nosyreaction = os.path.join(home, 'detectors', 'nosyreaction.py')
+    if os.path.exists(nosyreaction):
+        os.remove(nosyreaction)
+    nosyreaction += 'c'
+    if os.path.exists(nosyreaction):
+        os.remove(nosyreaction)
+    init.write_select_db(home, backend)
 
     # figure basic params for server
-    hostname = socket.gethostname()
+    hostname = 'localhost'
     # pick a fairly odd, random port
     port = 8917
     while 1:
@@ -41,59 +76,106 @@ def install_demo(home):
             s.close()
             print 'already in use.'
             port += 100
-    url = 'http://%s:%s/demo/'%(hostname, port)
+    config['TRACKER_WEB'] = 'http://%s:%s/demo/'%(hostname, port)
 
     # write the config
-    f = open(os.path.join(home, 'config.py'), 'r')
-    s = f.read().replace('http://tracker.example/cgi-bin/roundup.cgi/bugs/',
-        url)
-    f.close()
-    f = open(os.path.join(home, 'config.py'), 'w')
-    f.write(s)
-    f.close()
+    config['INSTANT_REGISTRATION'] = 1
+    config.save(os.path.join(home, config.INI_FILE))
 
-    # initialise the database
-    init.initialise(home, 'admin')
+    # open the tracker and initialise
+    tracker = instance.open(home)
+    tracker.init(password.Password('admin'))
 
     # add the "demo" user
-    tracker = instance.open(home)
     db = tracker.open('admin')
-    db.user.create(username='demo', password=password.Password('demo'),
-        realname='Demo User', roles='User')
+    # FIXME: Move tracker-specific demo initialization into the tracker templates.
+    if (template == 'minimal'):
+        db.user.create(username='demo', password=password.Password('demo'),
+                       roles='User')
+    else:
+        db.user.create(username='demo', password=password.Password('demo'),
+                       realname='Demo User', roles='User')
     db.commit()
     db.close()
 
-def run_demo():
-    ''' Run a demo server for users to play with for instant gratification.
-
-        Sets up the web service on localhost. Disables nosy lists.
-    '''
-    home = os.path.abspath('demo')
-    if not os.path.exists(home) or sys.argv[-1] == 'nuke':
-        install_demo(home)
-
-    f = open(os.path.join(home, 'config.py'), 'r')
-    url = re.search(r'^TRACKER_WEB\s*=\s*[\'"](http.+/)[\'"]$', f.read(),
-        re.M|re.I).group(1)
-    f.close()
+def run_demo(home):
+    """Run the demo tracker installed in ``home``"""
+    cfg = configuration.CoreConfig(home)
+    url = cfg["TRACKER_WEB"]
     hostname, port = urlparse.urlparse(url)[1].split(':')
     port = int(port)
+    success_message = '''Server running - connect to:
+    %s
+1. Log in as "demo"/"demo" or "admin"/"admin".
+2. Hit Control-C to stop the server.
+3. Re-start the server by running "roundup-demo" again.
+4. Re-initialise the server by running "roundup-demo nuke".
+
+Demo tracker is set up to be accessed by localhost browser.  If you
+run demo on a server host, please stop the demo, open file
+"demo/config.ini" with your editor, change the host name in the "web"
+option in section "[tracker]", save the file, then re-run the demo
+program. If you want to change backend types, you must use "nuke".
+
+''' % url
+
+    # disable command line processing in roundup_server
+    sys.argv = sys.argv[:1] + ['-p', str(port), 'demo=' + home]
+    roundup_server.run(success_message=success_message)
+
+
+def usage(msg = ''):
+
+    if msg: print msg
+    print 'Usage: %s [options] [nuke]'%sys.argv[0]
+    print """
+Options:
+ -h                -- print this help message
+ -t template       -- specify the tracker template to use
+ -b backend        -- specify the database backend to use
+"""
+
+
+def main():
+    """Run a demo server for users to play with for instant gratification.
+
+    Sets up the web service on localhost. Disables nosy lists.
+    """
 
-    # ok, so start up the server
-    from roundup.scripts.roundup_server import RoundupRequestHandler
-    RoundupRequestHandler.TRACKER_HOMES = {'demo': home}
-    httpd = BaseHTTPServer.HTTPServer((hostname, port), RoundupRequestHandler)
-    print 'Server running - connect to:\n  %s'%url
-    print '1. Log in as "demo"/"demo" or "admin"/"admin".'
-    print '2. Hit Control-C to stop the server.'
-    print '3. Re-start the server by running "python demo.py" again.'
-    print '4. Re-initialise the server by running "python demo.py nuke".'
     try:
-        httpd.serve_forever()
-    except KeyboardInterrupt:
-        print 'Keyboard Interrupt: exiting'
+        opts, args = getopt.getopt(sys.argv[1:], 't:b:h')
+    except getopt.GetoptError, e:
+        usage(str(e))
+        return 1
+
+    home = os.path.abspath('demo')
+    nuke = args and args[0] == 'nuke'
+    if not os.path.exists(home) or nuke:
+        backend = 'anydbm'
+        template = 'classic'
+        for opt, arg in opts:
+            if opt == '-h':
+                usage()
+                return 0
+            elif opt == '-t':
+                template = arg
+            elif opt == '-b':
+                backend = arg
+        if (len(args) > 1 or
+            (len(args) == 1 and args[0] != 'nuke')):
+            usage()
+            return 1
+
+        install_demo(home, backend, template)
+    elif opts:
+        print "Error: Arguments are not allowed when running an existing demo."
+        print "       Use the 'nuke' command to start over."
+        sys.exit(1)
+
+    run_demo(home)
+
 
 if __name__ == '__main__':
-    run_demo()
+    sys.exit(main())
 
-# vim: set filetype=python ts=4 sw=4 et si
+# vim: set filetype=python sts=4 sw=4 et si :