Accessing Trac with Python from inside a Java application

In my continuous endeavor to access the Trac Wiki/Ticket system (trac.edgewall.org) from outside I came across Python to test the various methods provided by the XMLRPC plugin (trac-hacks.org/wiki/XmlRpcPlugin). The goal is a Trac NB plugin implemented  in Groovy. Before the Groovy library was finally fixed (see my blog post) I tried to use Python (or better Jython) embedded in regular Javacode foloowing the JSR 223 (jcp.org/en/jsr/detail?id=223). I spare you from the usual comparison between Python and Jython vs other JVM based dynamic languages, you will find plenty of forums and websites rambling about this. Here we just have a short walk-through to see something in the console.

First we add the Jython plugin to Netbeans (Tools|Plugin)

Jython plugin

Create a simple java console application

New Java Project

Add the Jython library

Jython Library

Make a simple test

public static void main(String[] args) {
PythonInterpreter interp = new PythonInterpreter();
interp.exec("import sys");
interp.set("a", new PyInteger(42));
interp.exec("print a");
interp.exec("x = 2+2");
interp.exec("print x");
 }

Fix the imports

import org.python.core.PyInteger;
import org.python.util.PythonInterpreter;

Execute the application and we will get this (only slightly exciting) result:

run:
42
4
BUILD SUCCESSFUL (total time: 1 second);

You even can access the results from the Java “host” with this

 PyObject x = interp.get("x");
 System.out.println("x: " + x);

Now, we move on the Trac access via XML-RPC by using these few lines:

interp.exec("import xmlrpclib from xmlrpclib");
interp.exec("server = xmlrpclib.ServerProxy('https://user:password@sometracserver.com/cobra/login/xmlrpc') ");
interp.exec("print server.wiki.getPage('TracAdmin')");x);

Unfortunately the console serves us this error

run:
Exception in thread "main" Traceback (most recent call last):
 File "<string>", line 1, in <module>
ImportError: No module named xmlrpclib
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)

The XML-RPC is obviously not in the standard Standard but in the extra modules (that you find in you Netbeans folder under python1\jython2.5.1\lib). Adding that file to our classpath alone would not fix the problems due to the dependencies. Short-cut: Add all the .py  (aifc.py to zlib.py) files from the lib-folder into a new lib folder inside our java application lib-folder. (You dont need to add the subfolders from the jython and you dont need all the py files, but it is easier than digging out the depending ones.

Lib folder

Execute again, and there we go, Trac talks back ! (inclusive of a deprecation warning, which I leave to you to fix)

Trac

I recommend to explore the various methods to access your wiki content, create entries and manage tickets.

More about the NB plugin soon.

Reference and more information:

Complete NB Project on kenai: https://svn.kenai.com/svn/javadudes-toolshop~tracpythontest

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s