wutils.py
author Tom Henderson <tomh@tomh.org>
Sun, 12 Apr 2009 22:40:31 -0700
changeset 4377 2a05a47dba22
parent 4137 c1303bd63eb5
child 4326 179f86838e62
permissions -rw-r--r--
Remove class Ipv4Impl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     1
import os
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     2
import os.path
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     3
import sys
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     4
import pproc as subprocess
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     5
import shlex
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
     6
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
     7
# WAF modules
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     8
import ccroot
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
     9
import Options
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    10
import Utils
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    11
import Logs
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    12
import TaskGen
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    13
import Build
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    14
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    15
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    16
# these are set from the main wscript file
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    17
APPNAME=None
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    18
VERSION=None
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    19
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    20
#
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    21
# The last part of the path name to use to find the regression traces tarball.
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    22
# path will be APPNAME + '-' + VERSION + REGRESSION_SUFFIX + TRACEBALL_SUFFIX,
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    23
# e.g., ns-3-dev-ref-traces.tar.bz2
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    24
#
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    25
TRACEBALL_SUFFIX = ".tar.bz2"
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    26
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    27
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    28
4119
9ffcf2400aa5 Bug #323: waf --valgrind doesn't check for valgrind first
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4110
diff changeset
    29
def get_command_template(env, arguments=()):
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    30
    if Options.options.valgrind:
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    31
        if Options.options.command_template:
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    32
            raise Utils.WafError("Options --command-template and --valgrind are conflicting")
4119
9ffcf2400aa5 Bug #323: waf --valgrind doesn't check for valgrind first
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4110
diff changeset
    33
        if not env['VALGRIND']:
9ffcf2400aa5 Bug #323: waf --valgrind doesn't check for valgrind first
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4110
diff changeset
    34
            raise Utils.WafError("valgrind is not installed")
9ffcf2400aa5 Bug #323: waf --valgrind doesn't check for valgrind first
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4110
diff changeset
    35
        cmd = env['VALGRIND'] + " --leak-check=full --error-exitcode=1 %s"
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    36
    else:
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    37
        cmd = Options.options.command_template or '%s'
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    38
    for arg in arguments:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    39
        cmd = cmd + " " + arg
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    40
    return cmd
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    41
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    42
4108
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    43
if hasattr(os.path, "relpath"):
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    44
    relpath = os.path.relpath # since Python 2.6
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    45
else:
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    46
    def relpath(path, start=os.path.curdir):
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    47
        """Return a relative version of a path"""
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    48
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    49
        if not path:
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    50
            raise ValueError("no path specified")
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    51
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    52
        start_list = os.path.abspath(start).split(os.path.sep)
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    53
        path_list = os.path.abspath(path).split(os.path.sep)
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    54
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    55
        # Work out how much of the filepath is shared by start and path.
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    56
        i = len(os.path.commonprefix([start_list, path_list]))
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    57
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    58
        rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    59
        if not rel_list:
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    60
            return os.path.curdir
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    61
        return os.path.join(*rel_list)
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    62
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    63
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    64
def find_program(program_name, env):
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    65
    launch_dir = os.path.abspath(Options.cwd_launch)
4108
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    66
    top_dir = os.path.abspath(Options.launch_dir)
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    67
    found_programs = []
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    68
    for obj in Build.bld.all_task_gen:
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    69
        if not getattr(obj, 'is_ns3_program', False):
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    70
            continue
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    71
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    72
        ## filter out programs not in the subtree starting at the launch dir
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    73
        if not (obj.path.abspath().startswith(launch_dir)
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    74
                or obj.path.abspath(env).startswith(launch_dir)):
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    75
            continue
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    76
        
4108
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    77
        name1 = obj.target
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    78
        name2 = os.path.join(relpath(obj.path.abspath(), top_dir), obj.target)
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    79
        names = [name1, name2]
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    80
        found_programs.extend(names)
92bae583f934 waf --run now takes either full path to a program or just the (flat) program name, for all C++ programs
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4064
diff changeset
    81
        if program_name in names:
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    82
            return obj
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    83
    raise ValueError("program '%s' not found; available programs are: %r"
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    84
                     % (program_name, found_programs))
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    85
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    86
def get_proc_env(os_env=None):
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    87
    env = Build.bld.env
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    88
    if sys.platform == 'linux2':
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    89
        pathvar = 'LD_LIBRARY_PATH'
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    90
    elif sys.platform == 'darwin':
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    91
        pathvar = 'DYLD_LIBRARY_PATH'
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    92
    elif sys.platform == 'win32':
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    93
        pathvar = 'PATH'
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    94
    elif sys.platform == 'cygwin':
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    95
        pathvar = 'PATH'
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    96
    elif sys.platform.startswith('freebsd'):
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    97
        pathvar = 'LD_LIBRARY_PATH'
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    98
    else:
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
    99
        Logs.warn(("Don't know how to configure "
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   100
                        "dynamic library path for the platform %r;"
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   101
                        " assuming it's LD_LIBRARY_PATH.") % (sys.platform,))
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   102
        pathvar = 'LD_LIBRARY_PATH'        
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   103
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   104
    proc_env = dict(os.environ)
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   105
    if os_env is not None:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   106
        proc_env.update(os_env)
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   107
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   108
    if pathvar is not None:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   109
        if pathvar in proc_env:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   110
            proc_env[pathvar] = os.pathsep.join(list(env['NS3_MODULE_PATH']) + [proc_env[pathvar]])
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   111
        else:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   112
            proc_env[pathvar] = os.pathsep.join(list(env['NS3_MODULE_PATH']))
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   113
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   114
    pymoddir = Build.bld.path.find_dir('bindings/python').abspath(env)
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   115
    if 'PYTHONPATH' in proc_env:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   116
        proc_env['PYTHONPATH'] = os.pathsep.join([pymoddir] + [proc_env['PYTHONPATH']])
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   117
    else:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   118
        proc_env['PYTHONPATH'] = pymoddir
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   119
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   120
    return proc_env
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   121
4110
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   122
def run_argv(argv, os_env=None, cwd=None):
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   123
    proc_env = get_proc_env(os_env)
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   124
    #env = Build.bld.env
4110
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   125
    retval = subprocess.Popen(argv, env=proc_env, cwd=cwd).wait()
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   126
    if retval:
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   127
        raise Utils.WafError("Command %s exited with code %i" % (argv, retval))
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   128
    return retval
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   129
3933
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   130
def get_run_program(program_string, command_template=None):
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   131
    """
3933
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   132
    Return the program name and argv of the process that would be executed by
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   133
    run_program(program_string, command_template).
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   134
    """
3933
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   135
    #print "get_run_program_argv(program_string=%r, command_template=%r)" % (program_string, command_template)
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   136
    env = Build.bld.env
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   137
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   138
    if command_template in (None, '%s'):
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   139
        argv = shlex.split(program_string)
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   140
        program_name = argv[0]
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   141
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   142
        try:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   143
            program_obj = find_program(program_name, env)
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   144
        except ValueError, ex:
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   145
            raise Utils.WafError(str(ex))
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   146
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   147
        program_node = program_obj.path.find_or_declare(ccroot.get_target_name(program_obj))
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   148
        #try:
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   149
        #    program_node = program_obj.path.find_build(ccroot.get_target_name(program_obj))
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   150
        #except AttributeError:
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   151
        #    raise Utils.WafError("%s does not appear to be a program" % (program_name,))
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   152
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   153
        execvec = [program_node.abspath(env)] + argv[1:]
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   154
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   155
    else:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   156
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   157
        program_name = program_string
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   158
        try:
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   159
            program_obj = find_program(program_name, env)
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   160
        except ValueError, ex:
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   161
            raise Utils.WafError(str(ex))
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   162
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   163
        program_node = program_obj.path.find_or_declare(ccroot.get_target_name(program_obj))
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   164
        #try:
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   165
        #    program_node = program_obj.path.find_build(ccroot.get_target_name(program_obj))
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   166
        #except AttributeError:
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   167
        #    raise Utils.WafError("%s does not appear to be a program" % (program_name,))
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   168
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   169
        execvec = shlex.split(command_template % (program_node.abspath(env),))
3933
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   170
    return program_name, execvec
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   171
4110
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   172
def run_program(program_string, command_template=None, cwd=None):
3933
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   173
    """
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   174
    if command_template is not None, then program_string == program
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   175
    name and argv is given by command_template with %s replaced by the
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   176
    full path to the program.  Else, program_string is interpreted as
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   177
    a shell command with first name being the program name.
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   178
    """
3c149230e98a Fix waf problem running programs with arguments.
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3866
diff changeset
   179
    dummy_program_name, execvec = get_run_program(program_string, command_template)
4110
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   180
    if cwd is None:
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   181
        if (Options.options.cwd_launch):
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   182
            cwd = Options.options.cwd_launch
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   183
        else:
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   184
            cwd = Options.cwd_launch
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   185
    return run_argv(execvec, cwd=cwd)
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   186
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   187
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   188
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   189
def run_python_program(program_string):
4064
10222f483860 Upgrade to new WAF, work in progress
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 3933
diff changeset
   190
    env = Build.bld.env
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   191
    execvec = shlex.split(program_string)
4137
c1303bd63eb5 Fix waf --pyrun bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4119
diff changeset
   192
    if (Options.options.cwd_launch):
c1303bd63eb5 Fix waf --pyrun bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4119
diff changeset
   193
        cwd = Options.options.cwd_launch
c1303bd63eb5 Fix waf --pyrun bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4119
diff changeset
   194
    else:
c1303bd63eb5 Fix waf --pyrun bug
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4119
diff changeset
   195
        cwd = Options.cwd_launch
4110
04170734fa8b Don't abuse os.chdir so much (not thread safe)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 4108
diff changeset
   196
    return run_argv([env['PYTHON']] + execvec, cwd=cwd)
3866
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   197
9e946fee902c Refactor wscript code to move regression testing code to a separate python module (bug 325)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
   198