utils.py
author Ashwin Narayan
Sat, 16 Jul 2011 09:43:24 -0400
changeset 7330 5d5418812534
parent 7063 9f950840670b
permissions -rw-r--r--
Renaming MonitorMacHigh to MonitorWifiMac
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
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    47
def get_bool_from_file(file_path, bool_name, value_if_missing):
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    48
    '''Looks for a Python boolean variable called bool_name in the
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    49
    file specified by file_path and returns its value.
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    50
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    51
    If the file or boolean variable aren't found, this function will
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    52
    return value_if_missing.
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    53
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    54
    '''
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    55
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    56
    # Read in the file if it exists.
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    57
    if os.path.exists(file_path):
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    58
        file_in = open(file_path, "r")
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    59
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    60
        # Look for the boolean variable.
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    61
        bool_found = False
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    62
        for line in file_in:
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    63
            if bool_name in line:
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    64
                # Evaluate the variable's line once it is found.  Make
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    65
                # the split function only split it once.
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    66
                bool = eval(line.split('=', 1)[1].strip())
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    67
                bool_found = True
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    68
                break
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    69
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    70
        # Close the file
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    71
        file_in.close()
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    72
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    73
    if bool_found:
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    74
        return bool
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    75
    else:
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    76
        return value_if_missing
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    77
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    78
7063
9f950840670b Make function's docstring be comments to fix waf help message
Mitch Watrous <watrous@u.washington.edu>
parents: 7024
diff changeset
    79
# Reads the NS-3 configuration file and returns a list of enabled modules.
9f950840670b Make function's docstring be comments to fix waf help message
Mitch Watrous <watrous@u.washington.edu>
parents: 7024
diff changeset
    80
#
9f950840670b Make function's docstring be comments to fix waf help message
Mitch Watrous <watrous@u.washington.edu>
parents: 7024
diff changeset
    81
# This function first looks for the ns3 configuration file (.ns3rc) in
9f950840670b Make function's docstring be comments to fix waf help message
Mitch Watrous <watrous@u.washington.edu>
parents: 7024
diff changeset
    82
# the current working directory and then looks in the ~ directory.
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    83
def read_config_file():
7024
4392d52b3536 Make examples not be built by default
Mitch Watrous <watrous@u.washington.edu>
parents: 7023
diff changeset
    84
    # By default, all modules will be enabled, examples will be disabled,
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    85
    # and tests will be disabled.
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    86
    modules_enabled  = ['all_modules']
7024
4392d52b3536 Make examples not be built by default
Mitch Watrous <watrous@u.washington.edu>
parents: 7023
diff changeset
    87
    examples_enabled = False
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    88
    tests_enabled    = False
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    89
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    90
    # 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
    91
    # directory and then look for it in the ~ directory.
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    92
    config_file_exists = False
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    93
    dot_ns3rc_name = '.ns3rc'
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    94
    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
    95
    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
    96
        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
    97
        if not os.path.exists(dot_ns3rc_path):
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    98
            # Return all of the default values if the .ns3rc file can't be found.
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    99
            return (config_file_exists, modules_enabled, examples_enabled, tests_enabled)
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
   100
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   101
    config_file_exists = True
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   102
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   103
    # Read in the enabled modules.
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
   104
    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
   105
    if not modules_enabled:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
   106
        # Enable all modules if the modules_enabled line can't be found.
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   107
        modules_enabled = ['all_modules']
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   108
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   109
    # Read in whether examples should be enabled or not.
7024
4392d52b3536 Make examples not be built by default
Mitch Watrous <watrous@u.washington.edu>
parents: 7023
diff changeset
   110
    value_if_missing = False
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   111
    examples_enabled = get_bool_from_file(dot_ns3rc_path, 'examples_enabled', value_if_missing)
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
   112
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   113
    # Read in whether tests should be enabled or not.
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   114
    value_if_missing = False
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   115
    tests_enabled = get_bool_from_file(dot_ns3rc_path, 'tests_enabled', value_if_missing)
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   116
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   117
    return (config_file_exists, modules_enabled, examples_enabled, tests_enabled)
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   118