Home | History | Annotate | Download | only in monkeyrunner
      1 package com.android.monkeyrunner;
      2 
      3 import org.python.core.Py;
      4 import org.python.core.PyObject;
      5 import org.python.util.PythonInterpreter;
      6 import org.python.util.InteractiveConsole;
      7 
      8 import java.io.File;
      9 import java.io.IOException;
     10 import java.io.FileInputStream;
     11 import java.lang.RuntimeException;
     12 import java.util.Properties;
     13 
     14 
     15 /**
     16  * Runs Jython based scripts.
     17  */
     18 public class ScriptRunner {
     19 
     20   /** The "this" scope object for scripts. */
     21   private final Object scope;
     22   private final String variable;
     23 
     24   /** Private constructor. */
     25   private ScriptRunner(Object scope, String variable) {
     26     this.scope = scope;
     27     this.variable = variable;
     28   }
     29 
     30   /** Creates a new instance for the given scope object. */
     31   public static ScriptRunner newInstance(Object scope, String variable) {
     32     return new ScriptRunner(scope, variable);
     33   }
     34 
     35   /**
     36    * Runs the specified Jython script. First runs the initialization script to
     37    * preload the appropriate client library version.
     38    */
     39   public static void run(String scriptfilename) {
     40     try {
     41       initPython();
     42       PythonInterpreter python = new PythonInterpreter();
     43 
     44       python.execfile(scriptfilename);
     45     } catch(Exception e) {
     46       e.printStackTrace();
     47     }
     48   }
     49 
     50 
     51   /** Initialize the python interpreter. */
     52   private static void initPython() {
     53     Properties props = new Properties();
     54     // Default is 'message' which displays sys-package-mgr bloat
     55     // Choose one of error,warning,message,comment,debug
     56     props.setProperty("python.verbose", "error");
     57     props.setProperty("python.path", System.getProperty("java.class.path"));
     58     PythonInterpreter.initialize(System.getProperties(), props, new String[] {""});
     59   }
     60 
     61   /**
     62    * Create and run a console using a new python interpreter for the test
     63    * associated with this instance.
     64    */
     65   public void console() throws IOException {
     66     initPython();
     67     InteractiveConsole python = new InteractiveConsole();
     68     initInterpreter(python, scope, variable);
     69     python.interact();
     70   }
     71 
     72   /**
     73    * Start an interactive python interpreter using the specified set of local
     74    * variables. Use this to interrupt a running test script with a prompt:
     75    *
     76    * @param locals
     77    */
     78   public static void console(PyObject locals) {
     79     initPython();
     80     InteractiveConsole python = new InteractiveConsole(locals);
     81     python.interact();
     82   }
     83 
     84   /**
     85    * Initialize a python interpreter.
     86    *
     87    * @param python
     88    * @param scope
     89    * @throws IOException
     90    */
     91   public static void initInterpreter(PythonInterpreter python, Object scope, String variable)
     92       throws IOException {
     93     // Store the current test case as the this variable
     94     python.set(variable, scope);
     95   }
     96 }
     97