Thursday, July 23, 2009

Running Pylons with Jython for Beginners

Well I'm sure someone out there besides me is interested in Pylons running on the JVM. Jython is a project working to bring Python to the JVM. If you are a Java user, and also a Pylons user you may be interested in the following.

To get started you need to install Jython. You can visit the Jython Site or get the 2.5.0 release here. You will need a version >= 2.5.0. The download is a jar based installer. You will need to run this as an application. On some platforms you can run the installer by double clicking the jar file (worked on OS X). On others you may need to go to the command line and issue this command "java -jar <jarname>".

Once Jython is installed setup easy_install for jython. Get ez_setup.py (find it here). Once you have ez_setup.py downloaded run it using "jython ez_setup.py". This will install easy_install in Jython's bin folder.

Now if you work with Python you'll probably want to create an alias for this easy_install. I did. I just put the following in my .profile (note I also am aliasing virutualenv):
alias jeasy_install="$JYTHON_HME/bin/easy_install"
alias jeasy_install2.5="$JYTHON_HME/bin/easy_install-2.5"
alias jvirtualenv="$JYTHON_HME/bin/virtualenv"
Another note, don't use JYTHON_HOME in your bash env if you want virtualenv to work. I substituted JYTHON_HME. If you wan't to see why you can't use JYTHON_HOME check out the jython script. This needs to use JYTHON_HOME to configure the java classpath and run the Jython application.

Get virtualenv installed for Jython. Just type "jeasy_install virtualenv". Once that finishes you should have a 'virtualenv' tool in the Jython installation's bin folder. Note I have an example above of aliasing this to jvirtualenv, so that I can use the Python based version too.

Ok now you are ready for Pylons. You'll download the go-pylons script from PylonsHQ get this script here. Once it's downloaded run it using "jython go-pylons.py jylons". This will create all the virtual environment where we can install packages specific to our app that we do not want polluting the Jython site-packages.

Alright getting closer... Activate your new env. On *nix this is as easy as "source jylons/bin/activate" on Windows "jylons\bin\activate.bat". For more info on virtualenv see here.

You should now be using the 'jylons' virtual environment. We are close. Type "paster create --t pylons". A script will now guide you through creating your Pylons app. For this part I advise you follow the "getting started" guide from PylonsHQ here.

You'll now need to get the bleeding edge SQLAlchemy, which supports the Jython DBAPI implementation 'zxJDBC'. Use this URL to check it out. Once that is done make sure your jylons virtualenv is activated, if not activate it now. Change dir to the checked out project. Run the setup.py file like so: "jython setup.py install". This will install the trunk version of SQLAlchemy under the 'jylons' virtualenv.

Before I go I want to make sure that you can connect to your database. In my case Postgres is the database of choice. You could just as well use MySQL. As far as I can tell no integration exists for sqlite, so when you create a new Pylons project that uses SQLAlchemy it won't start up right away; due to errors stemming from sqlite being the configured database. Just switch to MySQL or Postgres.

In the development.ini file you'll want to set the sqlalchemy URL like so:

sqlalchemy.url = postgresql+zxjdbc://<username>:<password>@localhost:5432/<dbname>

Note the '+zxjdbc'. This is what SQLAlchemy 0.6 is using to designate a Jython zxJDBC based DBAPI. If you are interested check the source code for sqlalchemy.engine.url. This will in turn require the Postgres JDBC driver is on the classpath. I hacked it on by updating the jython script to add the driver. Not an optimal solution but workable for now. I'm hoping for a better mechanism to add jar files to Jython's class path at some point.

You should be ready to go. You can now create tables and such in SA. Have fun!

Thursday, June 4, 2009

OS X Terminal Send Function Keys to Terminal

Yikes this one took me a bit of time to figure out. I was installing an image remotely from my Mac, and the image installation edtior used the function keys to save, quit etc. Well on my OS X the function keys first are overridden to do specific things. Ok well just press the 'fn' key while pressing the function key. Oh now they are overridden for Expose and so forth.

Ok well what you are goign to want to do here is press the 'fn' key, the command key, and then the F# key you want sent to the Terminal.

Thursday, February 26, 2009

Mercurial "case-folding collision" More Explanation on the fix

I've run into this error now a few times. What happens is you rename a file, changing only the case of one or more characters in the file name. This causes confusion. Now you can read here and here about the fix to this problem. I'm going to give a little more info on how to make this work, just because it might not be crystal clear reading those two sources.

You got this error either by checking out the project, or pulling changesets into your project and trying the 'update' command. So run these commands:

hg debugsetparents <revision>

The revision should be the one you are attempting to update to.

hg debugrebuildstate

now you want to remove all the files in error, you can use 'hg manifest tip' to check for the files in error. I've had more than one before after doing numerous renames that camel cased a number of files. use:

hg rm -A <filename>

to remove files, you can list multiple files to delete using spaces between each.

Once you've removed the files you should commit the changes:

hg ci -m "Fixed case problems"

Then do a clean check out the tip revision:

hg co -C tip

thats it, hope this helps to shed a little extra light on the process.

Tuesday, February 24, 2009

Using sort and uniq to find duplicate lines in a file

I love my Mac, mostly because I can run all those great Unix utilities that make tasks easy. Today I want to point out a pair of tools that come in handy often. They are sort and uniq.

sort can be used to sort the lines in a given file. A simple use would be

echo -e "c\na\nb" | sort

which produces:

a
b
c

Sweet! Now lets look at the sort program:

echo -e "a\na\nb\nc\nb" | uniq

gives:

a
b
c
b

Removing the duplicate a, but not the b! The tool only removes any consecutive duplicate lines. So what if you want to remove all duplicates? Easy:

echo -e "a\na\nb\nc\nb" | sort | uniq

gives:

a
b
c

Taa Dah! Thats easy!

Now this duo can be used in very many useful ways. Just the other day I needed to find two XML elements that had the same value. I used 'sed' to pull out all the values in the given element, sort to put these values in lexographically sorted order, and uniq to tell me the duplicates found:

sed -n 's/.*<tag>\(.*\)<\/tag>/\1/p' | sort | uniq -d

For the 'sed' part you could put any tag name in there you need to find duplicate value for. should be the name of the file you are using. Notice that each XML element was on it's own line, so this wouldn't work for documents that are not formatted nicely (for that see xmllint :).

Friday, February 20, 2009

Memory settings when using Jetty inside of Eclipse

If you like to debug and run your webapps inside of Eclipse you've got two options. First there is Tomcat, which running will require a plugin like Sysdeo. Second, you can write a simple class to start up your web application using Jetty. I like to use Jetty rather than the old school Sysdeo Tomcat plugin. I've found that it is simpler to configure the application to run in a test or stage mode this way inside of Eclipse.

If you run Jetty inside of Eclipse you may need to increase the memory being used by the process (Jetty is fast to start up and doesn't requires much memory, but your web app may!). So if you have a process to start your web app in Eclipse you'll have a run configuration. If you open this (click the item menu drop down to the right of the green circle w/ a triangle in it) configuration in the right pane select the 'Arguments' tab.

As you can see in the image above you then will want to fill in the VM arguments to contain the needed memory settings. In the above configuration I've added settings to increase the minimum heap to 256 mb and the max heap to 512 mb. Thats all I needed for my webapp to run smoothly, you may require more or less.

Thursday, February 19, 2009

OAuth Library for Java & Groovy

I've released a latest version of a simple OAuth client I developed. You can find version 1.1 of the jar here. Also the site for the project is here, and finally the source is here. I should have some documentation on basic usage up later.

So far the library only supports the HMAC-SHA1 method of signing, and is using query parameters to send the OAuth information. Soon I'll add support for using headers and POST data to send the OAuth information.

Maven and Mercurial: tips on performing a release

If you are using Maven 2 and have a project in source control via Mercurial then you might need this tip. If you do a 'mvn release:prepare' then Maven will try and push the changes and will need auth. Use the sys properties -Dusername= -Dpassword=' to get authenticated. I don't think this is very secure because the properties seem to be plaintext, but eh what can you do?