utils.py
author Alexander Krotov <ilabdsf@yandex.ru>
Sun, 26 Jul 2015 19:44:06 +0300
changeset 11558 68e480bb7d74
parent 7865 eb55045524be
permissions -rw-r--r--
Remove ByteTagList::IsDirty{Start,End} These functions are only used once and do not save time. Both functions iterate over the whole list just to prevent iterating over the whole list if they return false. As traversing the list is required in any case, these functions can be removed.
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:
7865
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    29
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    30
            # Remove any comments.
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    31
            if '#' in line:
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    32
                (line, comment) = line.split('#', 1)
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    33
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    34
            # Parse the line.
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    35
            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
    36
                list_string += line
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    37
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    38
                # Handle multiline lists.
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    39
                if ']' not in list_string:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    40
                    parsing_multiline_list = True
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    41
                else:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    42
                    # 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
    43
                    # 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
    44
                    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
    45
                    break
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
        # Close the file
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
    48
        file_in.close()
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
    return list
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
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    53
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
    54
    '''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
    55
    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
    56
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    57
    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
    58
    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
    59
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    60
    '''
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    61
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    62
    # 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
    63
    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
    64
        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
    65
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    66
        # 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
    67
        bool_found = False
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    68
        for line in file_in:
7865
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    69
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    70
            # Remove any comments.
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    71
            if '#' in line:
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    72
                (line, comment) = line.split('#', 1)
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    73
eb55045524be Bug 1391 - .ns3rc does not allow comments as expected
Mitch Watrous <watrous@u.washington.edu>
parents: 7063
diff changeset
    74
            # Parse the line.
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    75
            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
    76
                # 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
    77
                # 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
    78
                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
    79
                bool_found = True
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    80
                break
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    81
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    82
        # Close the file
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    83
        file_in.close()
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    84
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    85
    if bool_found:
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    86
        return bool
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    87
    else:
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    88
        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
    89
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
    90
7063
9f950840670b Make function's docstring be comments to fix waf help message
Mitch Watrous <watrous@u.washington.edu>
parents: 7024
diff changeset
    91
# 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
    92
#
9f950840670b Make function's docstring be comments to fix waf help message
Mitch Watrous <watrous@u.washington.edu>
parents: 7024
diff changeset
    93
# 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
    94
# 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
    95
def read_config_file():
7024
4392d52b3536 Make examples not be built by default
Mitch Watrous <watrous@u.washington.edu>
parents: 7023
diff changeset
    96
    # 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
    97
    # 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
    98
    modules_enabled  = ['all_modules']
7024
4392d52b3536 Make examples not be built by default
Mitch Watrous <watrous@u.washington.edu>
parents: 7023
diff changeset
    99
    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
   100
    tests_enabled    = False
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   101
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
   102
    # 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
   103
    # 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
   104
    config_file_exists = False
6886
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
   105
    dot_ns3rc_name = '.ns3rc'
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
   106
    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
   107
    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
   108
        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
   109
        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
   110
            # 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
   111
            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
   112
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   113
    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
   114
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   115
    # 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
   116
    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
   117
    if not modules_enabled:
690b7a78bbd0 Make test.py handle examples-to-run.py files
Mitch Watrous <watrous@u.washington.edu>
parents:
diff changeset
   118
        # 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
   119
        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
   120
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   121
    # 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
   122
    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
   123
    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
   124
7023
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   125
    # 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
   126
    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
   127
    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
   128
af9c5ac72f2c Make examples and tests be enabled from the .ns3rc file
Mitch Watrous <watrous@u.washington.edu>
parents: 6937
diff changeset
   129
    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
   130