From c3e99bfc1d1e271eaac5b981cf4471a7d6888a9b Mon Sep 17 00:00:00 2001 From: grossmj Date: Sun, 8 Nov 2015 13:34:27 -0700 Subject: [PATCH] Drop netifaces (replaced by psutil). Fixes #344. --- README.rst | 2 +- gns3server/modules/docker/docker_vm.py | 7 ++----- gns3server/utils/interfaces.py | 16 ++++++++-------- requirements.txt | 3 +-- setup.py | 12 ++---------- tests/conftest.py | 4 ++-- tests/utils/test_interfaces.py | 4 ++-- 7 files changed, 18 insertions(+), 30 deletions(-) diff --git a/README.rst b/README.rst index 891577aa..e1ab30af 100644 --- a/README.rst +++ b/README.rst @@ -45,7 +45,7 @@ Dependencies: - Python 3.4 or above - aiohttp - setuptools -- netifaces +- psutil - jsonschema The following commands will install some of these dependencies: diff --git a/gns3server/modules/docker/docker_vm.py b/gns3server/modules/docker/docker_vm.py index 50e56472..97d405ec 100644 --- a/gns3server/modules/docker/docker_vm.py +++ b/gns3server/modules/docker/docker_vm.py @@ -21,8 +21,7 @@ Docker container instance. import asyncio import shutil -import docker -import netifaces +import psutil from docker.utils import create_host_config from gns3server.ubridge.hypervisor import Hypervisor @@ -271,10 +270,8 @@ class Container(BaseVM): name=self.name, adapter_number=adapter_number)) if nio and isinstance(nio, NIOUDP): - ifcs = netifaces.interfaces() for index in range(128): - ifcs = netifaces.interfaces() - if "gns3-veth{}ext".format(index) not in ifcs: + if "gns3-veth{}ext".format(index) not in psutil.net_if_addrs(): adapter.ifc = "eth{}".format(str(index)) adapter.host_ifc = "gns3-veth{}ext".format(str(index)) adapter.guest_ifc = "gns3-veth{}int".format(str(index)) diff --git a/gns3server/utils/interfaces.py b/gns3server/utils/interfaces.py index 6cb70097..f7b5a312 100644 --- a/gns3server/utils/interfaces.py +++ b/gns3server/utils/interfaces.py @@ -20,6 +20,7 @@ import sys import aiohttp import socket import struct +import psutil import logging log = logging.getLogger(__name__) @@ -114,8 +115,7 @@ def is_interface_up(interface): if sys.platform.startswith("linux"): - import netifaces - if interface not in netifaces.interfaces(): + if interface not in psutil.net_if_addrs(): return False import fcntl @@ -143,13 +143,13 @@ def interfaces(): results = [] if not sys.platform.startswith("win"): - import netifaces - for interface in netifaces.interfaces(): + for interface in sorted(psutil.net_if_addrs().keys()): ip_address = "" - ip_addresses = netifaces.ifaddresses(interface) - if netifaces.AF_INET in ip_addresses and ip_addresses[netifaces.AF_INET]: - # get the first IPv4 address only - ip_address = ip_addresses[netifaces.AF_INET][0]["addr"] + for addr in psutil.net_if_addrs()[interface]: + # get the first available IPv4 address only + if addr.family == socket.AF_INET: + ip_address = addr.address + break results.append({"id": interface, "name": interface, "ip_address": ip_address}) diff --git a/requirements.txt b/requirements.txt index e0912e13..a623fb77 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,5 @@ jsonschema>=2.4.0 aiohttp==0.17.4 Jinja2>=2.7.3 raven>=5.2.0 -gns3-netifaces==0.10.4.1 docker-py==1.4.0 -psutil>=2.2.1 +psutil>=3.0.0 diff --git a/setup.py b/setup.py index 9ffb10f5..db28855c 100644 --- a/setup.py +++ b/setup.py @@ -44,18 +44,10 @@ dependencies = [ "Jinja2>=2.7.3", "raven>=5.2.0", "docker-py>=1.4.0", - "psutil>=2.2.1" + "psutil>=3.0.0" ] -if not sys.platform.startswith("win"): - # netifaces if not used on Windows - try: - import netifaces - except ImportError: - # add gns3-netifaces only if netifaces isn't already installed - # for instance via a Debian package. - dependencies.append("gns3-netifaces>=0.10.4.1") -else: +if sys.platform.startswith("win"): dependencies.append("pywin32>=219") setup( diff --git a/tests/conftest.py b/tests/conftest.py index d3c38ecd..eff35558 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -132,8 +132,8 @@ def free_console_port(request, port_manager, project): @pytest.fixture def ethernet_device(): - import netifaces - return netifaces.interfaces()[0] + import psutil + return sorted(psutil.net_if_addrs().keys())[0] @pytest.yield_fixture(autouse=True) diff --git a/tests/utils/test_interfaces.py b/tests/utils/test_interfaces.py index 2380ae3c..153639dc 100644 --- a/tests/utils/test_interfaces.py +++ b/tests/utils/test_interfaces.py @@ -16,7 +16,6 @@ # along with this program. If not, see . import sys -import netifaces from gns3server.utils.interfaces import interfaces, is_interface_up @@ -28,7 +27,8 @@ def test_interfaces(): def test_is_interface_up(): if sys.platform.startswith("win"): - assert is_interface_up(netifaces.interfaces[0]) is True + # is_interface_up() always returns True on Windows + pass elif sys.platform.startswith("darwin"): assert is_interface_up("lo0") is True else: