hgext/zeroconf/__init__.py
author Yuya Nishihara <yuya@tcha.org>
Wed, 10 Feb 2016 22:53:17 +0900
branchstable
changeset 28038 72f2a19c5f88
parent 27910 d2c5ad3deccb
child 28250 6d0d11731e1c
permissions -rw-r--r--
zeroconf: forward all arguments passed to ui.configitems() wrapper f43988e5954c added 'ignoresub' argument to ui.configitems(), but zeroconf wrapper wasn't updated. It caused the following crash: Traceback (most recent call last): File "bin/hg", line 43, in <module> mercurial.dispatch.run() File "lib/python/mercurial/dispatch.py", line 54, in run sys.exit((dispatch(request(sys.argv[1:])) or 0) & 255) File "lib/python/mercurial/dispatch.py", line 120, in dispatch ret = _runcatch(req) File "lib/python/mercurial/dispatch.py", line 191, in _runcatch return _dispatch(req) File "lib/python/mercurial/dispatch.py", line 924, in _dispatch cmdpats, cmdoptions) File "lib/python/mercurial/dispatch.py", line 681, in runcommand ret = _runcommand(ui, options, cmd, d) File "lib/python/mercurial/extensions.py", line 195, in closure return func(*(args + a), **kw) File "lib/python/hgext/zeroconf/__init__.py", line 180, in cleanupafterdispatch return orig(ui, options, cmd, cmdfunc) File "lib/python/mercurial/dispatch.py", line 1055, in _runcommand return checkargs() File "lib/python/mercurial/dispatch.py", line 1015, in checkargs return cmdfunc() File "lib/python/mercurial/dispatch.py", line 921, in <lambda> d = lambda: util.checksignature(func)(ui, *args, **cmdoptions) File "lib/python/mercurial/util.py", line 991, in check return func(*args, **kwargs) File "lib/python/mercurial/commands.py", line 5405, in paths pathitems = sorted(ui.paths.iteritems()) File "lib/python/mercurial/util.py", line 723, in __get__ result = self.func(obj) File "lib/python/mercurial/ui.py", line 619, in paths return paths(self) File "lib/python/mercurial/ui.py", line 1099, in __init__ for name, loc in ui.configitems('paths', ignoresub=True): File "lib/python/mercurial/extensions.py", line 195, in closure return func(*(args + a), **kw) TypeError: configitems() got an unexpected keyword argument 'ignoresub' We have no test coverage for zeroconf, so I've added a minimal test that could reproduce this problem.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     1
# zeroconf.py - zeroconf support for Mercurial
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     2
#
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     3
# Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     4
#
8225
46293a0c7e9f updated license to be explicit about GPL version 2
Martin Geisler <mg@lazybytes.net>
parents: 8191
diff changeset
     5
# This software may be used and distributed according to the terms of the
10263
25e572394f5c Update license to GPLv2+
Matt Mackall <mpm@selenic.com>
parents: 9489
diff changeset
     6
# GNU General Public License version 2 or any later version.
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
     7
8894
868670dbc237 extensions: improve the consistency of synopses
Cédric Duval <cedricduval@free.fr>
parents: 8866
diff changeset
     8
'''discover and advertise repositories on the local network
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
     9
11504
6bfb76cb8873 zeroconf: small fixes in docstring
Martin Geisler <mg@lazybytes.net>
parents: 11340
diff changeset
    10
Zeroconf-enabled repositories will be announced in a network without
8003
14f27921932a zeroconf: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7874
diff changeset
    11
the need to configure a server or a service. They can be discovered
14f27921932a zeroconf: word-wrap help texts at 70 characters
Martin Geisler <mg@daimi.au.dk>
parents: 7874
diff changeset
    12
without knowing their actual IP address.
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    13
11504
6bfb76cb8873 zeroconf: small fixes in docstring
Martin Geisler <mg@lazybytes.net>
parents: 11340
diff changeset
    14
To allow other people to discover your repository using run
6bfb76cb8873 zeroconf: small fixes in docstring
Martin Geisler <mg@lazybytes.net>
parents: 11340
diff changeset
    15
:hg:`serve` in your repository::
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    16
9218
d3db87d68337 zeroconf: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9078
diff changeset
    17
  $ cd test
d3db87d68337 zeroconf: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9078
diff changeset
    18
  $ hg serve
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    19
11504
6bfb76cb8873 zeroconf: small fixes in docstring
Martin Geisler <mg@lazybytes.net>
parents: 11340
diff changeset
    20
You can discover Zeroconf-enabled repositories by running
6bfb76cb8873 zeroconf: small fixes in docstring
Martin Geisler <mg@lazybytes.net>
parents: 11340
diff changeset
    21
:hg:`paths`::
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    22
9218
d3db87d68337 zeroconf: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9078
diff changeset
    23
  $ hg paths
d3db87d68337 zeroconf: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9078
diff changeset
    24
  zc-test = http://example.com:8000/test
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    25
'''
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    26
11340
938fefb57db5 hgext/zeroconf/__init__.py: Separate relative and absolute imports.
Renato Cunha <renatoc@gmail.com>
parents: 11005
diff changeset
    27
import socket, time, os
938fefb57db5 hgext/zeroconf/__init__.py: Separate relative and absolute imports.
Renato Cunha <renatoc@gmail.com>
parents: 11005
diff changeset
    28
938fefb57db5 hgext/zeroconf/__init__.py: Separate relative and absolute imports.
Renato Cunha <renatoc@gmail.com>
parents: 11005
diff changeset
    29
import Zeroconf
18190
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
    30
from mercurial import ui, hg, encoding, dispatch
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
    31
from mercurial import extensions
18190
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
    32
from mercurial.hgweb import server as servermod
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    33
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 18190
diff changeset
    34
# Note for extension authors: ONLY specify testedwith = 'internal' for
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 18190
diff changeset
    35
# extensions which SHIP WITH MERCURIAL. Non-mainline extensions should
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 18190
diff changeset
    36
# be specifying the version(s) of Mercurial they are tested with, or
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 18190
diff changeset
    37
# leave the attribute unspecified.
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16688
diff changeset
    38
testedwith = 'internal'
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16688
diff changeset
    39
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    40
# publish
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    41
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    42
server = None
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    43
localip = None
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    44
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    45
def getip():
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    46
    # finds external-facing interface without sending any packets (Linux)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    47
    try:
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    48
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    49
        s.connect(('1.0.0.1', 0))
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    50
        ip = s.getsockname()[0]
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    51
        return ip
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16683
diff changeset
    52
    except socket.error:
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    53
        pass
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    54
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    55
    # Generic method, sometimes gives useless results
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    56
    try:
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    57
        dumbip = socket.gethostbyaddr(socket.gethostname())[2][0]
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    58
        if not dumbip.startswith('127.') and ':' not in dumbip:
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    59
            return dumbip
10317
192083a3e6fe zeroconf: gethostbyaddr may also fail with socket.herror
Augie Fackler <durin42@gmail.com>
parents: 10263
diff changeset
    60
    except (socket.gaierror, socket.herror):
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    61
        dumbip = '127.0.0.1'
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    62
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    63
    # works elsewhere, but actually sends a packet
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    64
    try:
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    65
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    66
        s.connect(('1.0.0.1', 1))
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    67
        ip = s.getsockname()[0]
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    68
        return ip
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16683
diff changeset
    69
    except socket.error:
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    70
        pass
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    71
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    72
    return dumbip
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    73
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    74
def publish(name, desc, path, port):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    75
    global server, localip
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    76
    if not server:
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    77
        ip = getip()
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    78
        if ip.startswith('127.'):
7295
66d0fc108044 zeroconf: Don't break serve if no internet connection is present.
Augie Fackler <durin42@gmail.com>
parents: 7282
diff changeset
    79
            # if we have no internet connection, this can happen.
66d0fc108044 zeroconf: Don't break serve if no internet connection is present.
Augie Fackler <durin42@gmail.com>
parents: 7282
diff changeset
    80
            return
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    81
        localip = socket.inet_aton(ip)
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    82
        server = Zeroconf.Zeroconf(ip)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    83
7845
c2cd8d772805 zeroconf: advertise repositories with hostname
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7777
diff changeset
    84
    hostname = socket.gethostname().split('.')[0]
c2cd8d772805 zeroconf: advertise repositories with hostname
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7777
diff changeset
    85
    host = hostname + ".local"
c2cd8d772805 zeroconf: advertise repositories with hostname
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7777
diff changeset
    86
    name = "%s-%s" % (hostname, name)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    87
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    88
    # advertise to browsers
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    89
    svc = Zeroconf.ServiceInfo('_http._tcp.local.',
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    90
                               name + '._http._tcp.local.',
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    91
                               server = host,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    92
                               port = port,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    93
                               properties = {'description': desc,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    94
                                             'path': "/" + path},
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    95
                               address = localip, weight = 0, priority = 0)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    96
    server.registerService(svc)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    97
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    98
    # advertise to Mercurial clients
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    99
    svc = Zeroconf.ServiceInfo('_hg._tcp.local.',
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   100
                               name + '._hg._tcp.local.',
7088
58b7b5ef6cd0 zeroconf: advertise a proper hostname for _hg services
Matt Mackall <mpm@selenic.com>
parents: 7087
diff changeset
   101
                               server = host,
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   102
                               port = port,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   103
                               properties = {'description': desc,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   104
                                             'path': "/" + path},
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   105
                               address = localip, weight = 0, priority = 0)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   106
    server.registerService(svc)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   107
18190
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   108
def zc_create_server(create_server, ui, app):
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   109
    httpd = create_server(ui, app)
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   110
    port = httpd.port
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   111
18190
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   112
    try:
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   113
        repos = app.repos
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   114
    except AttributeError:
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   115
        # single repo
27910
d2c5ad3deccb zeroconf: access repo on hgweb_mod properly (issue5036)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   116
        with app._obtainrepo() as repo:
d2c5ad3deccb zeroconf: access repo on hgweb_mod properly (issue5036)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   117
            name = app.reponame or os.path.basename(repo.root)
d2c5ad3deccb zeroconf: access repo on hgweb_mod properly (issue5036)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   118
            path = repo.ui.config("web", "prefix", "").strip('/')
d2c5ad3deccb zeroconf: access repo on hgweb_mod properly (issue5036)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   119
            desc = repo.ui.config("web", "description", name)
18190
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   120
        publish(name, desc, path, port)
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   121
    else:
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   122
        # webdir
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   123
        prefix = app.ui.config("web", "prefix", "").strip('/') + '/'
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   124
        for repo, path in repos:
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   125
            u = app.ui.copy()
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   126
            u.readconfig(os.path.join(path, '.hg', 'hgrc'))
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   127
            name = os.path.basename(repo)
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   128
            path = (prefix + repo).strip('/')
9489
cec4b0d3fb02 zeroconf: read actual description for repos in hgwebdir
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9488
diff changeset
   129
            desc = u.config('web', 'description', name)
18190
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   130
            publish(name, desc, path, port)
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   131
    return httpd
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   132
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   133
# listen
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   134
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   135
class listener(object):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   136
    def __init__(self):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   137
        self.found = {}
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   138
    def removeService(self, server, type, name):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   139
        if repr(name) in self.found:
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   140
            del self.found[repr(name)]
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   141
    def addService(self, server, type, name):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   142
        self.found[repr(name)] = server.getServiceInfo(type, name)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   143
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   144
def getzcpaths():
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   145
    ip = getip()
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   146
    if ip.startswith('127.'):
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   147
        return
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   148
    server = Zeroconf.Zeroconf(ip)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   149
    l = listener()
7874
d812029cda85 cleanup: drop variables for unused return values
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 7845
diff changeset
   150
    Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   151
    time.sleep(1)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   152
    server.close()
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   153
    for value in l.found.values():
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   154
        name = value.name[:value.name.index('.')]
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   155
        url = "http://%s:%s%s" % (socket.inet_ntoa(value.address), value.port,
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   156
                                  value.properties.get("path", "/"))
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   157
        yield "zc-" + name, url
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   158
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
   159
def config(orig, self, section, key, default=None, untrusted=False):
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   160
    if section == "paths" and key.startswith("zc-"):
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   161
        for name, path in getzcpaths():
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   162
            if name == key:
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   163
                return path
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
   164
    return orig(self, section, key, default, untrusted)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   165
28038
72f2a19c5f88 zeroconf: forward all arguments passed to ui.configitems() wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 27910
diff changeset
   166
def configitems(orig, self, section, *args, **kwargs):
72f2a19c5f88 zeroconf: forward all arguments passed to ui.configitems() wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 27910
diff changeset
   167
    repos = orig(self, section, *args, **kwargs)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   168
    if section == "paths":
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   169
        repos += getzcpaths()
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   170
    return repos
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   171
10342
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   172
def defaultdest(orig, source):
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   173
    for name, path in getzcpaths():
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   174
        if path == source:
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   175
            return name.encode(encoding.encoding)
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   176
    return orig(source)
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   177
14104
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   178
def cleanupafterdispatch(orig, ui, options, cmd, cmdfunc):
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   179
    try:
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   180
        return orig(ui, options, cmd, cmdfunc)
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   181
    finally:
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   182
        # we need to call close() on the server to notify() the various
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   183
        # threading Conditions and allow the background threads to exit
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   184
        global server
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   185
        if server:
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   186
            server.close()
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   187
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   188
extensions.wrapfunction(dispatch, '_runcommand', cleanupafterdispatch)
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   189
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
   190
extensions.wrapfunction(ui.ui, 'config', config)
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
   191
extensions.wrapfunction(ui.ui, 'configitems', configitems)
10342
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   192
extensions.wrapfunction(hg, 'defaultdest', defaultdest)
18190
d57879e72e18 zeroconf: use port from server instead of picking port from config (issue3746)
Benoit Boissinot <benoit.boissinot@ens-lyon.org>
parents: 16743
diff changeset
   193
extensions.wrapfunction(servermod, 'create_server', zc_create_server)