comparison 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
comparison
equal deleted inserted replaced
12075:f585c9bb85c1 12076:49463314c24f
15 15
16 from i18n import _ 16 from i18n import _
17 import error, osutil, encoding 17 import error, osutil, encoding
18 import errno, re, shutil, sys, tempfile, traceback 18 import errno, re, shutil, sys, tempfile, traceback
19 import os, stat, time, calendar, textwrap, unicodedata, signal 19 import os, stat, time, calendar, textwrap, unicodedata, signal
20 import imp 20 import imp, socket
21 21
22 # Python compatibility 22 # Python compatibility
23 23
24 def sha1(s): 24 def sha1(s):
25 return _fastsha1(s) 25 return _fastsha1(s)
1412 """ 1412 """
1413 fn = fn or (lambda s: s) 1413 fn = fn or (lambda s: s)
1414 r = re.compile(r'%s(%s)' % (prefix, '|'.join(mapping.keys()))) 1414 r = re.compile(r'%s(%s)' % (prefix, '|'.join(mapping.keys())))
1415 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s) 1415 return r.sub(lambda x: fn(mapping[x.group()[1:]]), s)
1416 1416
1417 def getport(port):
1418 """Return the port for a given network service.
1419
1420 If port is an integer, it's returned as is. If it's a string, it's
1421 looked up using socket.getservbyname(). If there's no matching
1422 service, util.Abort is raised.
1423 """
1424 try:
1425 return int(port)
1426 except ValueError:
1427 pass
1428
1429 try:
1430 return socket.getservbyname(port)
1431 except socket.error:
1432 raise Abort(_("no port number associated with service '%s'") % port)