private boolean doRedirect(PipedInputStream[] pin) {
   PythonInterpreter py = getInterpreter();
   Debug.saveRedirected(System.out, System.err);
   try {
     PipedOutputStream pout = new PipedOutputStream(pin[0]);
     PrintStream ps = new PrintStream(pout, true);
     if (!ScriptRunner.systemRedirected) {
       System.setOut(ps);
     }
     py.setOut(ps);
   } catch (Exception e) {
     log(-1, "%s: redirect STDOUT: %s", getName(), e.getMessage());
     return false;
   }
   try {
     PipedOutputStream eout = new PipedOutputStream(pin[1]);
     PrintStream eps = new PrintStream(eout, true);
     if (!ScriptRunner.systemRedirected) {
       System.setErr(eps);
     }
     py.setErr(eps);
   } catch (Exception e) {
     log(-1, "%s: redirect STDERR: %s", getName(), e.getMessage());
     return false;
   }
   return true;
 }
 public void evalFile(String s) {
   for (String s2 : bindings.keySet()) {
     py.set(s2, bindings.get(s2));
   }
   py.setOut(getWriter());
   py.setErr(getErrorWriter());
   try {
     py.execfile(s);
     PyStringMap locals = (PyStringMap) py.getLocals();
     Object[] values = locals.values().toArray();
     Object[] keys = locals.keys().toArray();
     bindings.clear();
     for (int i = 0; i < keys.length; ++i) {
       bindings.put((String) keys[i], values[i]);
     }
   } catch (PyException pe) {
     getErrorWriter().write(pe.toString());
     getErrorWriter().flush();
   }
 }
 /**
  * Creates a jython object instance for the script cache.
  *
  * @param key the path to the jython script
  * @return an instantiated jython object
  * @throws Exception if the jython object failed to be instantiated
  */
 @Override
 public Object createEntry(Object key) throws Exception {
   // log.debug("createEntry({})", key);
   String path = key.toString();
   int qmarkPos = path.lastIndexOf("?");
   if (qmarkPos != -1) {
     path = path.substring(0, qmarkPos);
   }
   int slashPos = path.indexOf("/");
   String portalId = path.substring(0, slashPos);
   PyObject scriptObject = null;
   InputStream in = velocityService.getResource(path);
   if (in == null) {
     log.debug("Failed to load script: '{}'", path);
   } else {
     // add current and default portal directories to python sys.path
     addSysPaths(portalId, Py.getSystemState());
     // setup the python interpreter
     PythonInterpreter python = PythonInterpreter.threadLocalStateInterpreter(null);
     // expose services for backward compatibility - deprecated
     python.set("Services", scriptingServices);
     // python.setLocals(scriptObject);
     JythonLogger jythonLogger = new JythonLogger(path);
     python.setOut(jythonLogger);
     python.setErr(jythonLogger);
     python.execfile(in, path);
     String scriptClassName = StringUtils.capitalize(FilenameUtils.getBaseName(path)) + "Data";
     PyObject scriptClass = python.get(scriptClassName);
     if (scriptClass != null) {
       scriptObject = scriptClass.__call__();
     } else {
       log.debug("Failed to find class '{}'", scriptClassName);
     }
     python.cleanup();
   }
   return scriptObject;
 }
 public void setErr(java.io.OutputStream outStream) {
   setErr(new PyFile(outStream));
 }
 /** @deprecated */
 public void setErr(java.io.Writer outStream) {
   setErr(new PyFileWriter(outStream));
 }
示例#6
0
  public PythonRunner(
      Map<String, String> environment,
      Repositories repositories,
      Scheduler scheduler,
      Hierarchy loggerRepository,
      String pythonPath,
      BufferedWriter out,
      BufferedWriter err)
      throws Exception {

    init();

    this.staticContext = new StaticContext(scheduler, loggerRepository).repository(repositories);
    this.environment = environment;

    final PySystemState systemState = new PySystemState();
    interpreter = new PythonInterpreter(null, systemState);
    for (String path : pythonPath.split(":")) {
      systemState.path.add(new PyString(path));
    }
    interpreter.setOut(out);
    interpreter.setErr(err);
    scriptContext = staticContext.scriptContext();

    // XPM module
    final PyModule xpmModule = new PyModule("xpm", (PyObject) null);
    systemState.modules.__setitem__("xpm", xpmModule);

    // Add classes
    for (PyType type : TYPES.values()) {
      xpmModule.__setattr__(type.getName(), type);
    }

    // Add constants
    Scripting.forEachConstant(
        (name, value) -> {
          xpmModule.__setattr__(name, wrap(value));
        });

    // Add functions
    Scripting.forEachFunction(
        m -> {
          xpmModule.__setattr__(m.getKey(), new PythonMethod(null, m));
        });

    // Add Python specific functions
    for (MethodFunction m : Scripting.getMethodFunctions(PythonFunctions.class)) {
      xpmModule.__setattr__(m.getKey(), new PythonMethod(null, m));
    }

    // XPM object: wrap properties
    final XPM xpm = new XPM();
    ClassDescription xpmDescription = ClassDescription.analyzeClass(XPM.class);
    for (Map.Entry<Object, MethodFunction> x : xpmDescription.getMethods().entrySet()) {
      final Object key = x.getKey();
      if (key instanceof String) {
        xpmModule.__setattr__((String) key, new PythonMethod(xpm, x.getValue()));
      } else {
        throw new XPMRuntimeException("Could not handle key ", key);
      }
    }

    // Add properties
    xpmModule.__setattr__("tasks", wrap(new Tasks()));
    xpmModule.__setattr__("logger", wrap(new ScriptingLogger("xpm")));
    xpmModule.__setattr__("env", wrap(environment));
  }