utils.py
author Mitch Watrous <watrous@u.washington.edu>
Wed, 09 Mar 2011 16:49:59 -0800
changeset 6886 690b7a78bbd0
child 6937 99ee62eec34c
permissions -rw-r--r--
Make test.py handle examples-to-run.py files
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
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
     3
import os
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
     4
import sys
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
     5
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
     6
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
     7
    '''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
     8
    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
     9
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    10
    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
    11
    an empty list.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    12
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
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    15
    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
    # 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
    18
    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
    19
        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
    20
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    21
        # Look for the list.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    22
        list_string = ""
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    23
        parsing_multiline_list = False
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    24
        for line in file_in:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    25
            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
    26
                list_string += line
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    27
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    28
                # Handle multiline lists.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    29
                if ']' not in list_string:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    30
                    parsing_multiline_list = True
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    31
                else:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    32
                    # 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
    33
                    # 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
    34
                    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
    35
                    break
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    36
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    37
        # Close the file
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    38
        file_in.close()
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    39
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    40
    return list
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    41
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    42
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    43
def read_config_file():
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    44
    '''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
    45
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    46
    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
    47
    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
    48
    directory.
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
    '''
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    51
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    52
    # 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
    53
    # 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
    54
    dot_ns3rc_name = '.ns3rc'
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    55
    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
    56
    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
    57
        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
    58
        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
    59
            # 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
    60
            return ['all_modules']
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    61
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    62
    # 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
    63
    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
    64
    if not modules_enabled:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    65
        # 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
    66
        return ['all_modules']
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    67
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    68
    return modules_enabled