##// END OF EJS Templates
zeroconf: use only first part of hostname for building local name
Matt Mackall -
r7087:62c71741 default
parent child Browse files
Show More
@@ -1,133 +1,134 b''
1 1 # zeroconf.py - zeroconf support for Mercurial
2 2 #
3 3 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com>
4 4 #
5 5 # This software may be used and distributed according to the terms of
6 6 # the GNU General Public License (version 2), incorporated herein by
7 7 # reference.
8 8
9 9 import Zeroconf, socket, time, os
10 10 from mercurial import ui
11 11 from mercurial.hgweb import hgweb_mod
12 12 from mercurial.hgweb import hgwebdir_mod
13 13
14 14 # publish
15 15
16 16 server = None
17 17 localip = None
18 18
19 19 def getip():
20 20 # finds external-facing interface without sending any packets (Linux)
21 21 try:
22 22 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
23 23 s.connect(('1.0.0.1', 0))
24 24 ip = s.getsockname()[0]
25 25 return ip
26 26 except:
27 27 pass
28 28
29 29 # Generic method, sometimes gives useless results
30 30 dumbip = socket.gethostbyaddr(socket.gethostname())[2][0]
31 31 if not dumbip.startswith('127.'):
32 32 return dumbip
33 33
34 34 # works elsewhere, but actually sends a packet
35 35 try:
36 36 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
37 37 s.connect(('1.0.0.1', 1))
38 38 ip = s.getsockname()[0]
39 39 return ip
40 40 except:
41 41 pass
42 42
43 43 return dumbip
44 44
45 45 def publish(name, desc, path, port):
46 46 global server, localip
47 47 if not server:
48 48 server = Zeroconf.Zeroconf()
49 49 ip = getip()
50 50 localip = socket.inet_aton(ip)
51 51
52 host = socket.gethostname() + ".local"
52 parts = socket.gethostname().split('.')
53 host = parts[0] + ".local"
53 54
54 55 # advertise to browsers
55 56 svc = Zeroconf.ServiceInfo('_http._tcp.local.',
56 57 name + '._http._tcp.local.',
57 58 server = host,
58 59 port = port,
59 60 properties = {'description': desc,
60 61 'path': "/" + path},
61 62 address = localip, weight = 0, priority = 0)
62 63 server.registerService(svc)
63 64
64 65 # advertise to Mercurial clients
65 66 svc = Zeroconf.ServiceInfo('_hg._tcp.local.',
66 67 name + '._hg._tcp.local.',
67 68 port = port,
68 69 properties = {'description': desc,
69 70 'path': "/" + path},
70 71 address = localip, weight = 0, priority = 0)
71 72 server.registerService(svc)
72 73
73 74 class hgwebzc(hgweb_mod.hgweb):
74 75 def __init__(self, repo, name=None):
75 76 super(hgwebzc, self).__init__(repo, name)
76 77 name = self.reponame or os.path.basename(repo.root)
77 78 desc = self.repo.ui.config("web", "description", name)
78 79 publish(name, desc, name, int(repo.ui.config("web", "port", 8000)))
79 80
80 81 class hgwebdirzc(hgwebdir_mod.hgwebdir):
81 82 def run(self):
82 83 print os.environ
83 84 for r, p in self.repos:
84 85 u = ui.ui(parentui=self.parentui)
85 86 u.readconfig(os.path.join(path, '.hg', 'hgrc'))
86 87 n = os.path.basename(r)
87 88 desc = u.config("web", "description", n)
88 89 publish(n, "hgweb", p, int(repo.ui.config("web", "port", 8000)))
89 90 return super(hgwebdirzc, self).run()
90 91
91 92 # listen
92 93
93 94 class listener(object):
94 95 def __init__(self):
95 96 self.found = {}
96 97 def removeService(self, server, type, name):
97 98 if repr(name) in self.found:
98 99 del self.found[repr(name)]
99 100 def addService(self, server, type, name):
100 101 self.found[repr(name)] = server.getServiceInfo(type, name)
101 102
102 103 def getzcpaths():
103 104 server = Zeroconf.Zeroconf()
104 105 l = listener()
105 106 browser = Zeroconf.ServiceBrowser(server, "_hg._tcp.local.", l)
106 107 time.sleep(1)
107 108 server.close()
108 109 for v in l.found.values():
109 110 n = v.name[:v.name.index('.')]
110 111 n.replace(" ", "-")
111 112 u = "http://%s:%s%s" % (socket.inet_ntoa(v.address), v.port,
112 113 v.properties.get("path", "/"))
113 114 yield "zc-" + n, u
114 115
115 116 def config(self, section, key, default=None, untrusted=False):
116 117 if section == "paths" and key.startswith("zc-"):
117 118 for n, p in getzcpaths():
118 119 if n == key:
119 120 return p
120 121 return oldconfig(self, section, key, default, untrusted)
121 122
122 123 def configitems(self, section):
123 124 r = oldconfigitems(self, section, untrusted=False)
124 125 if section == "paths":
125 126 r += getzcpaths()
126 127 return r
127 128
128 129 oldconfig = ui.ui.config
129 130 oldconfigitems = ui.ui.configitems
130 131 ui.ui.config = config
131 132 ui.ui.configitems = configitems
132 133 hgweb_mod.hgweb = hgwebzc
133 134 hgwebdir_mod.hgwebdir = hgwebdirzc
General Comments 0
You need to be logged in to leave comments. Login now