Code

Print a nicer error message (without usage) when the address is
authorjlgijsbers <jlgijsbers@57a73879-2fb5-44c3-a270-3262357dd7e2>
Sat, 25 Oct 2003 12:26:42 +0000 (12:26 +0000)
committerjlgijsbers <jlgijsbers@57a73879-2fb5-44c3-a270-3262357dd7e2>
Sat, 25 Oct 2003 12:26:42 +0000 (12:26 +0000)
already in use (bug #798659). Factor out error() function to enable this.

git-svn-id: http://svn.roundup-tracker.org/svnroot/roundup/trunk@1940 57a73879-2fb5-44c3-a270-3262357dd7e2

roundup/scripts/roundup_server.py

index f87ce02f91073929fb1e61fd110c24cb2ce3d52d..757951ad4de468f42fac090054f4be0b72e239ed 100644 (file)
 # 
 """ HTTP Server that serves roundup.
 
-$Id: roundup_server.py,v 1.31 2003-10-25 11:41:06 jlgijsbers Exp $
+$Id: roundup_server.py,v 1.32 2003-10-25 12:26:42 jlgijsbers Exp $
 """
 
 # python version check
 from roundup import version_check
 
 import sys, os, urllib, StringIO, traceback, cgi, binascii, getopt, imp
-import BaseHTTPServer, socket
+import BaseHTTPServer, socket, errno
 
 # Roundup modules of use here
 from roundup.cgi import cgitb, client
@@ -198,10 +198,14 @@ class RoundupRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
             host, port = self.client_address
             return socket.getfqdn(host)
 
+def error():
+    exc_type, exc_value = sys.exc_info()[:2]
+    return _('Error: %s: %s' % (exc_type, exc_value))
+
 def usage(message=''):
-    if message:
-        message = _('Error: %(error)s\n\n')%{'error': message}
-    print _('''%(message)sUsage:
+    print _('''%(message)s
+
+Usage:
 roundup-server [options] [name=tracker home]*
 
 options:
@@ -299,7 +303,13 @@ def run(port=8080, success_message=None):
         # obtain server before changing user id - allows to use port <
         # 1024 if started as root
         address = (hostname, port)
-        httpd = BaseHTTPServer.HTTPServer(address, RoundupRequestHandler)
+        try:
+            httpd = BaseHTTPServer.HTTPServer(address, RoundupRequestHandler)
+        except socket.error, e:
+            if e[0] == errno.EADDRINUSE:
+                raise socket.error, \
+                      _("Unable to bind to port %s, port already in use." % port)
+            raise
 
         if group is not None and hasattr(os, 'getgid'):
             # if root, setgid to the running user
@@ -347,9 +357,11 @@ def run(port=8080, success_message=None):
             RoundupRequestHandler.TRACKER_HOMES = d
     except SystemExit:
         raise
+    except ValueError:
+        usage(error())
     except:
-        exc_type, exc_value = sys.exc_info()[:2]
-        usage('%s: %s'%(exc_type, exc_value))
+        print error()
+        sys.exit(1)
 
     # we don't want the cgi module interpreting the command-line args ;)
     sys.argv = sys.argv[:1]