hgweb: internalize some socket details
authorMatt Mackall <mpm@selenic.com>
Mon, 13 Nov 2006 13:26:57 -0600
changeset 3628 dc3504af7722
parent 3627 44e75d9fa654
child 3629 4cfb72bcb978
hgweb: internalize some socket details
mercurial/commands.py
mercurial/hgweb/server.py
--- a/mercurial/commands.py	Mon Nov 13 13:26:57 2006 -0600
+++ b/mercurial/commands.py	Mon Nov 13 13:26:57 2006 -0600
@@ -11,7 +11,7 @@
 demandload(globals(), "os re sys signal shutil imp urllib pdb shlex")
 demandload(globals(), "fancyopts ui hg util lock revlog templater bundlerepo")
 demandload(globals(), "difflib patch tempfile time")
-demandload(globals(), "traceback errno socket version atexit sets bz2")
+demandload(globals(), "traceback errno version atexit sets bz2")
 demandload(globals(), "archival changegroup cmdutil hgweb.server sshserver")
 
 class UnknownCommand(Exception):
@@ -2531,24 +2531,14 @@
         os.read(rfd, 1)
         os._exit(0)
 
-    try:
-        httpd = hgweb.server.create_server(ui, repo)
-    except socket.error, inst:
-        raise util.Abort(_('cannot start server: %s') % inst.args[1])
+    httpd = hgweb.server.create_server(ui, repo)
 
     if ui.verbose:
-        addr, port = httpd.socket.getsockname()
-        if addr == '0.0.0.0':
-            addr = socket.gethostname()
+        if httpd.port != 80:
+            ui.status(_('listening at http://%s:%d/\n') %
+                      (httpd.addr, httpd.port))
         else:
-            try:
-                addr = socket.gethostbyaddr(addr)[0]
-            except socket.error:
-                pass
-        if port != 80:
-            ui.status(_('listening at http://%s:%d/\n') % (addr, port))
-        else:
-            ui.status(_('listening at http://%s/\n') % addr)
+            ui.status(_('listening at http://%s/\n') % httpd.addr)
 
     if opts['pid_file']:
         fp = open(opts['pid_file'], 'w')
--- a/mercurial/hgweb/server.py	Mon Nov 13 13:26:57 2006 -0600
+++ b/mercurial/hgweb/server.py	Mon Nov 13 13:26:57 2006 -0600
@@ -200,6 +200,16 @@
             self.reqmaker = wsgiapplication(self.make_handler)
             self.daemon_threads = True
 
+            addr, port = self.socket.getsockname()
+            if addr == '0.0.0.0':
+                addr = socket.gethostname()
+            else:
+                try:
+                    addr = socket.gethostbyaddr(addr)[0]
+                except socket.error:
+                    pass
+            self.addr, self.port = addr, port
+
         def make_handler(self):
             if self.webdir_conf:
                 hgwebobj = self.webdirmaker(self.webdir_conf)
@@ -219,7 +229,10 @@
                 raise hg.RepoError(_('IPv6 not available on this system'))
             super(IPv6HTTPServer, self).__init__(*args, **kwargs)
 
-    if use_ipv6:
-        return IPv6HTTPServer((address, port), _hgwebhandler)
-    else:
-        return MercurialHTTPServer((address, port), _hgwebhandler)
+    try:
+        if use_ipv6:
+            return IPv6HTTPServer((address, port), _hgwebhandler)
+        else:
+            return MercurialHTTPServer((address, port), _hgwebhandler)
+    except socket.error, inst:
+        raise util.Abort(_('cannot start server: %s') % inst.args[1])