From 9901aaf3d6ead464863039c9f1b5a88b3e3df987 Mon Sep 17 00:00:00 2001
From: RaNaN <Mast3rRaNaN@hotmail.de>
Date: Sat, 13 Feb 2010 22:48:12 +0100
Subject: django builtin server improvement

---
 module/web/ServerThread.py | 27 ++++---------------
 module/web/run_server.py   | 66 ++++++++++++++++++++++++++++++++++++++++++++++
 module/web/run_unix.py     | 21 ---------------
 3 files changed, 71 insertions(+), 43 deletions(-)
 create mode 100755 module/web/run_server.py
 delete mode 100755 module/web/run_unix.py

diff --git a/module/web/ServerThread.py b/module/web/ServerThread.py
index 5d0dc8738..712cd4304 100644
--- a/module/web/ServerThread.py
+++ b/module/web/ServerThread.py
@@ -64,26 +64,14 @@ class WebServer(threading.Thread):
             command2 = ['lighttpd', '-D', '-f', join(path, "lighttpd", "lighttpd.conf")]
             self.p2 = Popen(command2, stderr=PIPE, stdin=PIPE, stdout=PIPE)
 
-
-            
+         
         else:
-            self.pycore.logger.info("Starting django buildin Webserver: %s:%s" % (host, port))
+            self.pycore.logger.info("Starting django builtin Webserver: %s:%s" % (host, port))
 
-            if os.name == 'posix':
-                command = ['python', join(self.pycore.path, "module", "web", "run_unix.py"), "runserver", "%s:%s" % (host, port)]
-                self.p = Popen(command, close_fds=True, stderr=PIPE, stdin=PIPE, stdout=PIPE)
-                #os.system("python " + join(self.pycore.path,"module","web","manage.py runserver %s:%s" % (host,port)))
-                #@TODO: better would be real python code
+            command = ['python', join(self.pycore.path, "module", "web", "run_server.py"), "%s:%s" % (host, port)]
+            self.p = Popen(command, stderr=PIPE, stdin=PIPE, stdout=PIPE)
+            while self.running:
                 sleep(1)
-                with open("webserver.pid", "r") as f:
-                    self.pid = int(f.read().strip())
-                while self.running:
-                    sleep(1)
-            else:
-                command = ['python', join(self.pycore.path, "module", "web", "manage.py"), "runserver", "%s:%s" % (host, port)]
-                self.p = Popen(command, stderr=PIPE, stdin=PIPE, stdout=PIPE)
-                while self.running:
-                    sleep(1)
 
     def quit(self):
 
@@ -92,11 +80,6 @@ class WebServer(threading.Thread):
             self.p2.kill()
             return True
 
-        if os.name == 'posix':
-            try:
-                os.kill(self.pid, SIGINT)
-            except:
-                pass
         else:
             self.p.kill()
         
diff --git a/module/web/run_server.py b/module/web/run_server.py
new file mode 100755
index 000000000..3bb9b13f7
--- /dev/null
+++ b/module/web/run_server.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from django.core.management.base import BaseCommand, CommandError
+import os
+import sys
+
+os.environ["DJANGO_SETTINGS_MODULE"] = 'settings'
+
+def handle( *args ):
+    import django
+    from django.core.servers.basehttp import run, AdminMediaHandler, WSGIServerException
+    from django.core.handlers.wsgi import WSGIHandler
+ 
+    try:
+	if len(args) == 1:
+	    try:
+		addr,port = args[0].split(":")
+	    except:
+		addr = "127.0.0.1"
+		port = args[0]
+	else:
+	    addr = args[0]
+	    port = args[1]
+    except:
+	addr = '127.0.0.1'
+	port = '8000'
+
+    print addr, port
+
+    admin_media_path = ''
+    shutdown_message =  ''
+    quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'
+
+    from django.conf import settings
+    from django.utils import translation
+
+    print "\nDjango version %s, using settings %r" % (django.get_version(), settings.SETTINGS_MODULE)
+    print "Development server is running at http://%s:%s/" % (addr, port)
+    print "Quit the server with %s." % quit_command
+
+    translation.activate(settings.LANGUAGE_CODE)
+
+    try:
+	handler = AdminMediaHandler(WSGIHandler(), admin_media_path)
+	run(addr, int(port), handler)
+    except WSGIServerException, e:
+	# Use helpful error messages instead of ugly tracebacks.
+	ERRORS = {
+	    13: "You don't have permission to access that port.",
+	    98: "That port is already in use.",
+	    99: "That IP address can't be assigned-to.",
+	}
+	try:
+	    error_text = ERRORS[e.args[0].args[0]]
+	except (AttributeError, KeyError):
+	    error_text = str(e)
+	sys.stderr.write(("Error: %s" % error_text) + '\n')
+	# Need to use an OS exit because sys.exit doesn't work in a thread
+	os._exit(1)
+    except KeyboardInterrupt:
+	if shutdown_message:
+	    print shutdown_message
+	sys.exit(0)
+
+if __name__ == "__main__":
+  handle(*sys.argv[1:])
diff --git a/module/web/run_unix.py b/module/web/run_unix.py
deleted file mode 100755
index 09c67b282..000000000
--- a/module/web/run_unix.py
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/usr/bin/env python
-# -*- coding: utf-8 -*-
-from __future__ import with_statement
-import os
-
-pid = os.fork()
-if pid:
-    with open("webserver.pid", "w") as f:
-        f.write(str(pid))
-else:
-    from django.core.management import execute_manager
-
-    try:
-        import settings # Assumed to be in the same directory.
-    except ImportError:
-        import sys
-        sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
-        sys.exit(1)
-
-    if __name__ == "__main__":
-        execute_manager(settings)
\ No newline at end of file
-- 
cgit v1.2.3