Add parsing of ns-3 configuration file
authorMitch Watrous <watrous@u.washington.edu>
Fri, 04 Mar 2011 15:41:14 -0800
changeset 6857 d7db3cee241f
parent 6856 298dbc966811
child 6858 2580a2dc2780
Add parsing of ns-3 configuration file
util.py
utils/.ns3rc
wscript
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util.py	Fri Mar 04 15:41:14 2011 -0800
@@ -0,0 +1,69 @@
+import os
+import sys
+
+def fatal(msg):
+    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.
+                    # Make the split function only split it once.
+                    list = eval(list_string.split('=', 1)[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.
+
+    This function first looks for the ns3 configuration file (.ns3rc)
+    in the current working directory and then looks in the ~
+    directory.
+
+    '''
+
+    # See if the ns3 configuration file exists in the current working
+    # directory and then look for it in the ~ directory.
+    dot_ns3rc_name = '.ns3rc'
+    dot_ns3rc_path = dot_ns3rc_name
+    if not os.path.exists(dot_ns3rc_path):
+        dot_ns3rc_path = os.path.expanduser('~/') + dot_ns3rc_name
+        if not os.path.exists(dot_ns3rc_path):
+            # Enable all modules if the .ns3rc file can't be found.
+            return ['all_modules']
+
+    # Read in the ns3 configuration file.
+    modules_enabled = get_list_from_file(dot_ns3rc_path, 'modules_enabled')
+    if not modules_enabled:
+        # Enable all modules if the modules_enabled line can't be found.
+        return ['all_modules']
+
+    return modules_enabled
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/utils/.ns3rc	Fri Mar 04 15:41:14 2011 -0800
@@ -0,0 +1,7 @@
+#! /usr/bin/env python
+
+# A list of the modules that will be enabled when ns-3 is run.
+# Modules that depend on the listed modules will be enabled also.
+#
+# All modules can be enabled by choosing 'all_modules'.
+modules_enabled = ['all_modules']
--- a/wscript	Fri Mar 04 19:14:23 2011 +0000
+++ b/wscript	Fri Mar 04 15:41:14 2011 -0800
@@ -28,6 +28,14 @@
 import Configure
 import Scripting
 
+from util import read_config_file
+
+# By default, all modules will be enabled.
+modules_enabled = ['all_modules']
+
+# Get the list of enabled modules out of the NS-3 configuration file.
+modules_enabled = read_config_file()
+
 sys.path.insert(0, os.path.abspath('waf-tools'))
 try:
     import cflags # override the build profiles from waf
@@ -274,9 +282,21 @@
     conf.sub_config('src')
     conf.sub_config('bindings/python')
 
+    # Set the list of enabled modules.
     if Options.options.enable_modules:
+        # Use the modules explicitly enabled. 
         conf.env['NS3_ENABLED_MODULES'] = ['ns3-'+mod for mod in
                                            Options.options.enable_modules.split(',')]
+    else:
+        # Use the enabled modules list from the ns3 configuration file.
+        if modules_enabled[0] == 'all_modules':
+            # Enable all modules if requested.
+            conf.env['NS3_ENABLED_MODULES'] = conf.env['NS3_MODULES']
+        else:
+            # Enable the modules from the list.
+            conf.env['NS3_ENABLED_MODULES'] = ['ns3-'+mod for mod in
+                                               modules_enabled]
+
     # for MPI
     conf.find_program('mpic++', var='MPI')
     if Options.options.enable_mpi and conf.env['MPI']:
@@ -321,6 +341,13 @@
         env['ENABLE_EXAMPLES'] = False
         why_not_examples = "option --disable-examples selected"
 
+    env['EXAMPLE_DIRECTORIES'] = []
+    for dir in os.listdir('examples'):
+        if dir.startswith('.') or dir == 'CVS':
+            continue
+        if os.path.isdir(os.path.join('examples', dir)):
+            env['EXAMPLE_DIRECTORIES'].append(dir)
+
     conf.report_optional_feature("ENABLE_EXAMPLES", "Build examples and samples", env['ENABLE_EXAMPLES'], 
                                  why_not_examples)