How do I debug Trac plugins?

I am going to start quite a bit of work expanding Trac to fit our business requirements. So far I have used pythonWin and now Netbeans 6.5 as a development environment - none of them seem to allow me to debug the plugin I'm working on.

I am completely new to Python, so I probably haven’t set up the development environment, how it could be configured to debug it.

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

+4
source share
5 answers

You can create a wsgi script shell and run it in the debugger. For instance:

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 run this script in a debugger, and you can use lighttpd as an interface for a web application with a trivial configuration like this:

 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 shell in the debugger, set breakpoints in your plugin and open a web page.

+2
source

Usually unit test first.

Then we record log messages to diagnose problems.

We usually don’t depend heavily on debugging because it is often difficult to do in situations where Python scripts are embedded in a larger product.

0
source

I found that Winpdb is a decent python debugger.

But, as S. Lott points out, debuggers may not be very useful for you when your project is built into a larger one.

0
source

Trac contains good examples of Python code; using it as a guideline will help avoid errors. Just remember to check your code and do it often, since you're new to Python ... you will find that you don't need a debugger.

For unit testing, check out PyUnit .

0
source

I found it most useful to add these fancy messages during runtime as help or debug tracking, for example:

 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) 

This is sometimes more convenient than reading a magazine later. Although this only works if the function you are using has req arg.

0
source

Source: https://habr.com/ru/post/1285897/


All Articles