diff mercurial/util.py @ 12076:49463314c24f

mail/hgweb: support service names for ports (issue2350) This adds util.getport(port) which tries to parse port as an int, and failing that, looks it up using socket.getservbyname(). Thus, the following will work: [smtp] port = submission [web] port = http This does not apply to ports in URLs used in clone, pull, etc.
author Brodie Rao <brodie@bitheap.org>
date Sat, 28 Aug 2010 12:31:07 -0400
parents 5d22e631c365
children ff6f5310ad92
line wrap: on
line diff
--- a/mercurial/util.py	Fri Aug 27 16:25:47 2010 +0200
+++ b/mercurial/util.py	Sat Aug 28 12:31:07 2010 -0400
@@ -17,7 +17,7 @@
 import error, osutil, encoding
 import errno, re, shutil, sys, tempfile, traceback
 import os, stat, time, calendar, textwrap, unicodedata, signal
-import imp
+import imp, socket
 
 # Python compatibility
 
@@ -1414,3 +1414,19 @@
     r = re.compile(r'%s(%s)' % (prefix, '|'.join(mapping.keys())))
     return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
 
+def getport(port):
+    """Return the port for a given network service.
+
+    If port is an integer, it's returned as is. If it's a string, it's
+    looked up using socket.getservbyname(). If there's no matching
+    service, util.Abort is raised.
+    """
+    try:
+        return int(port)
+    except ValueError:
+        pass
+
+    try:
+        return socket.getservbyname(port)
+    except socket.error:
+        raise Abort(_("no port number associated with service '%s'") % port)