Index: tools/gformat.py
===================================================================
--- tools/gformat.py	(revision 11326)
+++ tools/gformat.py	(revision 11426)
@@ -13,4 +13,16 @@
 #  </Directory>
 #
+# MUCH FASTER WILL IT BE with mod_wsgi, due to caches and avoiding loading all
+# the heavy template lifting all the time.
+#
+# WSGIDaemonProcess gformat processes=2 threads=25
+# WSGISocketPrefix run/wsgi
+# 
+# <Directory /var/www/cgi-bin>
+#   WSGIProcessGroup gformat
+# </Directory>
+# WSGIScriptAlias /hello /var/www/cgi-bin/genesis/tools/gformat.py
+#
+#
 # Rick van der Zwet <info@rickvanderzwet.nl>
 #
@@ -26,11 +38,12 @@
 import copy
 import glob
+import make_network_kml
+import math
+import rdnap
 import socket
 import string
 import subprocess
 import time
-import rdnap
-import math
-import make_network_kml
+import urlparse
 from pprint import pprint
 from collections import defaultdict
@@ -94,5 +107,15 @@
 UNKNOWN = 90
 
+
 datadump_cache = {}
+interface_list_cache = {}
+rc_conf_local_cache = {}
+def clear_cache():
+  ''' Poor mans cache implementation '''
+  global datadump_cache, interface_list_cache, rc_conf_local_cache
+  datadump_cache = {}
+  interface_list_cache = {}
+  rc_conf_local_cache = {}
+  
 
 NO_DHCP = 0
@@ -527,5 +550,4 @@
 
 
-interface_list_cache = {}
 def make_interface_list(datadump):
   if interface_list_cache.has_key(datadump['autogen_item']):
@@ -609,5 +631,4 @@
 ileiden_proxies = []
 normal_proxies = []
-rc_conf_local_cache = {}
 def generate_rc_conf_local(datadump):
   """ Generate configuration file '/etc/rc.conf.local' """
@@ -1108,50 +1129,52 @@
 
 
-def process_cgi_request():
+def process_cgi_request(environ=os.environ):
   """ When calling from CGI """
+  response_headers = []
+  content_type = 'text/plain'
+
   # Update repository if requested
-  form = cgi.FieldStorage()
-  if form.getvalue("action") == "update":
-    print "Refresh: 5; url=."
-    print "Content-type:text/plain\r\n\r\n",
-    print "[INFO] Updating subverion, please wait..."
-    print subprocess.Popen(['svn', 'cleanup', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
-    print subprocess.Popen(['svn', 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0],
-    print "[INFO] All done, redirecting in 5 seconds"
-    sys.exit(0)
-
-
-  base_uri = os.environ['PATH_INFO']
-  uri = base_uri.strip('/').split('/')
-
-  output = "Template Holder"
-  content_type='text/plain'
-  if base_uri.endswith('/create/network.kml'):
-      content_type='application/vnd.google-earth.kml+xml'
-      output = make_network_kml.make_graph()
-  elif not uri[0]:
-    if is_text_request():
-      content_type = 'text/plain'
-      output = '\n'.join(get_hostlist())
+  form = urlparse.parse_qs(environ['QUERY_STRING'])
+  if form.has_key("action") and "update" in form["action"]:
+    output = "[INFO] Updating subverion, please wait...\n"
+    output += subprocess.Popen(['svn', 'cleanup', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
+    output += subprocess.Popen(['svn', 'up', "%s/.." % NODE_DIR], stderr=subprocess.STDOUT, stdout=subprocess.PIPE).communicate()[0]
+    output += "[INFO] All done, redirecting in 5 seconds"
+    response_headers += [
+      ('Refresh', '5; url=.'),
+    ]
+    clear_cache()
+  else:
+    base_uri = environ['PATH_INFO']
+    uri = base_uri.strip('/').split('/')
+
+    output = "Template Holder"
+    if base_uri.endswith('/create/network.kml'):
+        content_type='application/vnd.google-earth.kml+xml'
+        output = make_network_kml.make_graph()
+    elif not uri[0]:
+      if is_text_request(environ):
+        output = '\n'.join(get_hostlist())
+      else:
+        content_type = 'text/html'
+        output = generate_title(get_hostlist())
+    elif len(uri) == 1:
+      if is_text_request(environ):
+        output = generate_node(uri[0])
+      else:
+        content_type = 'text/html'
+        output = generate_node_overview(uri[0])
+    elif len(uri) == 2:
+      output = generate_config(uri[0], uri[1])
     else:
-      content_type = 'text/html'
-      output = generate_title(get_hostlist())
-  elif len(uri) == 1:
-    if is_text_request():
-      content_type = 'text/plain'
-      output = generate_node(uri[0])
-    else:
-      content_type = 'text/html'
-      output = generate_node_overview(uri[0])
-  elif len(uri) == 2:
-    content_type = 'text/plain'
-    output = generate_config(uri[0], uri[1])
-  else:
-    assert False, "Invalid option"
-
-  print "Content-Type: %s" % content_type
-  print "Content-Length: %s" % len(output)
-  print ""
-  print output
+      assert False, "Invalid option"
+
+  # Return response
+  response_headers += [
+    ('Content-type', content_type),
+    ('Content-Length', str(len(output))),
+  ]
+  return(response_headers, str(output))
+
 
 def get_realname(datadump):
@@ -1392,8 +1415,8 @@
 
 
-def is_text_request():
+def is_text_request(environ=os.environ):
   """ Find out whether we are calling from the CLI or any text based CLI utility """
   try:
-    return os.environ['HTTP_USER_AGENT'].split()[0] in ['curl', 'fetch', 'wget']
+    return environ['HTTP_USER_AGENT'].split()[0] in ['curl', 'fetch', 'wget']
   except KeyError:
     return True
@@ -1848,4 +1871,13 @@
     process_cgi_request()
 
+def application(environ, start_response):
+  status = '200 OK'
+  response_headers, output = process_cgi_request(environ)
+  start_response(status, response_headers)
+
+  # Debugging only
+  # output = 'wsgi.multithread = %s' % repr(environ['wsgi.multithread'])
+  # soutput += '\nwsgi.multiprocess = %s' % repr(environ['wsgi.multiprocess'])
+  return [output]
 
 if __name__ == "__main__":
