Question

I'm about to start a fair amount of work extending Trac to fit our business requirements. So far I've used pythonWin and now Netbeans 6.5 as the development environments - neither of these seem to provide any way of debugging the plugin I am working on.

I'm totally new to Python so probably have not set up the development environment how it could be congfigured to get it debugging.

Am I missing something obvious? It seems a bit archaic to have to resort to printing debug messages to the Trac log, which is how I'm debugging at the moment.

Was it helpful?

Solution

You can create a wrapper wsgi script and run it in a debugger. For example:

import os
import trac.web.main

os.environ['TRAC_ENV'] = '/path/to/your/trac/env'

application = trac.web.main.dispatch_request

from flup.server.fcgi import WSGIServer
server = WSGIServer(application, bindAddress=("127.0.0.1", 9000), )
server.run()

You would run this script in the debugger, and you can use lighttpd as a frontend for the web application with a trivial config like this one:

server.document-root = "/path/to/your/trac/env"
server.port = 1234
server.modules = ( "mod_fastcgi" )
server.pid-file = "/path/to/your/trac/env/httpd.pid"
server.errorlog = "/path/to/your/trac/env/error.log"
fastcgi.server = ( "/" =>
  (( "host" => "127.0.0.1",
     "port" => 9000,
     "docroot" => "/",
     "check-local" => "disable",
  ))
)

Just run the fcgi wsgi wrapper in the debugger, set the breakpoints in your plugin, and open the web page.

OTHER TIPS

Usually, we unit test first.

Then, we write log messages to diagnose problems.

We generally don't depend heavily on debugging because it's often hard to do in situations where Python scripts are embedded in a larger product.

I've found that Winpdb is a decent python debugger.

But as S.Lott points out, debuggers may not be very useful to you when your project is embedded within a larger one.

Trac contains good examples of Python code, using it as a guideline will help avoid bugs. Just be sure to test your code, and do it often since you are new to Python... You'll find you don't need a debugger.

For unit testing, check out PyUnit.

I found it most useful to add those fancy Trac messageboxes at runtime as debugging help or tracing, just like this:

from trac.web.chrome import add_notice
...
def any_function_somewhere(self, req, ...anyother args...):
    ...
    var = ...some value...
    add_notice(req, "my variable value I am tracing %s" % var)

Sometimes it's more comfortable than reading logging afterwards. Though it only works if the function you're running has that req arg.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top