hgext/zeroconf/__init__.py
author Patrick Mezard <patrick@mezard.eu>
Tue, 08 May 2012 22:43:44 +0200
changeset 16777 058e14da7044
parent 16743 38caf405d010
child 18190 d57879e72e18
permissions -rw-r--r--
graphlog: turn getlogrevs() into a generator This improves the poor "time to first changeset" compared to the original log command. When running: $ hg log -u user log will enumerate the changelog and display matching revisions when they are found. But: $ hg log -G -u user will first find all revisions matching the user then start to display them. Initially, I considered turning revset.match() into a generator. This is doable but requires a fair amount of work. Instead, cmdutil.increasingwindows() is reused to call the revset matcher repeatedly. This has the nice properties of: - Let us reorder the windows after filtering, which is necessary as the matcher can reorder inputs but is an internal detail not a feature. - Let us feed the matcher with windows in changelog order, which is good for performances. - Have a generator designed for log-like commands, returning small windows at first then batching larger ones. I feel that calling the matcher multiple times is correct, at least with the revsets involved in getlogrevs() because they are: - stateless (no limit()) - respecting f(a|b) = f(a) | f(b), though I have no valid argument about that. Known issues compared to log code: - Calling the revset matcher multiple times can be slow when revset functions have to create expensive data structure for filtering. This will be addressed in a followup. - Predicate combinations like "--user foo --user bar" or "--user foo and --branch bar" are inherently slower because all input revision are checked against the first condition, then against the second, and so forth. log would enumerate the input revisions once and check each of them once against all conditions, which is faster. There are solutions but nothing cheap to implement. Some numbers against mozilla repository: first line total * hg log -u rnewman /Users/pmezard/bin/hg-2.2 0.148s 7.293s /Users/pmezard/bin/hgdev 0.132s 5.747s * hg log -u rnewman -u girard /Users/pmezard/bin/hg-2.2 0.146s 7.323s /Users/pmezard/bin/hgdev 0.136s 11.096s * hg log -l 10 /Users/pmezard/bin/hg-2.2 0.137s 0.153s /Users/pmezard/bin/hgdev 0.128s 0.144s * hg log -l 10 -u rnewman /Users/pmezard/bin/hg-2.2 0.146s 0.265s /Users/pmezard/bin/hgdev 0.133s 0.236s * hg log -b GECKO193a2_20100228_RELBRANCH /Users/pmezard/bin/hg-2.2 2.332s 6.618s /Users/pmezard/bin/hgdev 1.972s 5.543s * hg log xulrunner /Users/pmezard/bin/hg-2.2 5.829s 5.958s /Users/pmezard/bin/hgdev 0.194s 6.017s * hg log --follow xulrunner/build.mk /Users/pmezard/bin/hg-2.2 0.353s 0.438s /Users/pmezard/bin/hgdev 0.394s 0.580s * hg log -u girard tools /Users/pmezard/bin/hg-2.2 5.853s 6.012s /Users/pmezard/bin/hgdev 0.195s 6.030s * hg log -b COMM2000_20110314_RELBRANCH --copies /Users/pmezard/bin/hg-2.2 2.231s 6.653s /Users/pmezard/bin/hgdev 1.897s 5.585s * hg log --follow /Users/pmezard/bin/hg-2.2 0.137s 14.140s /Users/pmezard/bin/hgdev 0.381s 44.246s * hg log --follow -r 80000:90000 /Users/pmezard/bin/hg-2.2 0.127s 1.611s /Users/pmezard/bin/hgdev 0.147s 1.847s * hg log --follow -r 90000:80000 /Users/pmezard/bin/hg-2.2 0.130s 1.702s /Users/pmezard/bin/hgdev 0.368s 6.106s * hg log --follow -r 80000:90000 js/src/jsproxy.cpp /Users/pmezard/bin/hg-2.2 0.343s 0.388s /Users/pmezard/bin/hgdev 0.437s 0.631s * hg log --follow -r 90000:80000 js/src/jsproxy.cpp /Users/pmezard/bin/hg-2.2 0.342s 0.389s /Users/pmezard/bin/hgdev 0.442s 0.628s
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
14104
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
    30
from mercurial import ui, hg, encoding, util, dispatch
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
    31
from mercurial import extensions
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    32
from mercurial.hgweb import hgweb_mod
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    33
from mercurial.hgweb import hgwebdir_mod
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    34
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16688
diff changeset
    35
testedwith = 'internal'
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16688
diff changeset
    36
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    37
# publish
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    38
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    39
server = None
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    40
localip = None
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
def getip():
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    43
    # finds external-facing interface without sending any packets (Linux)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    44
    try:
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    45
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    46
        s.connect(('1.0.0.1', 0))
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    47
        ip = s.getsockname()[0]
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    48
        return ip
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16683
diff changeset
    49
    except socket.error:
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    50
        pass
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    51
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    52
    # Generic method, sometimes gives useless results
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    53
    try:
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    54
        dumbip = socket.gethostbyaddr(socket.gethostname())[2][0]
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    55
        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
    56
            return dumbip
10317
192083a3e6fe zeroconf: gethostbyaddr may also fail with socket.herror
Augie Fackler <durin42@gmail.com>
parents: 10263
diff changeset
    57
    except (socket.gaierror, socket.herror):
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    58
        dumbip = '127.0.0.1'
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    59
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    60
    # works elsewhere, but actually sends a packet
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    61
    try:
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    62
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    63
        s.connect(('1.0.0.1', 1))
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    64
        ip = s.getsockname()[0]
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    65
        return ip
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16683
diff changeset
    66
    except socket.error:
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    67
        pass
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    68
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    69
    return dumbip
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    70
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    71
def publish(name, desc, path, port):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    72
    global server, localip
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    73
    if not server:
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    74
        ip = getip()
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    75
        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
    76
            # 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
    77
            return
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    78
        localip = socket.inet_aton(ip)
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    79
        server = Zeroconf.Zeroconf(ip)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    80
7845
c2cd8d772805 zeroconf: advertise repositories with hostname
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7777
diff changeset
    81
    hostname = socket.gethostname().split('.')[0]
c2cd8d772805 zeroconf: advertise repositories with hostname
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7777
diff changeset
    82
    host = hostname + ".local"
c2cd8d772805 zeroconf: advertise repositories with hostname
Alexander Solovyov <piranha@piranha.org.ua>
parents: 7777
diff changeset
    83
    name = "%s-%s" % (hostname, name)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    84
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    85
    # advertise to browsers
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    86
    svc = Zeroconf.ServiceInfo('_http._tcp.local.',
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    87
                               name + '._http._tcp.local.',
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    88
                               server = host,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    89
                               port = port,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    90
                               properties = {'description': desc,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    91
                                             'path': "/" + path},
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    92
                               address = localip, weight = 0, priority = 0)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    93
    server.registerService(svc)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    94
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    95
    # advertise to Mercurial clients
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    96
    svc = Zeroconf.ServiceInfo('_hg._tcp.local.',
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    97
                               name + '._hg._tcp.local.',
7088
58b7b5ef6cd0 zeroconf: advertise a proper hostname for _hg services
Matt Mackall <mpm@selenic.com>
parents: 7087
diff changeset
    98
                               server = host,
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    99
                               port = port,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   100
                               properties = {'description': desc,
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   101
                                             'path': "/" + path},
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   102
                               address = localip, weight = 0, priority = 0)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   103
    server.registerService(svc)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   104
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   105
class hgwebzc(hgweb_mod.hgweb):
11005
c9b4e9431af7 zeroconf: handle string hgweb config args
Matt Mackall <mpm@selenic.com>
parents: 10993
diff changeset
   106
    def __init__(self, repo, name=None, baseui=None):
c9b4e9431af7 zeroconf: handle string hgweb config args
Matt Mackall <mpm@selenic.com>
parents: 10993
diff changeset
   107
        super(hgwebzc, self).__init__(repo, name=name, baseui=baseui)
c9b4e9431af7 zeroconf: handle string hgweb config args
Matt Mackall <mpm@selenic.com>
parents: 10993
diff changeset
   108
        name = self.reponame or os.path.basename(self.repo.root)
9442
080227f584a1 zeroconf: fix hgweb published URLs (issue1819)
Patrick Mezard <pmezard@gmail.com>
parents: 8894
diff changeset
   109
        path = self.repo.ui.config("web", "prefix", "").strip('/')
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   110
        desc = self.repo.ui.config("web", "description", name)
11005
c9b4e9431af7 zeroconf: handle string hgweb config args
Matt Mackall <mpm@selenic.com>
parents: 10993
diff changeset
   111
        publish(name, desc, path,
12076
49463314c24f mail/hgweb: support service names for ports (issue2350)
Brodie Rao <brodie@bitheap.org>
parents: 11504
diff changeset
   112
                util.getport(self.repo.ui.config("web", "port", 8000)))
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   113
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   114
class hgwebdirzc(hgwebdir_mod.hgwebdir):
9442
080227f584a1 zeroconf: fix hgweb published URLs (issue1819)
Patrick Mezard <pmezard@gmail.com>
parents: 8894
diff changeset
   115
    def __init__(self, conf, baseui=None):
10993
c1b43d786889 hgweb: make baseui parameter non-positional
Matt Mackall <mpm@selenic.com>
parents: 10342
diff changeset
   116
        super(hgwebdirzc, self).__init__(conf, baseui=baseui)
9442
080227f584a1 zeroconf: fix hgweb published URLs (issue1819)
Patrick Mezard <pmezard@gmail.com>
parents: 8894
diff changeset
   117
        prefix = self.ui.config("web", "prefix", "").strip('/') + '/'
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   118
        for repo, path in self.repos:
8191
35604226d712 hgweb: kill parentui references
Matt Mackall <mpm@selenic.com>
parents: 8190
diff changeset
   119
            u = self.ui.copy()
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   120
            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
   121
            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
   122
            path = (prefix + repo).strip('/')
9489
cec4b0d3fb02 zeroconf: read actual description for repos in hgwebdir
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9488
diff changeset
   123
            desc = u.config('web', 'description', name)
16683
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 14104
diff changeset
   124
            publish(name, desc, path,
525fdb738975 cleanup: eradicate long lines
Brodie Rao <brodie@sf.io>
parents: 14104
diff changeset
   125
                    util.getport(u.config("web", "port", 8000)))
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   126
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   127
# listen
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   128
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   129
class listener(object):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   130
    def __init__(self):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   131
        self.found = {}
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   132
    def removeService(self, server, type, name):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   133
        if repr(name) in self.found:
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   134
            del self.found[repr(name)]
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   135
    def addService(self, server, type, name):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   136
        self.found[repr(name)] = server.getServiceInfo(type, name)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   137
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   138
def getzcpaths():
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   139
    ip = getip()
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   140
    if ip.startswith('127.'):
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   141
        return
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   142
    server = Zeroconf.Zeroconf(ip)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   143
    l = listener()
7874
d812029cda85 cleanup: drop variables for unused return values
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 7845
diff changeset
   144
    Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   145
    time.sleep(1)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   146
    server.close()
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   147
    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
   148
        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
   149
        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
   150
                                  value.properties.get("path", "/"))
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   151
        yield "zc-" + name, url
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   152
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
   153
def config(orig, self, section, key, default=None, untrusted=False):
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   154
    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
   155
        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
   156
            if name == key:
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   157
                return path
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
   158
    return orig(self, section, key, default, untrusted)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   159
7238
b1a9ad7b464e zeroconf: don't break on hg showconfig
Matt Mackall <mpm@selenic.com>
parents: 7216
diff changeset
   160
def configitems(orig, self, section, untrusted=False):
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   161
    repos = orig(self, section, untrusted)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   162
    if section == "paths":
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   163
        repos += getzcpaths()
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   164
    return repos
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   165
10342
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   166
def defaultdest(orig, source):
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   167
    for name, path in getzcpaths():
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   168
        if path == source:
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   169
            return name.encode(encoding.encoding)
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   170
    return orig(source)
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   171
14104
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   172
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
   173
    try:
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   174
        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
   175
    finally:
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   176
        # 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
   177
        # 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
   178
        global server
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   179
        if server:
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   180
            server.close()
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   181
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   182
extensions.wrapfunction(dispatch, '_runcommand', cleanupafterdispatch)
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   183
7216
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
   184
extensions.wrapfunction(ui.ui, 'config', config)
292fb2ad2846 extensions: use new wrapper functions
Matt Mackall <mpm@selenic.com>
parents: 7088
diff changeset
   185
extensions.wrapfunction(ui.ui, 'configitems', configitems)
10342
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   186
extensions.wrapfunction(hg, 'defaultdest', defaultdest)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   187
hgweb_mod.hgweb = hgwebzc
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   188
hgwebdir_mod.hgwebdir = hgwebdirzc