Porting tinypy to Android Project

From SYNTH

Jump to: navigation, search

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

  1. Write a native tinypy interpreter that
    • reads in Python script from stdin
    • prints execution results to stdout
  2. Create the tinypy process in Android Java application by calling Runtime.exec()
  3. Communicate with the native process on its I/O stream.

Image:cs636_proj2_screen.jpg

[edit] Implementation

[edit] Build a native tinypy program into an Android distribution

[edit] Build a tinypy interpreter

  1. Get the tinypy source and merge everything into one .c file and one .h file. ($ python setup.py blob)
  2. The generated file is in the build/ directory
  3. Write a main function to start the tinypy interpreter.
  4. Test the program on your machine.

[edit] Port to Android

  1. Download the Android source code.
  2. Create tinypy folder in ./external/tinypy
  3. Put the main.c, tinypy.c and tinypy.h in the folder
  4. Write an Android makefile
  5. Rebuild the Android source.

[edit] Test your native tinypy

  1. 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
  2. Use adb shell to log on your emulator.
  3. 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();
}

Personal tools