Porting tinypy to Android Project
From SYNTH
Contents |
[edit] Project Description
- Goal
- Integrate tinypy interpreter into an Android distribution.
- Tinypy
- A minimalism version of Python interpreter [1]
- Back-end
- Having tinypy running as a native Android/Linux process.
- Font-end
- Allow user submit Python script and see execution results in an Android Java application.
[edit] Program Design
- Write a native tinypy interpreter that
- reads in Python script from stdin
- prints execution results to stdout
- Create the tinypy process in Android Java application by calling Runtime.exec()
- Communicate with the native process on its I/O stream.
[edit] Implementation
[edit] Build a native tinypy program into an Android distribution
[edit] Build a tinypy interpreter
- Get the tinypy source and merge everything into one .c file and one .h file. ($ python setup.py blob)
- The generated file is in the build/ directory
- Write a main function to start the tinypy interpreter.
- Test the program on your machine.
[edit] Port to Android
- Download the Android source code.
- Create tinypy folder in ./external/tinypy
- Put the main.c, tinypy.c and tinypy.h in the folder
- Write an Android makefile
- Rebuild the Android source.
[edit] Test your native tinypy
- Run your newly built system.img in the Android emulator.
- replace the old system.img in tools/lib/images
- or use -image to load your new system.img
- Use adb shell to log on your emulator.
- Type tinypy to start the native tinypy interpreter.
The root folder on Android is read-only. You could change directory to data/ where you can save your python scripts.
[edit] Write an Android Java GUI
I'll skip the Android GUI programming part and here's the code that will create a native tinypy process:
private String execPython(String text) {
StringBuffer buffer = new StringBuffer();
try {
// run tinypy process
final Process p = Runtime.getRuntime().exec("tinypy");
// input python code
final PrintWriter out = new PrintWriter(new OutputStreamWriter(p.getOutputStream()));
out.write(text);
out.flush();
out.close();
// result reader
final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = in.readLine();
while(line != null) {
buffer.append(line + '\n');
line = in.readLine();
}
in.close();
// clean up
p.destroy();
} catch (IOException e) {
Log.e(TAG, "fail exec native tinypy", e);
}
return buffer.toString();
}

