Jython, the python implementation in Java, is one of the most amazing things i have come across in quite some time, the environment is a seamless fusion of both the languages.
Playing around with it since yesterday, from what i have noticed, the jythonc compiler is missing in 2.5.x version of jython. But on doing a quick search i found a java based jython compiler scripts ( here and here ). Also there don’t seem to be any jython compiler scripts in jython, perhaps i maybe looking at wrong place. So i wrote up a quick script which tries to do that.
""" Author:Kalyan (18-Jun-10) Usage: $jython jcompile.py """ from java.io import * import _py_compile import imp as pyimp import org.python.core.imp as jimp import os.path import sys #Show info def jcompile_usage(): print "Usage $:jython jcompile.py" print "file.py gets compiled to file$py.class" #Compile .py to .class def jcompile_compile( fname ): #java file object jfile = File( fname ) #Get module name mname = _py_compile.getModuleName( jfile ) #Compile and get bytes bytes = jimp.compileSource( mname, jfile ) #Dump bytecode into a file jimp.cacheCompiledSource( os.path.abspath( fname ), os.path.abspath( "./" + mname + "$py.class" ), bytes ) if( len(sys.argv) != 2 ): jcompile_usage(); #insufficient arguments sys.exit(0) #exit if( os.path.exists( sys.argv ) ): print "compiling %s" % sys.argv jcompile_compile( sys.argv ) else: print "Given file doesn't exist" jcompile_usage();
The code is a loose translation of the java code. The only catch is, there are two “imp” important internals libraries, org.python.modules.imp and org.python.core.imp, former the python implementation and the later one being the java python implementation.
Usage : to compile kalyanc$ jython jcompilec.py foo.py compiling foo.py
at this point foo$py.class is created
Usage : To execute kalyanc$ java -classpath .:/Library/Jython/2.5.1/jython.jar foo\$py foo bar