Exemple #1
0
def is_above_limit(value,
                   limit,
                   eps=1.e-6,
                   info_high_eps=None,
                   out=Default,
                   info_prefix="INFO HIGH VALUE: "):
    if (isinstance(value, (int, float)) and value > limit - eps):
        if (info_high_eps is not None and value > limit + info_high_eps):
            if (out is not None):
                if (out is Default): out = sys.stdout
                introspection.show_stack(frames_back=1,
                                         reverse=True,
                                         out=out,
                                         prefix=info_prefix)
                print(
                    "%sis_above_limit(value=%s, limit=%s, info_high_eps=%s)" %
                    (info_prefix, str(value), str(limit), str(info_high_eps)),
                    file=out)
        return True
    if (out is not None):
        if (out is Default): out = sys.stdout
        print("ERROR:", \
          "is_above_limit(value=%s, limit=%s, eps=%s)" % (
            str(value), str(limit), str(eps)), file=out)
    return False
Exemple #2
0
def show_exception_info_if_full_testing(prefix="EXCEPTION_INFO: "):
    import libtbx.load_env
    if (not libtbx.env.full_testing and not disable_tracebacklimit):
        return
    from libtbx import introspection
    from cStringIO import StringIO
    sio = StringIO()
    introspection.show_stack(out=sio)
    traceback.print_exc(file=sio)
    msg = "\n".join([prefix + line
                     for line in sio.getvalue().splitlines()]) + "\n"
    del sio
    done = []
    for out in [sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__]:

        def is_done():
            for o in done:
                if (o is out): return True
            return False

        if (is_done()): continue
        out.write(msg)
        flush = getattr(out, "flush", None)
        if (flush is not None): flush()
        done.append(out)
    return msg
Exemple #3
0
def is_below_limit(
      value,
      limit,
      eps=1.e-6,
      info_low_eps=None,
      out=Default,
      info_prefix="INFO LOW VALUE: "):
  if (isinstance(value, (int, float)) and value < limit + eps):
    if (info_low_eps is not None and value < limit - info_low_eps):
      if (out is not None):
        if (out is Default): out = sys.stdout
        introspection.show_stack(
          frames_back=1, reverse=True, out=out, prefix=info_prefix)
        print >> out, \
          "%sis_below_limit(value=%s, limit=%s, info_low_eps=%s)" % (
            info_prefix, str(value), str(limit), str(info_low_eps))
    return True
  if (out is not None):
    if (out is Default): out = sys.stdout
    print >> out, "ERROR:", \
      "is_below_limit(value=%s, limit=%s, eps=%s)" % (
        str(value), str(limit), str(eps))
  return False
Exemple #4
0
def is_above_limit(
      value,
      limit,
      eps=1.e-6,
      info_high_eps=None,
      out=Default,
      info_prefix="INFO HIGH VALUE: "):
  if (isinstance(value, (int, float)) and value > limit - eps):
    if (info_high_eps is not None and value > limit + info_high_eps):
      if (out is not None):
        if (out is Default): out = sys.stdout
        introspection.show_stack(
          frames_back=1, reverse=True, out=out, prefix=info_prefix)
        print >> out, \
          "%sis_above_limit(value=%s, limit=%s, info_high_eps=%s)" % (
            info_prefix, str(value), str(limit), str(info_high_eps))
    return True
  if (out is not None):
    if (out is Default): out = sys.stdout
    print >> out, "ERROR:", \
      "is_above_limit(value=%s, limit=%s, eps=%s)" % (
        str(value), str(limit), str(eps))
  return False
Exemple #5
0
def show_exception_info_if_full_testing(prefix="EXCEPTION_INFO: "):
  import libtbx.load_env
  if (    not libtbx.env.full_testing
      and not disable_tracebacklimit):
    return
  from libtbx import introspection
  from cStringIO import StringIO
  sio = StringIO()
  introspection.show_stack(out=sio)
  traceback.print_exc(file=sio)
  msg = "\n".join([prefix+line for line in sio.getvalue().splitlines()]) + "\n"
  del sio
  done = []
  for out in [sys.stdout, sys.stderr, sys.__stdout__, sys.__stderr__]:
    def is_done():
      for o in done:
        if (o is out): return True
      return False
    if (is_done()): continue
    out.write(msg)
    flush = getattr(out, "flush", None)
    if (flush is not None): flush()
    done.append(out)
  return msg
Exemple #6
0
def run_command(command,
                verbose=0,
                buffered=True,
                log_file_name=None,
                stdout_file_name=None,
                result_file_names=[],
                show_diff_log_stdout=False,
                sorry_expected=False,
                join_stdout_stderr=False):
    """\
This function starts another process to run command, with some
pre-call and post-call processing.
Before running command, the expected output files are removed:

  log_file_name
  stdout_file_name
  result_file_names

After command is finished, log_file_name and stdout_file_name are scanned
for Traceback and Sorry. An exception is raised if there are any
matches. sorry_expected=True suppresses the scanning for Sorry.

With buffered=True easy_run.fully_buffered() is used. If there
is any output to stderr of the child process, an exception is
raised. The run_command() return value is the result of the
easy_run.fully_buffered() call.

With buffered=False easy_run.call() is used. I.e. stdout and stderr
of the command are connected to stdout and stderr of the parent
process. stderr is not checked. The run_command() return value is None.

It is generally best to use buffered=True, and even better not to
use this function at all if command is another Python script. It
is better to organize the command script such that it can be called
directly from within the same Python process running the unit tests.
"""
    assert verbose >= 0
    if (verbose > 0):
        print(command)
        print()
        show_command_if_error = None
    else:
        show_command_if_error = command
    all_file_names = [log_file_name, stdout_file_name] + result_file_names
    for file_name in all_file_names:
        if (file_name is None): continue
        if (os.path.isfile(file_name)): os.remove(file_name)
        if (os.path.exists(file_name)):
            raise RunCommandError("Unable to remove file: %s" %
                                  show_string(file_name))
    if (buffered):
        sys.stdout.flush()
        sys.stderr.flush()
        cmd_result = easy_run.fully_buffered(
            command=command, join_stdout_stderr=join_stdout_stderr)
        if (len(cmd_result.stderr_lines) != 0):
            if (verbose == 0):
                print(command)
                print()
            print("\n".join(cmd_result.stdout_lines))
            cmd_result.raise_if_errors()
        _check_command_output(lines=cmd_result.stdout_lines,
                              show_command_if_error=show_command_if_error,
                              sorry_expected=sorry_expected)
    else:
        easy_run.call(command=command)
        cmd_result = None
    for file_name in [log_file_name, stdout_file_name]:
        if (file_name is None or not os.path.isfile(file_name)): continue
        _check_command_output(file_name=file_name,
                              show_command_if_error=show_command_if_error,
                              sorry_expected=sorry_expected)
    for file_name in all_file_names:
        if (file_name is None): continue
        if (not os.path.isfile(file_name)):
            raise RunCommandError("Missing output file: %s" %
                                  show_string(file_name))
    if (verbose > 1 and cmd_result is not None):
        print("\n".join(cmd_result.stdout_lines))
        print()
    if (show_diff_log_stdout and log_file_name is not None
            and stdout_file_name is not None):
        if (verbose > 0):
            print("diff %s %s" %
                  (show_string(log_file_name), show_string(stdout_file_name)))
            print()
        if (show_diff(
                open(log_file_name).read(),
                open(stdout_file_name).read())):
            introspection.show_stack(frames_back=1,
                                     reverse=True,
                                     prefix="INFO_LOG_STDOUT_DIFFERENCE: ")
            print("ERROR_LOG_STDOUT_DIFFERENCE")
    sys.stdout.flush()
    return cmd_result
Exemple #7
0
def run_command(
      command,
      verbose=0,
      buffered=True,
      log_file_name=None,
      stdout_file_name=None,
      result_file_names=[],
      show_diff_log_stdout=False,
      sorry_expected=False,
      join_stdout_stderr=False):
  """\
This function starts another process to run command, with some
pre-call and post-call processing.
Before running command, the expected output files are removed:

  log_file_name
  stdout_file_name
  result_file_names

After command is finished, log_file_name and stdout_file_name are scanned
for Traceback and Sorry. An exception is raised if there are any
matches. sorry_expected=True suppresses the scanning for Sorry.

With buffered=True easy_run.fully_buffered() is used. If there
is any output to stderr of the child process, an exception is
raised. The run_command() return value is the result of the
easy_run.fully_buffered() call.

With buffered=False easy_run.call() is used. I.e. stdout and stderr
of the command are connected to stdout and stderr of the parent
process. stderr is not checked. The run_command() return value is None.

It is generally best to use buffered=True, and even better not to
use this function at all if command is another Python script. It
is better to organize the command script such that it can be called
directly from within the same Python process running the unit tests.
"""
  assert verbose >= 0
  if (verbose > 0):
    print command
    print
    show_command_if_error = None
  else:
    show_command_if_error = command
  all_file_names = [log_file_name, stdout_file_name] + result_file_names
  for file_name in all_file_names:
    if (file_name is None): continue
    if (os.path.isfile(file_name)): os.remove(file_name)
    if (os.path.exists(file_name)):
      raise RunCommandError(
        "Unable to remove file: %s" % show_string(file_name))
  if (buffered):
    sys.stdout.flush()
    sys.stderr.flush()
    cmd_result = easy_run.fully_buffered(
      command=command,
      join_stdout_stderr=join_stdout_stderr)
    if (len(cmd_result.stderr_lines) != 0):
      if (verbose == 0):
        print command
        print
      print "\n".join(cmd_result.stdout_lines)
      cmd_result.raise_if_errors()
    _check_command_output(
      lines=cmd_result.stdout_lines,
      show_command_if_error=show_command_if_error,
      sorry_expected=sorry_expected)
  else:
    easy_run.call(command=command)
    cmd_result = None
  for file_name in [log_file_name, stdout_file_name]:
    if (file_name is None or not os.path.isfile(file_name)): continue
    _check_command_output(
      file_name=file_name,
      show_command_if_error=show_command_if_error,
      sorry_expected=sorry_expected)
  for file_name in all_file_names:
    if (file_name is None): continue
    if (not os.path.isfile(file_name)):
      raise RunCommandError(
        "Missing output file: %s" % show_string(file_name))
  if (verbose > 1 and cmd_result is not None):
    print "\n".join(cmd_result.stdout_lines)
    print
  if (    show_diff_log_stdout
      and log_file_name is not None
      and stdout_file_name is not None):
    if (verbose > 0):
      print "diff %s %s" % (show_string(log_file_name),
                            show_string(stdout_file_name))
      print
    if (show_diff(open(log_file_name).read(), open(stdout_file_name).read())):
      introspection.show_stack(
        frames_back=1, reverse=True, prefix="INFO_LOG_STDOUT_DIFFERENCE: ")
      print "ERROR_LOG_STDOUT_DIFFERENCE"
  sys.stdout.flush()
  return cmd_result