Make netifaces optional

netifaces is a binary Python extension. Outside of the installer, it's
only used by CheckedIPAddress.get_matching_interface, which is only
called from installer code.

Make the import of netifaces optional to reduce the amount of
dependencies for PyPI package use case. Binary extensions are especially
annoying, because they depend on shared libraries, compiler, and header
files to be present.

Related: https://pagure.io/freeipa/issue/6468
Signed-off-by: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Oleg Kozlov <okozlov@redhat.com>
This commit is contained in:
Christian Heimes 2019-04-07 20:55:15 +02:00
parent 8a5dc1b375
commit a5213140c3
2 changed files with 9 additions and 2 deletions

View File

@ -34,7 +34,6 @@ import socket
import re import re
import datetime import datetime
import netaddr import netaddr
import netifaces
import time import time
import pwd import pwd
import grp import grp
@ -49,6 +48,11 @@ from dns.exception import DNSException
import six import six
from six.moves import input from six.moves import input
try:
import netifaces
except ImportError:
netifaces = None
from ipapython.dn import DN from ipapython.dn import DN
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -197,6 +201,8 @@ class CheckedIPAddress(UnsafeIPAddress):
:return: InterfaceDetails named tuple or None if no interface has :return: InterfaceDetails named tuple or None if no interface has
this address this address
""" """
if netifaces is None:
raise ImportError("netifaces")
logger.debug("Searching for an interface of IP address: %s", self) logger.debug("Searching for an interface of IP address: %s", self)
if self.version == 4: if self.version == 4:
family = netifaces.AF_INET family = netifaces.AF_INET

View File

@ -43,11 +43,12 @@ if __name__ == '__main__':
# "ipalib", # circular dependency # "ipalib", # circular dependency
"ipaplatform", "ipaplatform",
"netaddr", "netaddr",
"netifaces",
"python-ldap", "python-ldap",
"six", "six",
], ],
extras_require={ extras_require={
"install": ["dbus-python"], # for certmonger "install": ["dbus-python"], # for certmonger
# CheckedIPAddress.get_matching_interface
"netifaces": ["netifaces"],
}, },
) )