hgext/zeroconf/__init__.py
author Joerg Sonnenberger <joerg@bec.de>
Fri, 30 Apr 2021 02:11:58 +0200
changeset 47043 12450fbea288
parent 46819 d4ba4d51f85f
child 48875 6000f5b25c9b
permissions -rw-r--r--
manifests: push down expected node length into the parser This strictly enforces the node length in the manifest lines according to what the repository expects. One test case moves large hash testing into the non-treemanifest part as treemanifests don't provide an interface for overriding just the node length for now. Differential Revision: https://phab.mercurial-scm.org/D10533
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
#
46819
d4ba4d51f85f contributor: change mentions of mpm to olivia
Raphaël Gomès <rgomes@octobus.net>
parents: 44246
diff changeset
     3
# Copyright 2005-2007 Olivia Mackall <olivia@selenic.com>
7071
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.
8894
868670dbc237 extensions: improve the consistency of synopses
Cédric Duval <cedricduval@free.fr>
parents: 8866
diff changeset
     7
'''discover and advertise repositories on the local network
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
     8
43500
6337b10c46bc zeroconf: improve the extension's documentation
Vernon Tang <vt@foilhead.net>
parents: 43077
diff changeset
     9
The zeroconf extension will advertise :hg:`serve` instances over
6337b10c46bc zeroconf: improve the extension's documentation
Vernon Tang <vt@foilhead.net>
parents: 43077
diff changeset
    10
DNS-SD so that they can be discovered using the :hg:`paths` command
6337b10c46bc zeroconf: improve the extension's documentation
Vernon Tang <vt@foilhead.net>
parents: 43077
diff changeset
    11
without knowing the server's IP address.
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    12
11504
6bfb76cb8873 zeroconf: small fixes in docstring
Martin Geisler <mg@lazybytes.net>
parents: 11340
diff changeset
    13
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
    14
:hg:`serve` in your repository::
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    15
9218
d3db87d68337 zeroconf: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9078
diff changeset
    16
  $ cd test
d3db87d68337 zeroconf: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9078
diff changeset
    17
  $ hg serve
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    18
11504
6bfb76cb8873 zeroconf: small fixes in docstring
Martin Geisler <mg@lazybytes.net>
parents: 11340
diff changeset
    19
You can discover Zeroconf-enabled repositories by running
6bfb76cb8873 zeroconf: small fixes in docstring
Martin Geisler <mg@lazybytes.net>
parents: 11340
diff changeset
    20
:hg:`paths`::
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    21
9218
d3db87d68337 zeroconf: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9078
diff changeset
    22
  $ hg paths
d3db87d68337 zeroconf: use reST syntax for literal blocks
Martin Geisler <mg@lazybytes.net>
parents: 9078
diff changeset
    23
  zc-test = http://example.com:8000/test
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    24
'''
28296
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    25
from __future__ import absolute_import
7606
e86ca711544d zeroconf: add extension documentation
David Soria Parra <dsp@php.net>
parents: 7295
diff changeset
    26
28296
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    27
import os
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    28
import socket
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    29
import time
11340
938fefb57db5 hgext/zeroconf/__init__.py: Separate relative and absolute imports.
Renato Cunha <renatoc@gmail.com>
parents: 11005
diff changeset
    30
28296
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    31
from . import Zeroconf
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    32
from mercurial import (
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    33
    dispatch,
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    34
    encoding,
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    35
    extensions,
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    36
    hg,
42549
fa2071753dc2 zeroconf: port to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34238
diff changeset
    37
    pycompat,
44246
eecc005229ff config: also respect HGRCSKIPREPO in the zeroconf extension
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43506
diff changeset
    38
    rcutil,
28308
4de74eda1d65 zeroconf: import ui as uimod per test-check-module-imports
Augie Fackler <augie@google.com>
parents: 28296
diff changeset
    39
    ui as uimod,
28296
a73394e7b47c zeroconf: use absolute_import
timeless <timeless@mozdev.org>
parents: 28250
diff changeset
    40
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
    41
from mercurial.hgweb import server as servermod
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    42
29841
d5883fd055c6 extensions: change magic "shipped with hg" string
Augie Fackler <augie@google.com>
parents: 28308
diff changeset
    43
# Note for extension authors: ONLY specify testedwith = 'ships-with-hg-core' for
25186
80c5b2666a96 extensions: document that `testedwith = 'internal'` is special
Augie Fackler <augie@google.com>
parents: 18190
diff changeset
    44
# 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
    45
# 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
    46
# leave the attribute unspecified.
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    47
testedwith = b'ships-with-hg-core'
16743
38caf405d010 hgext: mark all first-party extensions as such
Augie Fackler <raf@durin42.com>
parents: 16688
diff changeset
    48
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    49
# publish
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    50
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    51
server = None
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    52
localip = None
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    53
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
    54
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    55
def getip():
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    56
    # finds external-facing interface without sending any packets (Linux)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    57
    try:
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    58
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    59
        s.connect(('1.0.0.1', 0))
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    60
        ip = s.getsockname()[0]
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    61
        return ip
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16683
diff changeset
    62
    except socket.error:
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    63
        pass
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    64
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    65
    # Generic method, sometimes gives useless results
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    66
    try:
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    67
        dumbip = socket.gethostbyaddr(socket.gethostname())[2][0]
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    68
        if ':' in dumbip:
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    69
            dumbip = '127.0.0.1'
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    70
        if not dumbip.startswith('127.'):
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    71
            return dumbip
10317
192083a3e6fe zeroconf: gethostbyaddr may also fail with socket.herror
Augie Fackler <durin42@gmail.com>
parents: 10263
diff changeset
    72
    except (socket.gaierror, socket.herror):
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    73
        dumbip = '127.0.0.1'
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    74
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    75
    # works elsewhere, but actually sends a packet
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    76
    try:
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    77
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    78
        s.connect(('1.0.0.1', 1))
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    79
        ip = s.getsockname()[0]
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    80
        return ip
16688
cfb6682961b8 cleanup: replace naked excepts with more specific ones
Brodie Rao <brodie@sf.io>
parents: 16683
diff changeset
    81
    except socket.error:
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    82
        pass
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    83
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    84
    return dumbip
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    85
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
    86
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    87
def publish(name, desc, path, port):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    88
    global server, localip
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    89
    if not server:
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    90
        ip = getip()
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    91
        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
    92
            # 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
    93
            return
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    94
        localip = socket.inet_aton(ip)
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
    95
        server = Zeroconf.Zeroconf(ip)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
    96
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    97
    hostname = socket.gethostname().split('.')[0]
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
    98
    host = hostname + ".local"
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43500
diff changeset
    99
    name = "%s-%s" % (hostname, name)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   100
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   101
    # advertise to browsers
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   102
    svc = Zeroconf.ServiceInfo(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   103
        b'_http._tcp.local.',
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   104
        pycompat.bytestr(name + '._http._tcp.local.'),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   105
        server=host,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   106
        port=port,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   107
        properties={b'description': desc, b'path': b"/" + path},
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   108
        address=localip,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   109
        weight=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   110
        priority=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   111
    )
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   112
    server.registerService(svc)
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
    # advertise to Mercurial clients
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   115
    svc = Zeroconf.ServiceInfo(
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   116
        b'_hg._tcp.local.',
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   117
        pycompat.bytestr(name + '._hg._tcp.local.'),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   118
        server=host,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   119
        port=port,
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   120
        properties={b'description': desc, b'path': b"/" + path},
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   121
        address=localip,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   122
        weight=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   123
        priority=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   124
    )
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   125
    server.registerService(svc)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   126
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   127
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
   128
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
   129
    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
   130
    port = httpd.port
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   131
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
   132
    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
   133
        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
   134
    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
   135
        # single repo
27910
d2c5ad3deccb zeroconf: access repo on hgweb_mod properly (issue5036)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   136
        with app._obtainrepo() as repo:
d2c5ad3deccb zeroconf: access repo on hgweb_mod properly (issue5036)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25186
diff changeset
   137
            name = app.reponame or os.path.basename(repo.root)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   138
            path = repo.ui.config(b"web", b"prefix", b"").strip(b'/')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   139
            desc = repo.ui.config(b"web", b"description")
34238
a6c18628dff1 configitems: register the 'web.description' config
Boris Feld <boris.feld@octobus.net>
parents: 33178
diff changeset
   140
            if not desc:
a6c18628dff1 configitems: register the 'web.description' config
Boris Feld <boris.feld@octobus.net>
parents: 33178
diff changeset
   141
                desc = 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
   142
        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
   143
    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
   144
        # webdir
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   145
        prefix = app.ui.config(b"web", b"prefix", b"").strip(b'/') + b'/'
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
   146
        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
   147
            u = app.ui.copy()
44246
eecc005229ff config: also respect HGRCSKIPREPO in the zeroconf extension
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43506
diff changeset
   148
            if rcutil.use_repo_hgrc():
eecc005229ff config: also respect HGRCSKIPREPO in the zeroconf extension
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43506
diff changeset
   149
                u.readconfig(os.path.join(path, b'.hg', b'hgrc'))
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   150
            name = os.path.basename(repo)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   151
            path = (prefix + repo).strip(b'/')
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   152
            desc = u.config(b'web', b'description')
34238
a6c18628dff1 configitems: register the 'web.description' config
Boris Feld <boris.feld@octobus.net>
parents: 33178
diff changeset
   153
            if not desc:
a6c18628dff1 configitems: register the 'web.description' config
Boris Feld <boris.feld@octobus.net>
parents: 33178
diff changeset
   154
                desc = 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
   155
            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
   156
    return httpd
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   157
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   158
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   159
# listen
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   160
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   161
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   162
class listener(object):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   163
    def __init__(self):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   164
        self.found = {}
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   165
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   166
    def removeService(self, server, type, name):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   167
        if repr(name) in self.found:
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   168
            del self.found[repr(name)]
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   169
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   170
    def addService(self, server, type, name):
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   171
        self.found[repr(name)] = server.getServiceInfo(type, name)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   172
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   173
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   174
def getzcpaths():
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   175
    ip = getip()
43506
9f70512ae2cf cleanup: remove pointless r-prefixes on single-quoted strings
Augie Fackler <augie@google.com>
parents: 43503
diff changeset
   176
    if ip.startswith('127.'):
8264
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   177
        return
63ea850b3312 zeroconf: guess ip for Zeroconf
Alexander Solovyov <piranha@piranha.org.ua>
parents: 8225
diff changeset
   178
    server = Zeroconf.Zeroconf(ip)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   179
    l = listener()
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   180
    Zeroconf.ServiceBrowser(server, b"_hg._tcp.local.", l)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   181
    time.sleep(1)
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   182
    server.close()
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   183
    for value in l.found.values():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   184
        name = value.name[: value.name.index(b'.')]
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43500
diff changeset
   185
        url = "http://%s:%s%s" % (
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   186
            socket.inet_ntoa(value.address),
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   187
            value.port,
43503
313e3a279828 cleanup: remove pointless r-prefixes on double-quoted strings
Augie Fackler <augie@google.com>
parents: 43500
diff changeset
   188
            value.properties.get("path", "/"),
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   189
        )
42549
fa2071753dc2 zeroconf: port to Python 3
Gregory Szorc <gregory.szorc@gmail.com>
parents: 34238
diff changeset
   190
        yield b"zc-" + name, pycompat.bytestr(url)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   191
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   192
33178
a0068f49b5f6 zeroconf: blindly forward extra argument to the core config method
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 30885
diff changeset
   193
def config(orig, self, section, key, *args, **kwargs):
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   194
    if section == b"paths" and key.startswith(b"zc-"):
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   195
        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
   196
            if name == key:
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   197
                return path
33178
a0068f49b5f6 zeroconf: blindly forward extra argument to the core config method
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 30885
diff changeset
   198
    return orig(self, section, key, *args, **kwargs)
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   199
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   200
28038
72f2a19c5f88 zeroconf: forward all arguments passed to ui.configitems() wrapper
Yuya Nishihara <yuya@tcha.org>
parents: 27910
diff changeset
   201
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
   202
    repos = orig(self, section, *args, **kwargs)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   203
    if section == b"paths":
9488
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   204
        repos += getzcpaths()
33a6213a974e zeroconf: code cleanup, fixing variable names to be meaningful
Alexander Solovyov <piranha@piranha.org.ua>
parents: 9443
diff changeset
   205
    return repos
7071
643c751e60b2 zeroconf: initial implementation
Matt Mackall <mpm@selenic.com>
parents:
diff changeset
   206
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   207
28250
6d0d11731e1c zeroconf: fix crash in "hg paths" when zeroconf server is up
Danek Duvall <danek.duvall@oracle.com>
parents: 28038
diff changeset
   208
def configsuboptions(orig, self, section, name, *args, **kwargs):
6d0d11731e1c zeroconf: fix crash in "hg paths" when zeroconf server is up
Danek Duvall <danek.duvall@oracle.com>
parents: 28038
diff changeset
   209
    opt, sub = orig(self, section, name, *args, **kwargs)
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   210
    if section == b"paths" and name.startswith(b"zc-"):
28250
6d0d11731e1c zeroconf: fix crash in "hg paths" when zeroconf server is up
Danek Duvall <danek.duvall@oracle.com>
parents: 28038
diff changeset
   211
        # We have to find the URL in the zeroconf paths.  We can't cons up any
6d0d11731e1c zeroconf: fix crash in "hg paths" when zeroconf server is up
Danek Duvall <danek.duvall@oracle.com>
parents: 28038
diff changeset
   212
        # suboptions, so we use any that we found in the original config.
6d0d11731e1c zeroconf: fix crash in "hg paths" when zeroconf server is up
Danek Duvall <danek.duvall@oracle.com>
parents: 28038
diff changeset
   213
        for zcname, zcurl in getzcpaths():
6d0d11731e1c zeroconf: fix crash in "hg paths" when zeroconf server is up
Danek Duvall <danek.duvall@oracle.com>
parents: 28038
diff changeset
   214
            if zcname == name:
6d0d11731e1c zeroconf: fix crash in "hg paths" when zeroconf server is up
Danek Duvall <danek.duvall@oracle.com>
parents: 28038
diff changeset
   215
                return zcurl, sub
6d0d11731e1c zeroconf: fix crash in "hg paths" when zeroconf server is up
Danek Duvall <danek.duvall@oracle.com>
parents: 28038
diff changeset
   216
    return opt, sub
6d0d11731e1c zeroconf: fix crash in "hg paths" when zeroconf server is up
Danek Duvall <danek.duvall@oracle.com>
parents: 28038
diff changeset
   217
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   218
10342
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   219
def defaultdest(orig, source):
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   220
    for name, path in getzcpaths():
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   221
        if path == source:
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   222
            return name.encode(encoding.encoding)
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   223
    return orig(source)
579aae5aa549 zeroconf: override default destination folder on clone
Henrik Stuart <hg@hstuart.dk>
parents: 10317
diff changeset
   224
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   225
14104
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   226
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
   227
    try:
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   228
        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
   229
    finally:
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   230
        # 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
   231
        # 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
   232
        global server
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   233
        if server:
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   234
            server.close()
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   235
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 42549
diff changeset
   236
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   237
extensions.wrapfunction(dispatch, b'_runcommand', cleanupafterdispatch)
14104
23fc62e0a960 zeroconf: notify the Zeroconf threads when hg exits
Nicolas Dumazet <nicdumz.commits@gmail.com>
parents: 12076
diff changeset
   238
43077
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   239
extensions.wrapfunction(uimod.ui, b'config', config)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   240
extensions.wrapfunction(uimod.ui, b'configitems', configitems)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   241
extensions.wrapfunction(uimod.ui, b'configsuboptions', configsuboptions)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   242
extensions.wrapfunction(hg, b'defaultdest', defaultdest)
687b865b95ad formatting: byteify all mercurial/ and hgext/ string literals
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   243
extensions.wrapfunction(servermod, b'create_server', zc_create_server)