This program consists of two parts:
-
A Source translator which attempts to convert Python syntax to the Java equivalent
-
A profiler which uses Python’s Debugger and AST tools to index all run time method argument information, used by 1. above during type inference.
Surprisingly these two simple parts are sufficient to translate some simple Python to Java.
In its current form the tool will perform about 75% of code translation. The remainder must be converted manually. A lot of the outstanding syntax errors can be fixed semi-automatically with a modern Java IDE. The remainder of the work is dealing with unsupported concepts (like creating value objects for tuples etc) and porting library client code to Java equivalents.
Prepare to get your hands a little dirty…
Profiling a running program is slow so the tracer is restricted to profiling methods within a single python source file at a time. On a fast machine you can change this if you like.
-
Copy “tracer/trace.py” into your application’s directory (the directory of the file where the main(args) method is located).
-
Edit trace.py and update
TRACE_BASE = "/home/chris/ab/gamemenu.py"with the path to the python script file you wish to profile. In this examplegamemenu.py. -
Change the
importstatement to import everything from the file containing main():
from gameengine import * -
Run your application using
python trace.py. Parameters are passed through to your main(). -
Use your application in a normal manner to ‘exercise’ all the methods in the target file.
-
Quit your application.
-
Ensure that both gamemenu.py.trace and gamemenu.py.return-trace have been written.
-
Repeat for all other python source files you wish to convert.
-
Copy all the trace files captured above into the translator directory.
-
Copy all your python source files you wish to translate into the translator directory.
-
Create an output directory within the translator directory:
mkdir target -
Translate each file in turn using a command similar to:
python translate.py gamemenu.py -
Verify your source files have been written to target.
-
Create a new project in your Eclipse IDE and import all the generated source files.
-
Stub out the python library APIs which your code is dependent on and have not been converted.
-
Use your IDE’s tools to infer types of local variables and fields. It should manage this in most
-
cases.
-
Fix up return types manually.
-
Port library client code to Java API equivalents. This is easier to do once the majority of the code is compiling (against your stubs).
Each python source file results in a similarly named java package.
Each class within a python source file is written to its own source file
Static procedures and variables within the python file are written to a class called Default.java within the package. This works as long as all the static content of a file is at the top of the file, with only classes following. You may wish to reorder your input source files before translation. Alternatively you will find content dispersed through the generated class files which you must move manually.
Collection generics are not supported. You will have to add the missing type information yourself.
For methods that return tuples or multiple results you will have to create value object classes.