##// END OF EJS Templates
zeroconf: port to Python 3...
Gregory Szorc -
r42752:fa207175 default
parent child Browse files
Show More
@@ -34,6 +34,7 b' from mercurial import ('
34 encoding,
34 encoding,
35 extensions,
35 extensions,
36 hg,
36 hg,
37 pycompat,
37 ui as uimod,
38 ui as uimod,
38 )
39 )
39 from mercurial.hgweb import (
40 from mercurial.hgweb import (
@@ -55,7 +56,7 b' def getip():'
55 # finds external-facing interface without sending any packets (Linux)
56 # finds external-facing interface without sending any packets (Linux)
56 try:
57 try:
57 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
58 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
58 s.connect(('1.0.0.1', 0))
59 s.connect((r'1.0.0.1', 0))
59 ip = s.getsockname()[0]
60 ip = s.getsockname()[0]
60 return ip
61 return ip
61 except socket.error:
62 except socket.error:
@@ -64,17 +65,17 b' def getip():'
64 # Generic method, sometimes gives useless results
65 # Generic method, sometimes gives useless results
65 try:
66 try:
66 dumbip = socket.gethostbyaddr(socket.gethostname())[2][0]
67 dumbip = socket.gethostbyaddr(socket.gethostname())[2][0]
67 if ':' in dumbip:
68 if r':' in dumbip:
68 dumbip = '127.0.0.1'
69 dumbip = r'127.0.0.1'
69 if not dumbip.startswith('127.'):
70 if not dumbip.startswith(r'127.'):
70 return dumbip
71 return dumbip
71 except (socket.gaierror, socket.herror):
72 except (socket.gaierror, socket.herror):
72 dumbip = '127.0.0.1'
73 dumbip = r'127.0.0.1'
73
74
74 # works elsewhere, but actually sends a packet
75 # works elsewhere, but actually sends a packet
75 try:
76 try:
76 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
77 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
77 s.connect(('1.0.0.1', 1))
78 s.connect((r'1.0.0.1', 1))
78 ip = s.getsockname()[0]
79 ip = s.getsockname()[0]
79 return ip
80 return ip
80 except socket.error:
81 except socket.error:
@@ -86,19 +87,19 b' def publish(name, desc, path, port):'
86 global server, localip
87 global server, localip
87 if not server:
88 if not server:
88 ip = getip()
89 ip = getip()
89 if ip.startswith('127.'):
90 if ip.startswith(r'127.'):
90 # if we have no internet connection, this can happen.
91 # if we have no internet connection, this can happen.
91 return
92 return
92 localip = socket.inet_aton(ip)
93 localip = socket.inet_aton(ip)
93 server = Zeroconf.Zeroconf(ip)
94 server = Zeroconf.Zeroconf(ip)
94
95
95 hostname = socket.gethostname().split('.')[0]
96 hostname = socket.gethostname().split(r'.')[0]
96 host = hostname + ".local"
97 host = hostname + r".local"
97 name = "%s-%s" % (hostname, name)
98 name = r"%s-%s" % (hostname, name)
98
99
99 # advertise to browsers
100 # advertise to browsers
100 svc = Zeroconf.ServiceInfo('_http._tcp.local.',
101 svc = Zeroconf.ServiceInfo('_http._tcp.local.',
101 name + '._http._tcp.local.',
102 pycompat.bytestr(name + r'._http._tcp.local.'),
102 server = host,
103 server = host,
103 port = port,
104 port = port,
104 properties = {'description': desc,
105 properties = {'description': desc,
@@ -108,7 +109,7 b' def publish(name, desc, path, port):'
108
109
109 # advertise to Mercurial clients
110 # advertise to Mercurial clients
110 svc = Zeroconf.ServiceInfo('_hg._tcp.local.',
111 svc = Zeroconf.ServiceInfo('_hg._tcp.local.',
111 name + '._hg._tcp.local.',
112 pycompat.bytestr(name + r'._hg._tcp.local.'),
112 server = host,
113 server = host,
113 port = port,
114 port = port,
114 properties = {'description': desc,
115 properties = {'description': desc,
@@ -158,7 +159,7 b' class listener(object):'
158
159
159 def getzcpaths():
160 def getzcpaths():
160 ip = getip()
161 ip = getip()
161 if ip.startswith('127.'):
162 if ip.startswith(r'127.'):
162 return
163 return
163 server = Zeroconf.Zeroconf(ip)
164 server = Zeroconf.Zeroconf(ip)
164 l = listener()
165 l = listener()
@@ -166,10 +167,10 b' def getzcpaths():'
166 time.sleep(1)
167 time.sleep(1)
167 server.close()
168 server.close()
168 for value in l.found.values():
169 for value in l.found.values():
169 name = value.name[:value.name.index('.')]
170 name = value.name[:value.name.index(b'.')]
170 url = "http://%s:%s%s" % (socket.inet_ntoa(value.address), value.port,
171 url = r"http://%s:%s%s" % (socket.inet_ntoa(value.address), value.port,
171 value.properties.get("path", "/"))
172 value.properties.get(r"path", r"/"))
172 yield "zc-" + name, url
173 yield b"zc-" + name, pycompat.bytestr(url)
173
174
174 def config(orig, self, section, key, *args, **kwargs):
175 def config(orig, self, section, key, *args, **kwargs):
175 if section == "paths" and key.startswith("zc-"):
176 if section == "paths" and key.startswith("zc-"):
General Comments 0
You need to be logged in to leave comments. Login now