hgext/zeroconf/__init__.py
changeset 43077 687b865b95ad
parent 43076 2372284d9457
child 43500 6337b10c46bc
equal deleted inserted replaced
43076:2372284d9457 43077:687b865b95ad
    41 
    41 
    42 # 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
    43 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
    43 # extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
    44 # 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
    45 # leave the attribute unspecified.
    45 # leave the attribute unspecified.
    46 testedwith = 'ships-with-hg-core'
    46 testedwith = b'ships-with-hg-core'
    47 
    47 
    48 # publish
    48 # publish
    49 
    49 
    50 server = None
    50 server = None
    51 localip = None
    51 localip = None
    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(
   101     svc = Zeroconf.ServiceInfo(
   102         '_http._tcp.local.',
   102         b'_http._tcp.local.',
   103         pycompat.bytestr(name + r'._http._tcp.local.'),
   103         pycompat.bytestr(name + r'._http._tcp.local.'),
   104         server=host,
   104         server=host,
   105         port=port,
   105         port=port,
   106         properties={'description': desc, 'path': "/" + path},
   106         properties={b'description': desc, b'path': b"/" + path},
   107         address=localip,
   107         address=localip,
   108         weight=0,
   108         weight=0,
   109         priority=0,
   109         priority=0,
   110     )
   110     )
   111     server.registerService(svc)
   111     server.registerService(svc)
   112 
   112 
   113     # advertise to Mercurial clients
   113     # advertise to Mercurial clients
   114     svc = Zeroconf.ServiceInfo(
   114     svc = Zeroconf.ServiceInfo(
   115         '_hg._tcp.local.',
   115         b'_hg._tcp.local.',
   116         pycompat.bytestr(name + r'._hg._tcp.local.'),
   116         pycompat.bytestr(name + r'._hg._tcp.local.'),
   117         server=host,
   117         server=host,
   118         port=port,
   118         port=port,
   119         properties={'description': desc, 'path': "/" + path},
   119         properties={b'description': desc, b'path': b"/" + path},
   120         address=localip,
   120         address=localip,
   121         weight=0,
   121         weight=0,
   122         priority=0,
   122         priority=0,
   123     )
   123     )
   124     server.registerService(svc)
   124     server.registerService(svc)
   132         repos = app.repos
   132         repos = app.repos
   133     except AttributeError:
   133     except AttributeError:
   134         # single repo
   134         # single repo
   135         with app._obtainrepo() as repo:
   135         with app._obtainrepo() as repo:
   136             name = app.reponame or os.path.basename(repo.root)
   136             name = app.reponame or os.path.basename(repo.root)
   137             path = repo.ui.config("web", "prefix", "").strip('/')
   137             path = repo.ui.config(b"web", b"prefix", b"").strip(b'/')
   138             desc = repo.ui.config("web", "description")
   138             desc = repo.ui.config(b"web", b"description")
   139             if not desc:
   139             if not desc:
   140                 desc = name
   140                 desc = name
   141         publish(name, desc, path, port)
   141         publish(name, desc, path, port)
   142     else:
   142     else:
   143         # webdir
   143         # webdir
   144         prefix = app.ui.config("web", "prefix", "").strip('/') + '/'
   144         prefix = app.ui.config(b"web", b"prefix", b"").strip(b'/') + b'/'
   145         for repo, path in repos:
   145         for repo, path in repos:
   146             u = app.ui.copy()
   146             u = app.ui.copy()
   147             u.readconfig(os.path.join(path, '.hg', 'hgrc'))
   147             u.readconfig(os.path.join(path, b'.hg', b'hgrc'))
   148             name = os.path.basename(repo)
   148             name = os.path.basename(repo)
   149             path = (prefix + repo).strip('/')
   149             path = (prefix + repo).strip(b'/')
   150             desc = u.config('web', 'description')
   150             desc = u.config(b'web', b'description')
   151             if not desc:
   151             if not desc:
   152                 desc = name
   152                 desc = name
   153             publish(name, desc, path, port)
   153             publish(name, desc, path, port)
   154     return httpd
   154     return httpd
   155 
   155 
   173     ip = getip()
   173     ip = getip()
   174     if ip.startswith(r'127.'):
   174     if ip.startswith(r'127.'):
   175         return
   175         return
   176     server = Zeroconf.Zeroconf(ip)
   176     server = Zeroconf.Zeroconf(ip)
   177     l = listener()
   177     l = listener()
   178     Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l)
   178     Zeroconf.ServiceBrowser(server, b"_hg._tcp.local.", l)
   179     time.sleep(1)
   179     time.sleep(1)
   180     server.close()
   180     server.close()
   181     for value in l.found.values():
   181     for value in l.found.values():
   182         name = value.name[: value.name.index(b'.')]
   182         name = value.name[: value.name.index(b'.')]
   183         url = r"http://%s:%s%s" % (
   183         url = r"http://%s:%s%s" % (
   187         )
   187         )
   188         yield b"zc-" + name, pycompat.bytestr(url)
   188         yield b"zc-" + name, pycompat.bytestr(url)
   189 
   189 
   190 
   190 
   191 def config(orig, self, section, key, *args, **kwargs):
   191 def config(orig, self, section, key, *args, **kwargs):
   192     if section == "paths" and key.startswith("zc-"):
   192     if section == b"paths" and key.startswith(b"zc-"):
   193         for name, path in getzcpaths():
   193         for name, path in getzcpaths():
   194             if name == key:
   194             if name == key:
   195                 return path
   195                 return path
   196     return orig(self, section, key, *args, **kwargs)
   196     return orig(self, section, key, *args, **kwargs)
   197 
   197 
   198 
   198 
   199 def configitems(orig, self, section, *args, **kwargs):
   199 def configitems(orig, self, section, *args, **kwargs):
   200     repos = orig(self, section, *args, **kwargs)
   200     repos = orig(self, section, *args, **kwargs)
   201     if section == "paths":
   201     if section == b"paths":
   202         repos += getzcpaths()
   202         repos += getzcpaths()
   203     return repos
   203     return repos
   204 
   204 
   205 
   205 
   206 def configsuboptions(orig, self, section, name, *args, **kwargs):
   206 def configsuboptions(orig, self, section, name, *args, **kwargs):
   207     opt, sub = orig(self, section, name, *args, **kwargs)
   207     opt, sub = orig(self, section, name, *args, **kwargs)
   208     if section == "paths" and name.startswith("zc-"):
   208     if section == b"paths" and name.startswith(b"zc-"):
   209         # 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
   210         # suboptions, so we use any that we found in the original config.
   210         # suboptions, so we use any that we found in the original config.
   211         for zcname, zcurl in getzcpaths():
   211         for zcname, zcurl in getzcpaths():
   212             if zcname == name:
   212             if zcname == name:
   213                 return zcurl, sub
   213                 return zcurl, sub
   230         global server
   230         global server
   231         if server:
   231         if server:
   232             server.close()
   232             server.close()
   233 
   233 
   234 
   234 
   235 extensions.wrapfunction(dispatch, '_runcommand', cleanupafterdispatch)
   235 extensions.wrapfunction(dispatch, b'_runcommand', cleanupafterdispatch)
   236 
   236 
   237 extensions.wrapfunction(uimod.ui, 'config', config)
   237 extensions.wrapfunction(uimod.ui, b'config', config)
   238 extensions.wrapfunction(uimod.ui, 'configitems', configitems)
   238 extensions.wrapfunction(uimod.ui, b'configitems', configitems)
   239 extensions.wrapfunction(uimod.ui, 'configsuboptions', configsuboptions)
   239 extensions.wrapfunction(uimod.ui, b'configsuboptions', configsuboptions)
   240 extensions.wrapfunction(hg, 'defaultdest', defaultdest)
   240 extensions.wrapfunction(hg, b'defaultdest', defaultdest)
   241 extensions.wrapfunction(servermod, 'create_server', zc_create_server)
   241 extensions.wrapfunction(servermod, b'create_server', zc_create_server)