try and make test.py a bit more portable
authorCraig Dowell <craigdo@ee.washington.edu>
Mon Oct 26 23:44:04 2009 -0700 (3 months ago)
changeset 55053c240ed38d99
parent 5504 15fcd24f8b1e
child 5506 7c6f8ebf4a34
try and make test.py a bit more portable
test.py
     1.1 --- a/test.py	Mon Oct 26 14:50:38 2009 -0700
     1.2 +++ b/test.py	Mon Oct 26 23:44:04 2009 -0700
     1.3 @@ -533,37 +533,68 @@
     1.4  # path -- it is cooked up dynamically, so we do that too.
     1.5  #
     1.6  def make_library_path():
     1.7 -    global LIBRARY_PATH
     1.8 +    have_DYLD_LIBRARY_PATH = False
     1.9 +    have_LD_LIBRARY_PATH = False
    1.10 +    have_PATH = False
    1.11  
    1.12 -    LIBRARY_PATH = "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:'"        
    1.13 +    keys = os.environ.keys()
    1.14 +    for key in keys:
    1.15 +        if key == "DYLD_LIBRARY_PATH":
    1.16 +            have_DYLD_LIBRARY_PATH = True
    1.17 +        if key == "LD_LIBRARY_PATH":
    1.18 +            have_LD_LIBRARY_PATH = True
    1.19 +        if key == "PATH":
    1.20 +            have_PATH = True
    1.21  
    1.22      if sys.platform == "darwin":
    1.23 -        LIBRARY_PATH = "DYLD_LIBRARY_PATH='"
    1.24 +        if not have_DYLD_LIBRARY_PATH:
    1.25 +            os.environ["DYLD_LIBRARY_PATH"] = ""
    1.26 +        for path in NS3_MODULE_PATH:
    1.27 +            os.environ["DYLD_LIBRARY_PATH"] += ":" + path
    1.28 +        if options.verbose:
    1.29 +            print "os.environ[\"DYLD_LIBRARY_PATH\"] == %s" % os.environ["DY_LIBRARY_PATH"]
    1.30      elif sys.platform == "win32":
    1.31 -        LIBRARY_PATH = "PATH=$PATH:'"
    1.32 +        if not have_PATH:
    1.33 +            os.environ["PATH"] = ""
    1.34 +        for path in NS3_MODULE_PATH:
    1.35 +            os.environ["PATH"] += ';' + path
    1.36 +        if options.verbose:
    1.37 +            print "os.environ[\"PATH\"] == %s" % os.environ["PATH"]
    1.38      elif sys.platform == "cygwin":
    1.39 -        LIBRARY_PATH = "PATH=$PATH:'"
    1.40 -
    1.41 -    for path in NS3_MODULE_PATH:
    1.42 -        LIBRARY_PATH = LIBRARY_PATH + path + ":"
    1.43 -
    1.44 -    LIBRARY_PATH = LIBRARY_PATH + "'"
    1.45 -
    1.46 -    if options.verbose:
    1.47 -        print "LIBRARY_PATH == %s" % LIBRARY_PATH
    1.48 +        if not have_PATH:
    1.49 +            os.environ["PATH"] = ""
    1.50 +        for path in NS3_MODULE_PATH:
    1.51 +            os.environ["PATH"] += ":" + path
    1.52 +        if options.verbose:
    1.53 +            print "os.environ[\"PATH\"] == %s" % os.environ["PATH"]
    1.54 +    else:
    1.55 +        if not have_LD_LIBRARY_PATH:
    1.56 +            os.environ["LD_LIBRARY_PATH"] = ""
    1.57 +        for path in NS3_MODULE_PATH:
    1.58 +            os.environ["LD_LIBRARY_PATH"] += ":" + path
    1.59 +        if options.verbose:
    1.60 +            print "os.environ[\"LD_LIBRARY_PATH\"] == %s" % os.environ["LD_LIBRARY_PATH"]
    1.61  
    1.62  def run_job_synchronously(shell_command, directory, valgrind):
    1.63 +    path_cmd = os.path.join (NS3_BUILDDIR, NS3_ACTIVE_VARIANT, shell_command)
    1.64      if valgrind:
    1.65 -        cmd = "%s valgrind --leak-check=full --error-exitcode=2 %s/%s/%s" % (LIBRARY_PATH, NS3_BUILDDIR, NS3_ACTIVE_VARIANT, shell_command)
    1.66 +        cmd = "valgrind --leak-check=full --error-exitcode=2 %s" % path_cmd
    1.67      else:
    1.68 -        cmd = "%s %s/%s/%s" % (LIBRARY_PATH, NS3_BUILDDIR, NS3_ACTIVE_VARIANT, shell_command)
    1.69 +        cmd = path_cmd
    1.70  
    1.71      if options.verbose:
    1.72          print "Synchronously execute %s" % cmd
    1.73  
    1.74 -    proc = subprocess.Popen(cmd, shell=True, cwd=directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    1.75 +    start_time = time.clock()
    1.76 +    proc = subprocess.Popen(cmd, shell = True, cwd = directory, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    1.77      stdout_results, stderr_results = proc.communicate()
    1.78 -    return (proc.returncode, stdout_results, stderr_results)
    1.79 +    elapsed_time = time.clock() - start_time
    1.80 +
    1.81 +    if options.verbose:
    1.82 +        print "Return code = ", proc.returncode
    1.83 +        print "stderr = ", stderr_results
    1.84 +
    1.85 +    return (proc.returncode, stdout_results, stderr_results, elapsed_time)
    1.86  
    1.87  #
    1.88  # This class defines a unit of testing work.  It will typically refer to
    1.89 @@ -580,6 +611,7 @@
    1.90          self.cwd = ""
    1.91          self.tmp_file_name = ""
    1.92          self.returncode = False
    1.93 +        self.elapsed_time = 0
    1.94  
    1.95      #
    1.96      # A job is either a standard job or a special job indicating that a worker
    1.97 @@ -659,6 +691,12 @@
    1.98      def set_returncode(self, returncode):
    1.99          self.returncode = returncode
   1.100  
   1.101 +    #
   1.102 +    # The elapsed real time for the job execution.
   1.103 +    #
   1.104 +    def set_elapsed_time(self, elapsed_time):
   1.105 +        self.elapsed_time = elapsed_time
   1.106 +
   1.107  #
   1.108  # The worker thread class that handles the actual running of a given test.
   1.109  # Once spawned, it receives requests for work through its input_queue and
   1.110 @@ -711,17 +749,19 @@
   1.111                      # If we have an example, the shell command is all we need to
   1.112                      # know.  It will be something like "examples/udp-echo"
   1.113                      #
   1.114 -                    (job.returncode, standard_out, standard_err) = run_job_synchronously(job.shell_command, job.cwd,
   1.115 -                                                                                         options.valgrind)
   1.116 +                    (job.returncode, standard_out, standard_err, et) = run_job_synchronously(job.shell_command, 
   1.117 +                        job.cwd, options.valgrind)
   1.118                  else:
   1.119                      #
   1.120                      # If we're a test suite, we need to provide a little more info
   1.121                      # to the test runner, specifically the base directory and temp
   1.122                      # file name
   1.123                      #
   1.124 -                    (job.returncode, standard_out, standard_err) = run_job_synchronously(job.shell_command + 
   1.125 +                    (job.returncode, standard_out, standard_err, et) = run_job_synchronously(job.shell_command + 
   1.126                          " --basedir=%s --out=%s" % (job.basedir, job.tmp_file_name), job.cwd, options.valgrind)
   1.127  
   1.128 +                job.set_elapsed_time(et)
   1.129 +
   1.130                  if options.verbose:
   1.131                      print "returncode = %d" % job.returncode
   1.132                      print "---------- beign standard out ----------"
   1.133 @@ -757,9 +797,9 @@
   1.134          # to build the test-runner and can ignore all of the examples.
   1.135          #
   1.136          if options.kinds or options.list or (len(options.constrain) and options.constrain in core_kinds):
   1.137 -            proc = subprocess.Popen("./waf --target=test-runner", shell=True)
   1.138 +            proc = subprocess.Popen("waf --target=test-runner", shell = True)
   1.139          else:
   1.140 -            proc = subprocess.Popen("./waf", shell=True)
   1.141 +            proc = subprocess.Popen("waf", shell = True)
   1.142  
   1.143          proc.communicate()
   1.144  
   1.145 @@ -779,11 +819,13 @@
   1.146      # handle them without doing all of the hard work.
   1.147      #
   1.148      if options.kinds:
   1.149 -        (rc, standard_out, standard_err) = run_job_synchronously("utils/test-runner --kinds", os.getcwd(), False)
   1.150 +        path_cmd = os.path.join("utils", "test-runner --kinds")
   1.151 +        (rc, standard_out, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False)
   1.152          print standard_out
   1.153  
   1.154      if options.list:
   1.155 -        (rc, standard_out, standard_err) = run_job_synchronously("utils/test-runner --list", os.getcwd(), False)
   1.156 +        path_cmd = os.path.join("utils", "test-runner --list")
   1.157 +        (rc, standard_out, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False)
   1.158          print standard_out
   1.159  
   1.160      if options.kinds or options.list:
   1.161 @@ -854,10 +896,11 @@
   1.162          suites = options.suite + "\n"
   1.163      elif len(options.example) == 0:
   1.164          if len(options.constrain):
   1.165 -            (rc, suites, standard_err) = run_job_synchronously("utils/test-runner --list --constrain=%s" % 
   1.166 -                options.constrain, os.getcwd(), False)
   1.167 +            path_cmd = os.path.join("utils", "test-runner --list --constrain=%s" % options.constrain)
   1.168 +            (rc, suites, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False)
   1.169          else:
   1.170 -            (rc, suites, standard_err) = run_job_synchronously("utils/test-runner --list", os.getcwd(), False)
   1.171 +            path_cmd = os.path.join("utils", "test-runner --list")
   1.172 +            (rc, suites, standard_err, et) = run_job_synchronously(path_cmd, os.getcwd(), False)
   1.173      else:
   1.174          suites = ""
   1.175  
   1.176 @@ -888,13 +931,14 @@
   1.177      #
   1.178      processors = 1
   1.179  
   1.180 -    if 'SC_NPROCESSORS_ONLN'in os.sysconf_names:
   1.181 -        processors = os.sysconf('SC_NPROCESSORS_ONLN')
   1.182 -    else:
   1.183 -        proc = subprocess.Popen("sysctl -n hw.ncpu", shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   1.184 -        stdout_results, stderr_results = proc.communicate()
   1.185 -        if len(stderr_results) == 0:
   1.186 -            processors = int(stdout_results)
   1.187 +    if sys.platform != "win32":
   1.188 +        if 'SC_NPROCESSORS_ONLN'in os.sysconf_names:
   1.189 +            processors = os.sysconf('SC_NPROCESSORS_ONLN')
   1.190 +        else:
   1.191 +            proc = subprocess.Popen("sysctl -n hw.ncpu", shell = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   1.192 +            stdout_results, stderr_results = proc.communicate()
   1.193 +            if len(stderr_results) == 0:
   1.194 +                processors = int(stdout_results)
   1.195  
   1.196      #
   1.197      # Now, spin up one thread per processor which will eventually mean one test
   1.198 @@ -934,7 +978,8 @@
   1.199              else:
   1.200                  multiple = ""
   1.201  
   1.202 -            job.set_shell_command("utils/test-runner --suite='%s'%s" % (test, multiple))
   1.203 +            path_cmd = os.path.join("utils", "test-runner --suite='%s'%s" % (test, multiple))
   1.204 +            job.set_shell_command(path_cmd)
   1.205  
   1.206              if options.valgrind and test in core_valgrind_skip_tests:
   1.207                  job.set_is_skip(True)
   1.208 @@ -1108,6 +1153,7 @@
   1.209              else:
   1.210                  f.write('  <Result>CRASH</Result>\n')
   1.211  
   1.212 +            f.write('  <ElapsedTime>%s</ElapsedTime>\n' % job.elapsed_time)
   1.213              f.write('</Example>\n')
   1.214              f.close()
   1.215  
   1.216 @@ -1167,7 +1213,7 @@
   1.217              else:
   1.218                  if job.returncode == 0 or job.returncode == 1 or job.returncode == 2:
   1.219                      f_to = open(xml_results_file, 'a')
   1.220 -                    f_from = open(job.tmp_file_name, 'r')
   1.221 +                    f_from = open(job.tmp_file_name)
   1.222                      f_to.write(f_from.read())
   1.223                      f_to.close()
   1.224                      f_from.close()