# -*-python-*- # # Copyright (C) 1999-2007 The ViewCVS Group. All Rights Reserved. # # By using this file, you agree to the terms and conditions set forth in # the LICENSE.html file which can be found at the top level of the ViewVC # distribution or at http://viewvc.org/license-1.html. # # For more information, visit http://viewvc.org/ # # ----------------------------------------------------------------------- # # Note: a t_start/t_end pair consumes about 0.00005 seconds on a P3/700. # the lambda form (when debugging is disabled) should be even faster. # import sys # Set to non-zero to track and print processing times SHOW_TIMES = 0 # Set to non-zero to display child process info SHOW_CHILD_PROCESSES = 0 # Set to a server-side path to force the tarball view to generate the # tarball as a file on the server, instead of transmitting the data # back to the browser. This enables easy display of error # considitions in the browser, as well as tarball inspection on the # server. NOTE: The file will be a TAR archive, *not* gzip-compressed. TARFILE_PATH = '' if SHOW_TIMES: import time _timers = { } _times = { } def t_start(which): _timers[which] = time.time() def t_end(which): t = time.time() - _timers[which] if _times.has_key(which): _times[which] = _times[which] + t else: _times[which] = t def dump(): for name, value in _times.items(): print '%s: %.6f
' % (name, value) else: t_start = t_end = dump = lambda *args: None class ViewVCException: def __init__(self, msg, status=None): self.msg = msg self.status = status def __str__(self): if self.status: return '%s: %s' % (self.status, self.msg) return "ViewVC Unrecoverable Error: %s" % self.msg def PrintException(server, exc_data): status = exc_data['status'] msg = exc_data['msg'] tb = exc_data['stacktrace'] server.header(status=status) server.write("

An Exception Has Occurred

\n") s = '' if msg: s = '

%s

' % server.escape(msg) if status: s = s + ('

HTTP Response Status

\n

\n%s


\n' % status) server.write(s) server.write("

Python Traceback

\n

")
  server.write(server.escape(tb))
  server.write("

\n") def GetExceptionData(): # capture the exception before doing anything else exc_type, exc, exc_tb = sys.exc_info() exc_dict = { 'status' : None, 'msg' : None, 'stacktrace' : None, } try: import traceback, string if isinstance(exc, ViewVCException): exc_dict['msg'] = exc.msg exc_dict['status'] = exc.status tb = string.join(traceback.format_exception(exc_type, exc, exc_tb), '') exc_dict['stacktrace'] = tb finally: # prevent circular reference. sys.exc_info documentation warns # "Assigning the traceback return value to a local variable in a function # that is handling an exception will cause a circular reference..." # This is all based on 'exc_tb', and we're now done with it. Toss it. del exc_tb return exc_dict if SHOW_CHILD_PROCESSES: class Process: def __init__(self, command, inStream, outStream, errStream): self.command = command self.debugIn = inStream self.debugOut = outStream self.debugErr = errStream import sapi if not sapi.server is None: if not sapi.server.pageGlobals.has_key('processes'): sapi.server.pageGlobals['processes'] = [self] else: sapi.server.pageGlobals['processes'].append(self) def DumpChildren(server): import os if not server.pageGlobals.has_key('processes'): return server.header() lastOut = None i = 0 for k in server.pageGlobals['processes']: i = i + 1 server.write("\n") server.write("" % i) server.write("\n \n\n") server.write("\n \n\n") if k.debugOut is k.debugErr: server.write("\n \n\n") else: server.write("\n \n\n") server.write("\n \n\n") server.write("
Child Process%i
Command Line
")
      server.write(server.escape(k.command))
      server.write("
Standard In: ") if k.debugIn is lastOut and not lastOut is None: server.write("Output from process %i" % (i - 1)) elif k.debugIn: server.write("
")
        server.write(server.escape(k.debugIn.getvalue()))
        server.write("
") server.write("
Standard Out & Error:
")
        if k.debugOut:
          server.write(server.escape(k.debugOut.getvalue()))
        server.write("
Standard Out:
")
        if k.debugOut:
          server.write(server.escape(k.debugOut.getvalue()))
        server.write("
Standard Error:
")
        if k.debugErr:
          server.write(server.escape(k.debugErr.getvalue()))
        server.write("
\n") server.flush() lastOut = k.debugOut server.write("\n") server.write("") for k, v in os.environ.items(): server.write("\n \n \n") server.write("
Environment Variables
")
      server.write(server.escape(k))
      server.write("
")
      server.write(server.escape(v))
      server.write("
") else: def DumpChildren(server): pass