use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / src / nepi / data / processing / ccn / parser.py
index e3c1007..7d35d9e 100644 (file)
@@ -6,9 +6,8 @@
 #    Copyright (C) 2014 INRIA
 #
 #    This program is free software: you can redistribute it and/or modify
 #    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
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -41,6 +40,8 @@
 #                        \ nid3.log
 #
 
 #                        \ nid3.log
 #
 
+from __future__ import print_function
+
 import collections
 import functools
 import networkx
 import collections
 import functools
 import networkx
@@ -130,7 +131,7 @@ def parse_file(filename):
         try:
             size = int((cols[6]).replace('(',''))
         except:
         try:
             size = int((cols[6]).replace('(',''))
         except:
-            print "interest_expiry without face id!", line
+            print("interest_expiry without face id!", line)
             continue
 
         # If no external IP address was identified for this face
             continue
 
         # If no external IP address was identified for this face
@@ -199,6 +200,7 @@ def annotate_cn_graph(logs_dir, graph, parse_ping_logs = False):
     """ Adds CCN content history for each node in the topology graph.
 
     """
     """ Adds CCN content history for each node in the topology graph.
 
     """
+    
     # Make a copy of the graph to ensure integrity
     graph = graph.copy()
 
     # Make a copy of the graph to ensure integrity
     graph = graph.copy()
 
@@ -209,6 +211,8 @@ def annotate_cn_graph(logs_dir, graph, parse_ping_logs = False):
         for ip in ips:
             ips2nid[ip] = nid
 
         for ip in ips:
             ips2nid[ip] = nid
 
+    found_files = False
+
     # Now walk through the ccnd logs...
     for dirpath, dnames, fnames in os.walk(logs_dir):
         # continue if we are not at the leaf level (if there are subdirectories)
     # Now walk through the ccnd logs...
     for dirpath, dnames, fnames in os.walk(logs_dir):
         # continue if we are not at the leaf level (if there are subdirectories)
@@ -217,11 +221,16 @@ def annotate_cn_graph(logs_dir, graph, parse_ping_logs = False):
         
         # Each dirpath correspond to a different node
         nid = os.path.basename(dirpath)
         
         # Each dirpath correspond to a different node
         nid = os.path.basename(dirpath)
+
+        # Cast to numeric nid if necessary
+        if int(nid) in graph.nodes():
+            nid = int(nid)
     
         content_history = dict()
 
         for fname in fnames:
             if fname.endswith(".log"):
     
         content_history = dict()
 
         for fname in fnames:
             if fname.endswith(".log"):
+                found_files = True
                 filename = os.path.join(dirpath, fname)
                 data = parse_file(filename)
                 annotate_cn_node(graph, nid, ips2nid, data, content_history)
                 filename = os.path.join(dirpath, fname)
                 data = parse_file(filename)
                 annotate_cn_node(graph, nid, ips2nid, data, content_history)
@@ -231,6 +240,10 @@ def annotate_cn_graph(logs_dir, graph, parse_ping_logs = False):
         fname = dump_content_history(content_history)
         graph.node[nid]["history"] = fname
 
         fname = dump_content_history(content_history)
         graph.node[nid]["history"] = fname
 
+    if not found_files:
+        msg = "No CCND output files were found to parse at %s " % logs_dir
+        raise RuntimeError, msg
+
     if parse_ping_logs:
         ping_parser.annotate_cn_graph(logs_dir, graph)
 
     if parse_ping_logs:
         ping_parser.annotate_cn_graph(logs_dir, graph)
 
@@ -365,7 +378,7 @@ def process_content_history(graph):
         interest_count,
         content_count)
 
         interest_count,
         content_count)
 
-def process_content_history_logs(logs_dir, graph):
+def process_content_history_logs(logs_dir, graph, parse_ping_logs = False):
     """ Parse CCN logs and aggregate content history information in graph.
     Returns annotated graph and message countn and content names history.
 
     """ Parse CCN logs and aggregate content history information in graph.
     Returns annotated graph and message countn and content names history.
 
@@ -373,9 +386,9 @@ def process_content_history_logs(logs_dir, graph):
     ## Process logs and analyse data
     try:
         graph = annotate_cn_graph(logs_dir, graph, 
     ## Process logs and analyse data
     try:
         graph = annotate_cn_graph(logs_dir, graph, 
-                parse_ping_logs = True)
+                parse_ping_logs = parse_ping_logs)
     except:
     except:
-        print "Skipping: Error parsing ccnd logs", logs_dir
+        print("Skipping: Error parsing ccnd logs", logs_dir)
         raise
 
     source = ccn_consumers(graph)[0]
         raise
 
     source = ccn_consumers(graph)[0]
@@ -391,7 +404,7 @@ def process_content_history_logs(logs_dir, graph):
         interest_count,
         content_count) = process_content_history(graph)
     except:
         interest_count,
         content_count) = process_content_history(graph)
     except:
-        print "Skipping: Error processing ccn data", logs_dir
+        print("Skipping: Error processing ccn data", logs_dir)
         raise
 
     return (graph,
         raise
 
     return (graph,