From: Radek Pazdera rpazdera@redhat.com
I separated scan_netdevs method from the class, changed it to regular function and moved it into the Common.NetUtils module, because it will be useful elsewhere in the code, but not in the context of the class and outside of the NetConfig module.
Signed-off-by: Radek Pazdera rpazdera@redhat.com --- Common/NetUtils.py | 19 +++++++++++++++++++ NetConfig/NetConfigDevNames.py | 23 +++-------------------- 2 files changed, 22 insertions(+), 20 deletions(-)
diff --git a/Common/NetUtils.py b/Common/NetUtils.py index 7e642b6..42ee6f0 100644 --- a/Common/NetUtils.py +++ b/Common/NetUtils.py @@ -11,6 +11,7 @@ rpazdera@redhat.com (Radek Pazdera) """
import logging +import os import re import socket import subprocess @@ -18,6 +19,24 @@ import subprocess def normalize_hwaddr(hwaddr): return hwaddr.upper().rstrip("\n")
+def scan_netdevs(): + sys_dir = "/sys/class/net" + scan = [] + for root, dirs, files in os.walk(sys_dir): + if "lo" in dirs: + dirs.remove("lo") + for d in dirs: + dev_path = os.path.join(sys_dir, d) + addr_path = os.path.join(dev_path, "address") + if not os.path.isfile(addr_path): + continue + handle = open(addr_path, "rb") + addr = handle.read() + handle.close() + addr = normalize_hwaddr(addr) + scan.append({"name": d, "hwaddr": addr}) + return scan + def get_corespond_local_ip(query_ip): """ Get ip address in local system which can communicate with query_ip. diff --git a/NetConfig/NetConfigDevNames.py b/NetConfig/NetConfigDevNames.py index 7c7f862..0f246d9 100644 --- a/NetConfig/NetConfigDevNames.py +++ b/NetConfig/NetConfigDevNames.py @@ -14,31 +14,14 @@ import logging import os from NetConfigCommon import get_option from Common.NetUtils import normalize_hwaddr +from Common.NetUtils import scan_netdevs
class NetConfigDevNames: def __init__(self): - self._scan = self._scan_netdevs() - - def _scan_netdevs(self): - sys_dir = "/sys/class/net" - scan = [] - for root, dirs, files in os.walk(sys_dir): - if "lo" in dirs: - dirs.remove("lo") - for d in dirs: - dev_path = os.path.join(sys_dir, d) - addr_path = os.path.join(dev_path, "address") - if not os.path.isfile(addr_path): - continue - handle = open(addr_path, "rb") - addr = handle.read() - handle.close() - addr = normalize_hwaddr(addr) - scan.append({"name": d, "hwaddr": addr}) - return scan + self._scan = scan_netdevs()
def rescan_netdevs(self): - self._scan = self._scan_netdevs() + self._scan = scan_netdevs()
def assign_name_by_scan(self, dev_id, netdev): if (not "hwaddr" in netdev or