utils.py
author Mitch Watrous <watrous@u.washington.edu>
Tue, 22 Mar 2011 16:21:35 -0700
changeset 6937 99ee62eec34c
parent 6886 690b7a78bbd0
child 7023 af9c5ac72f2c
permissions -rw-r--r--
Add descriptive comment for utils.py
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
     1
#! /usr/bin/env python
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
     2
6937
99ee62eec34c Add descriptive comment for utils.py
Mitch Watrous <watrous@u.washington.edu>
parents: 6886
diff changeset
     3
# These methods are used by test.py and waf to look for and read the
99ee62eec34c Add descriptive comment for utils.py
Mitch Watrous <watrous@u.washington.edu>
parents: 6886
diff changeset
     4
# .ns3rc configuration file, which is used to specify the modules that
99ee62eec34c Add descriptive comment for utils.py
Mitch Watrous <watrous@u.washington.edu>
parents: 6886
diff changeset
     5
# should be enabled
99ee62eec34c Add descriptive comment for utils.py
Mitch Watrous <watrous@u.washington.edu>
parents: 6886
diff changeset
     6
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
     7
import os
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
     8
import sys
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
     9
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    10
def get_list_from_file(file_path, list_name):
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    11
    '''Looks for a Python list called list_name in the file specified
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    12
    by file_path and returns it.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    13
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    14
    If the file or list name aren't found, this function will return
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    15
    an empty list.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    16
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    17
    '''
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    18
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    19
    list = []
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    20
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    21
    # Read in the file if it exists.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    22
    if os.path.exists(file_path):
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    23
        file_in = open(file_path, "r")
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    24
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    25
        # Look for the list.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    26
        list_string = ""
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    27
        parsing_multiline_list = False
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    28
        for line in file_in:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    29
            if list_name in line or parsing_multiline_list:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    30
                list_string += line
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    31
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    32
                # Handle multiline lists.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    33
                if ']' not in list_string:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    34
                    parsing_multiline_list = True
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    35
                else:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    36
                    # Evaluate the list once its end is reached.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    37
                    # Make the split function only split it once.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    38
                    list = eval(list_string.split('=', 1)[1].strip())
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    39
                    break
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    40
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    41
        # Close the file
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    42
        file_in.close()
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    43
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    44
    return list
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    45
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    46
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    47
def read_config_file():
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    48
    '''Reads the NS-3 configuration file and returns a list of enabled modules.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    49
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    50
    This function first looks for the ns3 configuration file (.ns3rc)
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    51
    in the current working directory and then looks in the ~
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    52
    directory.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    53
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    54
    '''
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    55
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    56
    # See if the ns3 configuration file exists in the current working
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    57
    # directory and then look for it in the ~ directory.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    58
    dot_ns3rc_name = '.ns3rc'
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    59
    dot_ns3rc_path = dot_ns3rc_name
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    60
    if not os.path.exists(dot_ns3rc_path):
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    61
        dot_ns3rc_path = os.path.expanduser('~/') + dot_ns3rc_name
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    62
        if not os.path.exists(dot_ns3rc_path):
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    63
            # Enable all modules if the .ns3rc file can't be found.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    64
            return ['all_modules']
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    65
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    66
    # Read in the ns3 configuration file.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    67
    modules_enabled = get_list_from_file(dot_ns3rc_path, 'modules_enabled')
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    68
    if not modules_enabled:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    69
        # Enable all modules if the modules_enabled line can't be found.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    70
        return ['all_modules']
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    71
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    72
    return modules_enabled