applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / data / processing / ping / parser.py
index 3396207..d531118 100644 (file)
@@ -6,9 +6,8 @@
 #    Copyright (C) 2014 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU General Public License version 2 as
+#    published by the Free Software Foundation;
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -27,7 +26,9 @@
 # This library contains functions to parse log files generated using ping. 
 #
 
+import collections
 import re
+import os
 
 # RE to match line starting "traceroute to"
 _rre = re.compile("\d+ bytes from ((?P<hostname>[^\s]+) )?\(?(?P<ip>[^\s]+)\)??: icmp_.eq=\d+ ttl=\d+ time=(?P<time>[^\s]+) ms")
@@ -38,33 +39,31 @@ def parse_file(filename):
 
     """
 
-    f = open(filename, "r")
+    with open(filename, "r") as f:
 
-    # Traceroute info
-    target_ip = None
-    target_hostname = None
+        # Traceroute info
+        target_ip = None
+        target_hostname = None
    
-    data = []
-
-    for line in f:
-        # match traceroute to ...
-        m = re.match(_rre, line)
-        if not m:
-            continue
-
-        target_ip = m.groupdict()["ip"]
-        # FIX THIS: Make sure the regular expression does not inlcude 
-        # the ')' in the ip group 
-        target_ip = target_ip.replace(")","")
-        target_hostname = m.groupdict()["hostname"]
-        time = m.groupdict()["time"]
-        data.append((target_ip, target_hostname, time))
-
-    f.close()
+        data = []
+
+        for line in f:
+            # match traceroute to ...
+            m = re.match(_rre, line)
+            if not m:
+                continue
+
+            target_ip = m.groupdict()["ip"]
+            # FIX THIS: Make sure the regular expression does not inlcude 
+            # the ')' in the ip group 
+            target_ip = target_ip.replace(")","")
+            target_hostname = m.groupdict()["hostname"]
+            time = m.groupdict()["time"]
+            data.append((target_ip, target_hostname, time))
 
     return data
 
-def annotate_cn_node(graph, nid1, ips2nids, data):
+def annotate_cn_node(graph, nid1, ips2nid, data):
     for (target_ip, target_hostname, time) in data:
         nid2 = ips2nid[target_ip]
 
@@ -80,14 +79,16 @@ def annotate_cn_graph(logs_dir, graph):
     ping.
 
     """
-    ips2nids = dict()
+    ips2nid = dict()
 
     for nid in graph.nodes():
         ips = graph.node[nid]["ips"]
         for ip in ips:
-            ips2nids[ip] = nid
+            ips2nid[ip] = nid
 
     # Walk through the ping logs...
+    found_files = False
+
     for dirpath, dnames, fnames in os.walk(logs_dir):
         # continue if we are not at the leaf level (if there are subdirectories)
         if dnames: 
@@ -98,9 +99,14 @@ def annotate_cn_graph(logs_dir, graph):
     
         for fname in fnames:
             if fname.endswith(".ping"):
+                found_files = True
                 filename = os.path.join(dirpath, fname)
                 data = parse_file(filename)
-                annotate_cn_node(graph, nid, ips2nids, data)
+                annotate_cn_node(graph, nid, ips2nid, data)
+
+    if not found_files:
+        msg = "No PING output files were found to parse at %s " % logs_dir 
+        raise RuntimeError(msg)
 
     # Take as weight the most frequent value
     for nid1, nid2 in graph.edges():