Frank Wierzbicki's Weblog

Jython, Python, other stuff.

Thursday, April 24, 2008

Jython and Django Progress Part II: GlassFish and modjy

In part I I showed you how to run the Django dev server from Jython. In this post I will demo Django on GlassFish. So starting with the app you had running from part I:

  1. Download a preview build of GlassFish. I am using this snapshot, but a more recent snapshot will very likely work just as well.
  2. Unzip the snapshot somewhere and put the resulting glassfish/bin in your PATH.
  3. Download modjy and unzip it somewhere (you will only need modjy.jar)
  4. Create a directory that will house your webapp. I'm calling mine "webapp".
  5. Create a WEB-INF directory under webapp and a lib directory under that.
  6. Copy jython-trunk/dist/jython.jar into webapp/WEB-INF/lib
  7. Copy the whole jython-trunk/dist/Lib directory into webapp/WEB-INF/lib so that you end up with a webapp/WEB-INF/lib/Lib directory with all Jython's .py files.
  8. Copy modjy.jar from modjy_0_22_0/modjy_webapp/WEB-INF/lib/modjy.jar into webapp/WEB-INF/lib.
  9. Copy your postgres jdbc driver jar into webapp/WEB-INF/lib
  10. Copy django-trunk/django into the webapp directory so that you end up with webapp/django/
  11. Copy your mysite directory into the webapp directory so that you end up with webapp/mysite
  12. Copy django-trunk/django/contrib/admin/media/ into glassfish/domains/domain1/docroot/ so that you end up with a glassfish/domains/domain1/docroot/media directory (this is so the admin css and js files can be found by Django).
  13. Create a webapp/WEB-INF/web.xml file like this [Update: I've added a new init-param block with param-name=python.home and param-value=the home dir of my Jython dist - Thanks to Tristan for pointing out this omission]:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">
    <web-app>

    <display-name>Django and modjy</display-name>
    <description>
    Djagno on Jython through WSGI with modjy
    </description>

    <servlet>
    <servlet-name>modjy</servlet-name>
    <servlet-class>com.xhaus.modjy.ModjyJServlet</servlet-class>
    <init-param>
    <param-name>reload_on_mod</param-name>
    <param-value>1</param-value>
    </init-param>
    <init-param>
    <param-name>python.home</param-name>
    <param-value>/path/to/jython-trunk/dist</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>modjy</servlet-name>
    <url-pattern>/*</url-pattern>
    </servlet-mapping>

    </web-app>

  14. Create a file application.py (note that it must be called application.py as this is the default that modjy expects. If you want to give it a different name see the modjy docs) directly under webapp with these contents:

    import os

    from django.core.handlers import wsgi
    os.putenv("DJANGO_SETTINGS_MODULE", "mysite.settings")

    def handler(environ, start_response):
    h = wsgi.WSGIHandler()
    return h(environ, start_response)
  15. Now fire up GlassFish by issuing the "startserv" command
  16. Then deploy our webapp with "asadmin deploy webapp"

  17. Navigate to http://localhost:8080/webapp/admin and you should see the django admin app -- note the 8080 instead of the 8000 from the dev server.
This works pretty well -- though I've noticed that there is still a redirect problem when trying to log into the admin app from GlassFish. If you log in using the dev server from part I and then try the GlassFish version, it works fine. I will look into this bug when I can, but I really wanted to get this post out since it will be a while before I can come back to this (JavaOne is too close and I am on several panels (two with major demos) and doing a one hour talk :)

Thursday, April 10, 2008

Start of an _ast Module in Jython's Trunk

Philip Jenvey has a mako branch that uses _ast for its parsing. _ast is the module, new in Python 2.5, that exposes Python's internal parsing implementation. I have been using _ast to compare my antlr work with the real Python _ast. Philip's work on mako has motivated me to start on a real _ast for Jython. I just started it, and it is not yet sufficient for the mako work, but it's getting there.

The new _ast module relies on the new antlr parser which is now in trunk. For now you must put antlr-runtime-3.0.1 (which is in the extlibs dir) in your classpath to use it. Using it looks like this:


>>> import _ast
>>> compile("a=1", "<unknown>", "exec", _ast.PyCF_ONLY_AST)
tree: (Module (Assign (Target (Name a)) (Value (Num 1))))

Which outputs a tree representation for now.

If you do not use _ast or the PyCF_ONLY_AST flag, you should not need antlr-runtime-3.0.1.jar in your path.

Tuesday, April 08, 2008

Jython and Django Progress Part I: Dev Server

Jython and Django are working better and better together. About six months ago I took a crack at it. I had a little success and put together a list of problems and some workarounds that I found. Leo Soto, Jim Baker and others did a much better job at getting Django trunk and Jython trunk working together. I am currently working on getting Django to run on GlassFish through the WSGI interface provided by modjy.

So here are the steps to get Django's dev server running on Jython:

  1. Get postgresql installed, get a postgresql jdbc driver and put it on your classpath
  2. Get Django from svn:
    svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk
  3. Get Jython from svn:
    svn co https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython jython-trunk
  4. Get patches for Django that haven't made it in yet from svn:
    svn co https://jython.svn.sourceforge.net/svnroot/jython/trunk/sandbox/jbaker/django django-patches/
  5. Copy patches over django-trunk:
    mkdir django-trunk/django/db/backends/postgresql_zxjdbc/
    cp django-patches/db/backends/postgresql_zxjdbc/*.py django-trunk/django/db/backends/postgresql_zxjdbc/
    cp django-patches/dispatch/robustapply.py django-trunk/django/dispatch/robustapply.py
  6. Create a shell or batch file to run jython with django on the path. I created the file below and named it "run"
    #!/bin/sh
    JYTHON_HOME=jython-trunk/dist
    JYTHON_JAVA_ARGS="-classpath $JYTHON_HOME/jython.jar:$CLASSPATH"
    $JAVA_HOME/bin/java $JYTHON_JAVA_ARGS -Dpython.path='django-trunk' -Dpython.home="$JYTHON_HOME" org.python.util.jython "$@"
  7. Now create a new project. This is the start of the django tutorial except that I've explicitly used my "run" script and the full path to django-admin.py
    ./run django-trunk/django/bin/django-admin.py startproject mysite
  8. Try it out! Note the --noreload directive. Reloading does not work yet.
    ./run mysite/manage.py runserver --noreload

  9. Navigate to http://localhost:8000 and you should see the django default page.
  10. Now edit mysite/settings.py. For DATABASE_ENGINE use postgresql_zxjdbc, and add 'django.contrib.admin' to the bottom of the INSTALLED_APPS as outlined in the django tutorial. Of course, also put the database username/password etc into the right spots in settings.py.
  11. Edit mysite/urls.py and uncomment the admin app line.
  12. Now sync the database
    ./run mysite/manage.py syncdb
  13. Stop and start the app again (as in step 8) and navigate to the admin app at http://localhost:8000/admin/
Now if you want you could try out the Django tutorial with Jython. If you do let me know how it goes -- I haven't tried it all yet and I have to get back to Glassfish and modjy. Leo Soto has proposed Django work for the Google Summer of Code, and Jim Baker is likely to be the project mentor, so the remaining issues should get cleared up quickly followed I expect by more amazing stuff. At the very least I or perhaps Leo will get the MySQL backend I started up and running pretty soon.

Come back for the next post -- it will be on Glassfish and modjy. I've already had some pretty good success there.

Friday, March 21, 2008

PyCon 2008 and Jython

I knew that the Python community contained fans of Jython, but at PyCon this year I was just blown away by all of the encouragement and positive feedback on Jython's recent progress. Particularly nice was the encouragement that I got from many core Python developers. The main input I get is: "When will Jython 2.5 be out?" and "How can we get our framework/app working on Jython?". It is too soon to give a solid date for Jython 2.5, but I am willing to say that it will come out in 2008. Also an alpha should come out soon -- I'm going to try to get one cut before JavaOne.

I also got plenty of congratulations on my new job at Sun. Almost everyone I talked to felt positive about this move from Sun. One individual whose experience with Sun dates back to the nineties expressed some doubt about Sun's ability to play well with open source. I can say with some confidence that, at least in the corner of Sun that I live in, they really seem to get it. As an example, read the bottom of this post from Vice President Jim Parkinson where he tells us that Ted and my roles in the open source community are not changing. It is nice to get that kind of statement from that far up the chain. For further reassurance look no further than the way Sun has handled the JRuby project.

This was the first time I met Ted Leung in person. Be sure to read his notes on PyCon 2008 as they contain much more detail than mine. Ted was hired by Sun at the same time that I was hired. His job description ranges wider than mine: He is tasked with helping Sun become a good member of the Python community. Mine of course is just to make Jython succeed, a task that is quite a bit easier to define.

I got to meet some IronPython folks, Jim Hugunin and Dino Viehland. Jim Hugunin is of course the original inventor of Jython as well as IronPython. Dino is the lead developer of IronPython. They have also managed to get Django running in their world, and they are doing some pretty cool stuff with Microsoft technologies like Silverlight. I feel for them in that they are completely forbidden from looking at even a line of CPython code, and it is fairly difficult for them to send patches to outside projects (to, for example, make some part of Django work better on IronPython). They are working with the lawyers to get them to ease up a bit, but that's tough going. I'm not sure how they can work like that... ouch.

At PyCon, Jeffrey Yasskin started an effort to get a concurrency memory model written for Python (Similar to the way Java and C++ have such models). I have high hopes that this can be accomplished as it should make it simpler to think about concurrency in Jython (Something that Jim Baker has been spending a lot of time on lately).

So on to my favorite part of PyCon: the sprinting. Last year the Jython sprint consisted of three guys at a table with me as 1/2 a sprinter while I was working with Trac. This year Jython took up a room with around 11 sprinters. Sprinters from other projects came in throughout the week asking about how to get Jython working with their projects which was very nice. A particular moment where the value of the PyCon sprints stood out to me is when brand new Jython committer Nicholas Riley was working furiously on getting parts of Twisted working on Jython while surrounded by about six Twisted core developers giving him pointers. Awesome.

A partial list of Jython sprint accomplishments:

  • Start of Twisted on Jython
  • New compiler/New parser work
  • Better threading support for Jython
  • Collaboration with SQLAlchemy to get JDBC support
  • Work on writing decimal.py as a wrapper around Java's BigDecimal
  • Work on porting mmap to jython

I'm sure I missed some of the work that was done on Jython at PyCon -- there was so much!

Friday, March 07, 2008

Jython and Google Summer of Code 2008

Google Summer of Code is starting up again! That means it is time to start thinking of gsoc projects for 2008 and time for folks to start thinking about proposals. We keep some ideas in the Jython wiki under PotentialProjects. The overview of Jython's Summer of Code participation is here. Those who think they might like to mentor a Jython project please put your name on the list of potential mentors on that page. I resurrected the Jython help system idea from Brian Zimmer. I would be very willing to mentor that one. Other ideas should go on the aforementioned PotentialProjects page.

Last summer was a great success for us, particularly the work of Tobias Ivarsson mentored by Jim Baker producing I very nice prototype for a new compiler. Let's make this one even better!

Wednesday, March 05, 2008

So Long Gary Gygax

Gary Gygax died yesterday. His games brought me lots of enjoyment as a kid. It's funny just how many programmers I've met also played those games. It's also funny just how much nostalgia I get from the cover of the first version of D & D that I owned. Thanks for the memories and the games Gary, and so long.

Monday, March 03, 2008

Jython's Future Looking Sunny

Sorry for the title. There is something about Sun that makes it almost impossible to avoid solar puns. I just didn't have sufficient willpower to resist. I will try to be stronger in the future.

So by now you've probably guessed it: Sun Microsystems has hired me to work full time on Jython. They have also hired Ted Leung to represent the wider world of Python at Sun.

I don't think I can overstate just how excited I am about this. For a long time now I've been obsessed with Jython. Now I will be able to let my obsession take over completely.

So what does this mean for Jython? First off, just in case anyone is worried: Jython is going to remain completely open source. Sun has applied for membership to the PSF and the PSF will continue to be the steward of Jython's code. This move by Sun means that Jython is going to get some of the attention that it needs to move forward.

The JRuby folks have already been working with the Jython folks to find those places where we can share code. The fact that they work for Sun was no barrier at all to cooperation, but being in the same company means that I am going to interact with them all the more. Close cooperation with JRuby has no downside for Jython whatsoever.

I have to especially thank Tim Bray, who has been pushing the dynamic language thing at Sun for quite a long time. I'm sure the warm welcome that I got at Sun was in no small part due to the enormous success of the JRuby work that has been going on there, so thanks to Charles Nutter and Thomas Enebo for their work. Also thanks to the many folks at Sun who have been pushing for this, including Eduardo Pelegri-Llopart.

And of course thanks to all of you who use, contribute to, and talk about Jython. Jython is above all things a community effort. We should all share in the excitement. Hurray!

About Me

My Photo
Frank Wierzbicki
View my complete profile