♻ Use vendored zeroconf only on Py2
Adjusted vendored version to match (used) signatures of current py3 only versions. Use official builds under Py3
This commit is contained in:
1 parent
252b9b7e41
commit
ffde393c66
3 files changed
+89
-42
No files matched your search
@@ -81,6 +81,11 @@ INSTALL_REQUIRES_PYTHON2 = [
|
||||
"typing>=3.7.4.1,<4"
|
||||
]
|
||||
|
||||
# Python 3 specific requirements
|
||||
INSTALL_REQUIRES_PYTHON3 = [
|
||||
"zeroconf>=0.24,<0.25"
|
||||
]
|
||||
|
||||
# OSX specific requirements
|
||||
INSTALL_REQUIRES_OSX = [
|
||||
"appdirs>=1.4.4",
|
||||
@@ -130,12 +135,15 @@ if int(setuptools.__version__.split(".", 1)[0]) < 18:
|
||||
# add optional dependencies for setuptools versions < 18 that don't yet support environment markers
|
||||
if sys.version_info[0] < 3:
|
||||
INSTALL_REQUIRES += INSTALL_REQUIRES_PYTHON2
|
||||
else:
|
||||
INSTALL_REQUIRES += INSTALL_REQUIRES_PYTHON3
|
||||
|
||||
if sys.platform == "darwin":
|
||||
INSTALL_REQUIRES += INSTALL_REQUIRES_OSX
|
||||
else:
|
||||
# environment markers supported
|
||||
EXTRA_REQUIRES[":python_version < '3'"] = INSTALL_REQUIRES_PYTHON2
|
||||
EXTRA_REQUIRES[":python_version >= '3'"] = INSTALL_REQUIRES_PYTHON3
|
||||
EXTRA_REQUIRES[":sys_platform == 'darwin'"] = INSTALL_REQUIRES_OSX
|
||||
|
||||
#-----------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
@@ -18,7 +18,14 @@ from builtins import range
|
||||
import octoprint.plugin
|
||||
import octoprint.util
|
||||
|
||||
import octoprint.vendor.zeroconf as zeroconf
|
||||
try:
|
||||
# python 3
|
||||
import zeroconf
|
||||
|
||||
except ImportError:
|
||||
# python 2: vendored version with some backported fixes
|
||||
import octoprint.vendor.zeroconf as zeroconf
|
||||
|
||||
import time
|
||||
import collections
|
||||
import socket
|
||||
@@ -197,21 +204,19 @@ class DiscoveryPlugin(octoprint.plugin.StartupPlugin,
|
||||
txt_record = self._format_zeroconf_txt(txt_record)
|
||||
|
||||
key = (reg_type, port)
|
||||
addresses = octoprint.util.net.interface_addresses()
|
||||
addresses = list(map(lambda x: socket.inet_aton(x), octoprint.util.net.interface_addresses()))
|
||||
|
||||
try:
|
||||
for address in addresses:
|
||||
info = zeroconf.ServiceInfo(reg_type,
|
||||
name,
|
||||
address=socket.inet_aton(address),
|
||||
port=port,
|
||||
properties=txt_record)
|
||||
self._zeroconf.register_service(info, allow_name_change=True)
|
||||
self._zeroconf_registrations[key].append(info)
|
||||
|
||||
info = zeroconf.ServiceInfo(reg_type, name,
|
||||
addresses=addresses,
|
||||
port=port,
|
||||
server="{}.local.".format(socket.gethostname()),
|
||||
properties=txt_record)
|
||||
self._zeroconf.register_service(info, allow_name_change=True)
|
||||
self._zeroconf_registrations[key].append(info)
|
||||
self._logger.info("Registered '{}' for {}".format(name, reg_type))
|
||||
except Exception:
|
||||
self._logger.exception("Could not (fully) register {} for {} on port {}".format(name, reg_type, port))
|
||||
self._logger.exception("Could not register {} for {} on port {}".format(name, reg_type, port))
|
||||
|
||||
def zeroconf_unregister(self, reg_type, port=None):
|
||||
"""
|
||||
@@ -292,16 +297,24 @@ class DiscoveryPlugin(octoprint.plugin.StartupPlugin,
|
||||
self._logger.debug("Got a browsing result for Zeroconf resolution of {}, resolving...".format(type))
|
||||
info = zeroconf.get_service_info(type, name, timeout=resolve_timeout * 1000)
|
||||
if info:
|
||||
address = socket.inet_ntoa(info.address)
|
||||
self._logger.debug("Resolved a result for Zeroconf resolution of {}: {} @ {}".format(type,
|
||||
name,
|
||||
address))
|
||||
result.append(dict(
|
||||
name=info.name[:-(len(type) + 1)],
|
||||
host=address,
|
||||
port=info.port,
|
||||
txt_record=info.properties
|
||||
))
|
||||
def to_result(info, address):
|
||||
n = info.name[:-(len(type) + 1)]
|
||||
p = info.port
|
||||
|
||||
self._logger.debug("Resolved a result for Zeroconf resolution of {}: {} @ {}:{}".format(type,
|
||||
n,
|
||||
address,
|
||||
p))
|
||||
|
||||
return dict(name=n, host=address, port=p, txt_record=info.properties)
|
||||
|
||||
if hasattr(info, "addresses"):
|
||||
# Python 3
|
||||
for address in map(lambda x: socket.inet_ntoa(x), info.addresses):
|
||||
result.append(to_result(info, address))
|
||||
else:
|
||||
# Python 2
|
||||
result.append(to_result(info, socket.inet_ntoa(info.address)))
|
||||
|
||||
self._logger.debug("Browsing Zeroconf for {service_type}".format(service_type=service_type))
|
||||
|
||||
|
||||
Vendored
+46
-20
@@ -1343,13 +1343,14 @@ class ServiceInfo(object):
|
||||
|
||||
"""Service information"""
|
||||
|
||||
def __init__(self, type_, name, address=None, port=None, weight=0,
|
||||
def __init__(self, type_, name, addresses=None, port=None, weight=0,
|
||||
priority=0, properties=None, server=None):
|
||||
"""Create a service description.
|
||||
|
||||
type_: fully qualified service type name
|
||||
name: fully qualified service name
|
||||
address: IP address as unsigned short, network byte order
|
||||
addresses: List of IP addresses as unsigned short (IPv4),
|
||||
network byte order
|
||||
port: port that the service runs on
|
||||
weight: weight of the service
|
||||
priority: priority of the service
|
||||
@@ -1361,7 +1362,16 @@ class ServiceInfo(object):
|
||||
raise BadTypeInNameException
|
||||
self.type = type_
|
||||
self.name = name
|
||||
self.address = address
|
||||
if addresses is not None:
|
||||
self._addresses = addresses
|
||||
else:
|
||||
self._addresses = []
|
||||
# This results in an ugly error when registering, better check now
|
||||
invalid = [a for a in self._addresses
|
||||
if not isinstance(a, bytes) or len(a) != 4]
|
||||
if invalid:
|
||||
raise TypeError('Addresses must be bytes, got %s. Hint: convert string addresses '
|
||||
'with socket.inet_pton' % invalid)
|
||||
self.port = port
|
||||
self.weight = weight
|
||||
self.priority = priority
|
||||
@@ -1372,6 +1382,19 @@ class ServiceInfo(object):
|
||||
self._properties = {}
|
||||
self._set_properties(properties)
|
||||
|
||||
@property
|
||||
def addresses(self):
|
||||
"""IPv4 addresses of this service."""
|
||||
return self._addresses
|
||||
|
||||
@addresses.setter
|
||||
def addresses(self, value):
|
||||
"""Replace the addresses list.
|
||||
|
||||
This replaces all currently stored addresses.
|
||||
"""
|
||||
self._addresses = value
|
||||
|
||||
@property
|
||||
def properties(self):
|
||||
return self._properties
|
||||
@@ -1451,7 +1474,8 @@ class ServiceInfo(object):
|
||||
if record.type == _TYPE_A:
|
||||
# if record.name == self.name:
|
||||
if record.name == self.server:
|
||||
self.address = record.address
|
||||
if record.address not in self._addresses:
|
||||
self._addresses.append(record.address)
|
||||
elif record.type == _TYPE_SRV:
|
||||
if record.name == self.name:
|
||||
self.server = record.server
|
||||
@@ -1486,12 +1510,12 @@ class ServiceInfo(object):
|
||||
if cached:
|
||||
self.update_record(zc, now, cached)
|
||||
|
||||
if None not in (self.server, self.address, self.text):
|
||||
if self.server is not None and self.text is not None and self._addresses:
|
||||
return True
|
||||
|
||||
try:
|
||||
zc.add_listener(self, DNSQuestion(self.name, _TYPE_ANY, _CLASS_IN))
|
||||
while None in (self.server, self.address, self.text):
|
||||
while self.server is None or self.text is None or not self._addresses:
|
||||
if last <= now:
|
||||
return False
|
||||
if next_ <= now:
|
||||
@@ -1540,7 +1564,7 @@ class ServiceInfo(object):
|
||||
', '.join(
|
||||
'%s=%r' % (name, getattr(self, name))
|
||||
for name in (
|
||||
'type', 'name', 'address', 'port', 'weight', 'priority',
|
||||
'type', 'name', 'addresses', 'port', 'weight', 'priority',
|
||||
'server', 'properties',
|
||||
)
|
||||
)
|
||||
@@ -1780,10 +1804,10 @@ class Zeroconf(QuietLogger):
|
||||
|
||||
out.add_answer_at_time(
|
||||
DNSText(info.name, _TYPE_TXT, _CLASS_IN, ttl, info.text), 0)
|
||||
if info.address:
|
||||
for address in info.addresses:
|
||||
out.add_answer_at_time(
|
||||
DNSAddress(info.server, _TYPE_A, _CLASS_IN,
|
||||
ttl, info.address), 0)
|
||||
ttl, address), 0)
|
||||
self.send(out)
|
||||
i += 1
|
||||
next_time += _REGISTER_TIME
|
||||
@@ -1815,10 +1839,10 @@ class Zeroconf(QuietLogger):
|
||||
out.add_answer_at_time(
|
||||
DNSText(info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0)
|
||||
|
||||
if info.address:
|
||||
for address in info.addresses:
|
||||
out.add_answer_at_time(
|
||||
DNSAddress(info.server, _TYPE_A, _CLASS_IN, 0,
|
||||
info.address), 0)
|
||||
address), 0)
|
||||
self.send(out)
|
||||
i += 1
|
||||
next_time += _UNREGISTER_TIME
|
||||
@@ -1843,10 +1867,10 @@ class Zeroconf(QuietLogger):
|
||||
info.priority, info.weight, info.port, info.server), 0)
|
||||
out.add_answer_at_time(DNSText(
|
||||
info.name, _TYPE_TXT, _CLASS_IN, 0, info.text), 0)
|
||||
if info.address:
|
||||
for address in info.addresses:
|
||||
out.add_answer_at_time(DNSAddress(
|
||||
info.server, _TYPE_A, _CLASS_IN, 0,
|
||||
info.address), 0)
|
||||
address), 0)
|
||||
self.send(out)
|
||||
i += 1
|
||||
next_time += _UNREGISTER_TIME
|
||||
@@ -1978,10 +2002,11 @@ class Zeroconf(QuietLogger):
|
||||
if question.type in (_TYPE_A, _TYPE_ANY):
|
||||
for service in self.services.values():
|
||||
if service.server == question.name.lower():
|
||||
out.add_answer(msg, DNSAddress(
|
||||
question.name, _TYPE_A,
|
||||
_CLASS_IN | _CLASS_UNIQUE,
|
||||
_DNS_TTL, service.address))
|
||||
for address in service.addresses:
|
||||
out.add_answer(msg, DNSAddress(
|
||||
question.name, _TYPE_A,
|
||||
_CLASS_IN | _CLASS_UNIQUE,
|
||||
_DNS_TTL, address))
|
||||
|
||||
service = self.services.get(question.name.lower(), None)
|
||||
if not service:
|
||||
@@ -1997,9 +2022,10 @@ class Zeroconf(QuietLogger):
|
||||
question.name, _TYPE_TXT, _CLASS_IN | _CLASS_UNIQUE,
|
||||
_DNS_TTL, service.text))
|
||||
if question.type == _TYPE_SRV:
|
||||
out.add_additional_answer(DNSAddress(
|
||||
service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE,
|
||||
_DNS_TTL, service.address))
|
||||
for address in service.addresses:
|
||||
out.add_additional_answer(DNSAddress(
|
||||
service.server, _TYPE_A, _CLASS_IN | _CLASS_UNIQUE,
|
||||
_DNS_TTL, address))
|
||||
except Exception: # TODO stop catching all Exceptions
|
||||
self.log_exception_warning()
|
||||
|
||||
|
||||
Reference in new issue
Block a user