Skip to content

Commit

Permalink
Gut (and purge) the anemic 'debug' module
Browse files Browse the repository at this point in the history
* lib/common.py
  (print_exception_data): Was debug.PrintException().
  (get_exception_data): Was debug.GetExceptionData().

* lib/viewvc.py
  (): Track renamed/relocated methods.
  (download_tarball): Implement the tarball debugging stuff using a
    local variable instead of referring to something in the
    (now-removed) debug.py module.

* lib/debug.py
  Remove now-empty module.

* notes/HACKING.md
  Lose reference to the debug module.
  • Loading branch information
cmpilato committed Apr 10, 2020
1 parent 3a86e59 commit d415e77
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 96 deletions.
56 changes: 56 additions & 0 deletions lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#
# -----------------------------------------------------------------------

import sys

# Special type indicators for diff header processing and idiff return codes
_RCSDIFF_IS_BINARY = 'binary-diff'
_RCSDIFF_ERROR = 'error'
Expand Down Expand Up @@ -72,3 +74,57 @@ def __str__(self):
if self.status:
return '%s: %s' % (self.status, self.msg)
return "ViewVC Unrecoverable Error: %s" % self.msg


def print_exception_data(server, exc_data):
status = exc_data['status']
msg = exc_data['msg']
tb = exc_data['stacktrace']

server.header(status=status)
server.write(b"<h3>An Exception Has Occurred</h3>\n")

s = ''
if msg:
s = '<p><pre>%s</pre></p>' % server.escape(msg)
if status:
s = s + ('<h4>HTTP Response Status</h4>\n<p><pre>\n%s</pre></p><hr />\n'
% status)
s = s.encode('utf-8', 'xmlcharrefreplace')
server.write(s)

server.write(b"<h4>Python Traceback</h4>\n<p><pre>")
server.write(server.escape(tb).encode('utf-8', 'xmlcharrefreplace'))
server.write(b"</pre></p>\n")


def get_exception_data():
# Capture the exception before doing anything else.
exc_type, exc, exc_tb = sys.exc_info()

exc_dict = {
'status' : None,
'msg' : None,
'stacktrace' : None,
}

try:
# Build a string from the formatted exception, but skipping the
# first line.
import traceback
if isinstance(exc, ViewVCException):
exc_dict['msg'] = exc.msg
exc_dict['status'] = exc.status
formatted = traceback.format_exception(exc_type, exc, exc_tb)
if exc_tb is not None:
formatted = formatted[1:]
exc_dict['stacktrace'] = ''.join(formatted)
finally:
# Prevent circular reference. The documentation for sys.exc_info
# 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, so toss it.
del exc_tb

return exc_dict
77 changes: 0 additions & 77 deletions lib/debug.py

This file was deleted.

36 changes: 20 additions & 16 deletions lib/viewvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

__version__ = '1.3.0-dev'

# this comes from our library; measure the startup time
import debug

# standard modules that we know are in the path or builtin
# Standard modules that we know are in the path or builtin.
import sys
import os
import calendar
Expand All @@ -41,8 +38,9 @@
import subprocess

# These modules come from our library (the stub has set up the path)
from common import (ViewVCException, TemplateData, _item, _RCSDIFF_NO_CHANGES,
_RCSDIFF_IS_BINARY, _RCSDIFF_ERROR)
from common import (ViewVCException, get_exception_data, print_exception_data,
_RCSDIFF_NO_CHANGES, _RCSDIFF_IS_BINARY, _RCSDIFF_ERROR,
TemplateData, _item)
import accept
import config
import ezt
Expand Down Expand Up @@ -4121,12 +4119,18 @@ def download_tarball(request):
raise ViewVCException('Tarball generation is disabled',
'403 Forbidden')

# If debugging, we just need to open up the specified tar path for
# writing. Otherwise, we get a writeable server output stream --
# disabling any default compression thereupon -- and wrap that in
# our own gzip stream wrapper.
if debug.TARFILE_PATH:
fp = open(debug.TARFILE_PATH, 'wb')
# Set DEBUG_TARFILE_PATH to a server-local path to enable tarball
# generation debugging and cause ViewVC to write the generated
# tarball (minus the compression layer) to that server filesystem
# location. This is *NOT* suitable for production environments!
#
# Otherwise, we do tarball generation as usual by getting a
# writeable server output stream -- disabling any default
# compression thereupon -- and wrapping that in our own gzip stream
# wrapper.
DEBUG_TARFILE_PATH = None
if DEBUG_TARFILE_PATH is not None:
fp = open(DEBUG_TARFILE_PATH, 'wb')
else:
tarfile = request.rootname
if request.path_parts:
Expand All @@ -4145,14 +4149,14 @@ def download_tarball(request):
fp.write(b'\0' * 1024)
fp.close()

if debug.TARFILE_PATH:
if DEBUG_TARFILE_PATH:
request.server.header('')
print("""
<html>
<body>
<p>Tarball '%s' successfully generated!</p>
</body>
</html>""" % (debug.TARFILE_PATH))
</html>""" % (DEBUG_TARFILE_PATH))


def view_revision(request):
Expand Down Expand Up @@ -5176,7 +5180,7 @@ def load_config(pathname=None, server=None):


def view_error(server, cfg):
exc_dict = debug.GetExceptionData()
exc_dict = get_exception_data()
status = exc_dict['status']
if exc_dict['msg']:
exc_dict['msg'] = server.escape(exc_dict['msg'])
Expand All @@ -5195,7 +5199,7 @@ def view_error(server, cfg):

# Fallback to the old exception printer if no configuration is
# available, or if something went wrong.
debug.PrintException(server, exc_dict)
print_exception_data(server, exc_dict)

def main(server, cfg):
try:
Expand Down
3 changes: 0 additions & 3 deletions notes/HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ new features require modifications to the [ViewVC URL
schema](./docs/url-reference.html), make sure those modifications
preserve the existing functionality of all ViewVC URLs.

The `lib` subdirectory contains a module `debug.py` which you may find
useful for performance testing.

If a new file or module is added, a new line in the installer program
`viewvc-install` is required.

Expand Down

0 comments on commit d415e77

Please sign in to comment.