Add function that gets lists from Python files
authorMitch Watrous <watrous@u.washington.edu>
Mon, 13 Dec 2010 13:09:46 -0800
changeset 6673 31c2c3f09197
parent 6672 0c65dba78e40
child 6674 a7836d788fa1
Add function that gets lists from Python files
test.py
util.py
--- a/test.py	Fri Dec 10 16:25:06 2010 -0800
+++ b/test.py	Mon Dec 13 13:09:46 2010 -0800
@@ -29,6 +29,8 @@
 import shutil
 import re
 
+from util import get_list_from_file
+
 #
 # XXX This should really be part of a waf command to list the configuration
 # items relative to optional ns-3 pieces.
@@ -1056,38 +1058,20 @@
         # Read in the examples-to-run file if it exists.
         examples_to_run_path = os.path.join("examples", example_directory, "examples-to-run.py")
         if os.path.exists(examples_to_run_path):
-            examples_to_run_file = open(examples_to_run_path, "r")
-
-            # Look for the list of examples to run.
-            examples_to_run_line = ""
-            parsing_multiline_list = False
-            for line in examples_to_run_file:
-                if 'examples_to_run' in line or parsing_multiline_list:
-                    examples_to_run_line += line
-
-                    # Handle multiline lists.
-                    if ']' not in examples_to_run_line:
-                        parsing_multiline_list = True
-                    else:
-                        parsing_multiline_list = False
 
-                        # Evaluate the line once the end of the list is
-                        # reached.
-                        examples_to_run = eval(examples_to_run_line.split('=')[1].strip())
+            # Get the list of examples to run.
+            examples_to_run = get_list_from_file(examples_to_run_path, "examples_to_run")
+            if examples_to_run:
+                for example_name, do_run, do_valgrind_run in examples_to_run:
+                    example_path = os.path.join(
+                        NS3_BUILDDIR, NS3_ACTIVE_VARIANT, 
+                        "examples", example_directory, example_name)
+
+                    # Add all of the examples that were built to the list of
+                    # examples to run.
+                    if os.path.exists(example_path):
+                        example_tests.append((example_path, do_run, do_valgrind_run))
     
-                        # Add all of the examples that were built to the list of
-                        # examples to run.
-                        for example_name, do_run, do_valgrind_run in examples_to_run:
-                            example_path = os.path.join(
-                                NS3_BUILDDIR, NS3_ACTIVE_VARIANT, 
-                                "examples", example_directory, example_name)
-                            if os.path.exists(example_path):
-                                example_tests.append((example_path, do_run, do_valgrind_run))
-    
-                        break
-
-            examples_to_run_file.close()
-
     #
     # If lots of logging is enabled, we can crash Python when it tries to 
     # save all of the text.  We just don't allow logging to be turned on when
--- a/util.py	Fri Dec 10 16:25:06 2010 -0800
+++ b/util.py	Mon Dec 13 13:09:46 2010 -0800
@@ -5,6 +5,43 @@
     print >> sys.stderr, msg
     sys.exit(2)
 
+
+def get_list_from_file(file_path, list_name):
+    '''Looks for a Python list called list_name in the file specified
+    by file_path and returns it.
+
+    If the file or list name aren't found, this function will return
+    an empty list.
+
+    '''
+
+    list = []
+
+    # Read in the file if it exists.
+    if os.path.exists(file_path):
+        file_in = open(file_path, "r")
+
+        # Look for the list.
+        list_string = ""
+        parsing_multiline_list = False
+        for line in file_in:
+            if list_name in line or parsing_multiline_list:
+                list_string += line
+
+                # Handle multiline lists.
+                if ']' not in list_string:
+                    parsing_multiline_list = True
+                else:
+                    # Evaluate the list once its end is reached.
+                    list = eval(list_string.split('=')[1].strip())
+                    break
+
+        # Close the file
+        file_in.close()
+
+    return list
+
+
 def read_config_file():
     '''Reads the NS-3 configuration file and returns a list of enabled modules.
 
@@ -24,14 +61,8 @@
             fatal("NS-3 configuration file (" + dot_ns3rc_name + ") required but not found.")
 
     # Read in the ns3 configuration file.
-    dot_ns3rc_file = open(dot_ns3rc_path, "r")
-    modules_enabled = None
-    for line in dot_ns3rc_file:
-        if 'modules_enabled' in line:
-            modules_enabled = eval(line.split('=')[1].strip())
-            dot_ns3rc_file.close()
-            break
-    if modules_enabled is None:
+    modules_enabled = get_list_from_file(dot_ns3rc_path, 'modules_enabled')
+    if not modules_enabled:
         fatal("The enabled modules must be set in the NS-3 configuration file (" + dot_ns3rc_name + ").")
 
     return modules_enabled