You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by an...@apache.org on 2016/06/24 04:29:09 UTC
[3/5] libcloud git commit: refactored _parse_arp_table
refactored _parse_arp_table
split _parse_arp_table into 1 general and 2 specialized functions
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/fa63096a
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/fa63096a
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/fa63096a
Branch: refs/heads/trunk
Commit: fa63096af079ff39873d7b62c3da8b93aab9d617
Parents: 20c924b
Author: Rene Kjellerup <rk...@gmail.com>
Authored: Fri Jun 3 17:02:19 2016 -0700
Committer: Anthony Shaw <an...@apache.org>
Committed: Fri Jun 24 14:27:46 2016 +1000
----------------------------------------------------------------------
libcloud/compute/drivers/libvirt_driver.py | 39 ++++++++++++++++++-------
1 file changed, 28 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/libcloud/blob/fa63096a/libcloud/compute/drivers/libvirt_driver.py
----------------------------------------------------------------------
diff --git a/libcloud/compute/drivers/libvirt_driver.py b/libcloud/compute/drivers/libvirt_driver.py
index c8a6a7c..9d4e1a0 100644
--- a/libcloud/compute/drivers/libvirt_driver.py
+++ b/libcloud/compute/drivers/libvirt_driver.py
@@ -306,15 +306,14 @@ class LibvirtNodeDriver(NodeDriver):
child = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, _ = child.communicate()
- arp_table = self._parse_arp_table(arp_output=stdout)
+ arp_table = self._parse_ip_table_arp(arp_output=stdout)
except OSError as e:
if e.errno == 2:
cmd = ['ip', 'neigh']
child = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, _ = child.communicate()
- arp_table = self._parse_arp_table(arp_output=stdout,
- arp_cmd='ip')
+ arp_table = self._parse_ip_table_neigh(arp_output=stdout)
for mac_address in mac_addresses:
if mac_address in arp_table:
@@ -361,23 +360,41 @@ class LibvirtNodeDriver(NodeDriver):
return result
- def _parse_arp_table(self, arp_output, arp_cmd='arp'):
+ def _parse_ip_table_arp(self, arp_output):
"""
- Parse arp command output and return a dictionary which maps mac address
+ Sets up the regexp for parsing out IP addresses from the 'arp -an'
+ command and pass it along to the parser function.
+
+ :return: Dictionary from the parsing funtion
+ :rtype: ``dict``
+ """
+ arp_regex = re.compile('.*?\((.*?)\) at (.*?)\s+')
+ return self._parse_mac_addr_table(arp_output, arp_regex)
+
+ def _parse_ip_table_neigh(self, ip_output):
+ """
+ Sets up the regexp for parsing out IP addresses from the 'ip neighbor'
+ command and pass it along to the parser function.
+
+ :return: Dictionary from the parsing funtion
+ :rtype: ``dict``
+ """
+ ip_regex = re.compile('(.*?)\s+.*lladdr\s+(.*?)\s+')
+ return self._parse_mac_addr_table(ip_output, ip_regex)
+
+ def _parse_mac_addr_table(self, cmd_output, mac_regex):
+ """
+ Parse the command output and return a dictionary which maps mac address
to an IP address.
:return: Dictionary which maps mac address to IP address.
:rtype: ``dict``
"""
- re_match = {}
- re_match['arp'] = '.*?\((.*?)\) at (.*?)\s+'
- re_match['ip'] = '(.*?)\s+.*lladdr\s+(.*?)\s+'
- ip_mac = re.compile(re_match[arp_cmd])
- lines = arp_output.split('\n')
+ lines = cmd_output.split('\n')
arp_table = defaultdict(list)
for line in lines:
- match = ip_mac.match(line)
+ match = mac_regex.match(line)
if not match:
continue