comparison hgext/zeroconf/__init__.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents fa2071753dc2
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
35 extensions, 35 extensions,
36 hg, 36 hg,
37 pycompat, 37 pycompat,
38 ui as uimod, 38 ui as uimod,
39 ) 39 )
40 from mercurial.hgweb import ( 40 from mercurial.hgweb import server as servermod
41 server as servermod
42 )
43 41
44 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for 42 # Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
45 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should 43 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
46 # be specifying the version(s) of Mercurial they are tested with, or 44 # be specifying the version(s) of Mercurial they are tested with, or
47 # leave the attribute unspecified. 45 # leave the attribute unspecified.
49 47
50 # publish 48 # publish
51 49
52 server = None 50 server = None
53 localip = None 51 localip = None
52
54 53
55 def getip(): 54 def getip():
56 # finds external-facing interface without sending any packets (Linux) 55 # finds external-facing interface without sending any packets (Linux)
57 try: 56 try:
58 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 57 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
80 return ip 79 return ip
81 except socket.error: 80 except socket.error:
82 pass 81 pass
83 82
84 return dumbip 83 return dumbip
84
85 85
86 def publish(name, desc, path, port): 86 def publish(name, desc, path, port):
87 global server, localip 87 global server, localip
88 if not server: 88 if not server:
89 ip = getip() 89 ip = getip()
96 hostname = socket.gethostname().split(r'.')[0] 96 hostname = socket.gethostname().split(r'.')[0]
97 host = hostname + r".local" 97 host = hostname + r".local"
98 name = r"%s-%s" % (hostname, name) 98 name = r"%s-%s" % (hostname, name)
99 99
100 # advertise to browsers 100 # advertise to browsers
101 svc = Zeroconf.ServiceInfo('_http._tcp.local.', 101 svc = Zeroconf.ServiceInfo(
102 pycompat.bytestr(name + r'._http._tcp.local.'), 102 '_http._tcp.local.',
103 server = host, 103 pycompat.bytestr(name + r'._http._tcp.local.'),
104 port = port, 104 server=host,
105 properties = {'description': desc, 105 port=port,
106 'path': "/" + path}, 106 properties={'description': desc, 'path': "/" + path},
107 address = localip, weight = 0, priority = 0) 107 address=localip,
108 weight=0,
109 priority=0,
110 )
108 server.registerService(svc) 111 server.registerService(svc)
109 112
110 # advertise to Mercurial clients 113 # advertise to Mercurial clients
111 svc = Zeroconf.ServiceInfo('_hg._tcp.local.', 114 svc = Zeroconf.ServiceInfo(
112 pycompat.bytestr(name + r'._hg._tcp.local.'), 115 '_hg._tcp.local.',
113 server = host, 116 pycompat.bytestr(name + r'._hg._tcp.local.'),
114 port = port, 117 server=host,
115 properties = {'description': desc, 118 port=port,
116 'path': "/" + path}, 119 properties={'description': desc, 'path': "/" + path},
117 address = localip, weight = 0, priority = 0) 120 address=localip,
121 weight=0,
122 priority=0,
123 )
118 server.registerService(svc) 124 server.registerService(svc)
125
119 126
120 def zc_create_server(create_server, ui, app): 127 def zc_create_server(create_server, ui, app):
121 httpd = create_server(ui, app) 128 httpd = create_server(ui, app)
122 port = httpd.port 129 port = httpd.port
123 130
144 if not desc: 151 if not desc:
145 desc = name 152 desc = name
146 publish(name, desc, path, port) 153 publish(name, desc, path, port)
147 return httpd 154 return httpd
148 155
156
149 # listen 157 # listen
158
150 159
151 class listener(object): 160 class listener(object):
152 def __init__(self): 161 def __init__(self):
153 self.found = {} 162 self.found = {}
163
154 def removeService(self, server, type, name): 164 def removeService(self, server, type, name):
155 if repr(name) in self.found: 165 if repr(name) in self.found:
156 del self.found[repr(name)] 166 del self.found[repr(name)]
167
157 def addService(self, server, type, name): 168 def addService(self, server, type, name):
158 self.found[repr(name)] = server.getServiceInfo(type, name) 169 self.found[repr(name)] = server.getServiceInfo(type, name)
170
159 171
160 def getzcpaths(): 172 def getzcpaths():
161 ip = getip() 173 ip = getip()
162 if ip.startswith(r'127.'): 174 if ip.startswith(r'127.'):
163 return 175 return
165 l = listener() 177 l = listener()
166 Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l) 178 Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l)
167 time.sleep(1) 179 time.sleep(1)
168 server.close() 180 server.close()
169 for value in l.found.values(): 181 for value in l.found.values():
170 name = value.name[:value.name.index(b'.')] 182 name = value.name[: value.name.index(b'.')]
171 url = r"http://%s:%s%s" % (socket.inet_ntoa(value.address), value.port, 183 url = r"http://%s:%s%s" % (
172 value.properties.get(r"path", r"/")) 184 socket.inet_ntoa(value.address),
185 value.port,
186 value.properties.get(r"path", r"/"),
187 )
173 yield b"zc-" + name, pycompat.bytestr(url) 188 yield b"zc-" + name, pycompat.bytestr(url)
189
174 190
175 def config(orig, self, section, key, *args, **kwargs): 191 def config(orig, self, section, key, *args, **kwargs):
176 if section == "paths" and key.startswith("zc-"): 192 if section == "paths" and key.startswith("zc-"):
177 for name, path in getzcpaths(): 193 for name, path in getzcpaths():
178 if name == key: 194 if name == key:
179 return path 195 return path
180 return orig(self, section, key, *args, **kwargs) 196 return orig(self, section, key, *args, **kwargs)
181 197
198
182 def configitems(orig, self, section, *args, **kwargs): 199 def configitems(orig, self, section, *args, **kwargs):
183 repos = orig(self, section, *args, **kwargs) 200 repos = orig(self, section, *args, **kwargs)
184 if section == "paths": 201 if section == "paths":
185 repos += getzcpaths() 202 repos += getzcpaths()
186 return repos 203 return repos
204
187 205
188 def configsuboptions(orig, self, section, name, *args, **kwargs): 206 def configsuboptions(orig, self, section, name, *args, **kwargs):
189 opt, sub = orig(self, section, name, *args, **kwargs) 207 opt, sub = orig(self, section, name, *args, **kwargs)
190 if section == "paths" and name.startswith("zc-"): 208 if section == "paths" and name.startswith("zc-"):
191 # We have to find the URL in the zeroconf paths. We can't cons up any 209 # We have to find the URL in the zeroconf paths. We can't cons up any
193 for zcname, zcurl in getzcpaths(): 211 for zcname, zcurl in getzcpaths():
194 if zcname == name: 212 if zcname == name:
195 return zcurl, sub 213 return zcurl, sub
196 return opt, sub 214 return opt, sub
197 215
216
198 def defaultdest(orig, source): 217 def defaultdest(orig, source):
199 for name, path in getzcpaths(): 218 for name, path in getzcpaths():
200 if path == source: 219 if path == source:
201 return name.encode(encoding.encoding) 220 return name.encode(encoding.encoding)
202 return orig(source) 221 return orig(source)
222
203 223
204 def cleanupafterdispatch(orig, ui, options, cmd, cmdfunc): 224 def cleanupafterdispatch(orig, ui, options, cmd, cmdfunc):
205 try: 225 try:
206 return orig(ui, options, cmd, cmdfunc) 226 return orig(ui, options, cmd, cmdfunc)
207 finally: 227 finally:
209 # threading Conditions and allow the background threads to exit 229 # threading Conditions and allow the background threads to exit
210 global server 230 global server
211 if server: 231 if server:
212 server.close() 232 server.close()
213 233
234
214 extensions.wrapfunction(dispatch, '_runcommand', cleanupafterdispatch) 235 extensions.wrapfunction(dispatch, '_runcommand', cleanupafterdispatch)
215 236
216 extensions.wrapfunction(uimod.ui, 'config', config) 237 extensions.wrapfunction(uimod.ui, 'config', config)
217 extensions.wrapfunction(uimod.ui, 'configitems', configitems) 238 extensions.wrapfunction(uimod.ui, 'configitems', configitems)
218 extensions.wrapfunction(uimod.ui, 'configsuboptions', configsuboptions) 239 extensions.wrapfunction(uimod.ui, 'configsuboptions', configsuboptions)