Search in sources :

Example 1 with SimpleJythonRunner

use of org.python.pydev.ast.runners.SimpleJythonRunner in project gda-core by openGDA.

the class ScriptProjectCreator method createInterpreter.

/**
 * We programmatically create a Jython Interpreter so that the user does not have to.
 */
private static void createInterpreter(IProgressMonitor monitor) throws Exception {
    logger.debug("Stating creation of Jython interpreter");
    final IPreferenceStore preferenceStore = GDAClientActivator.getDefault().getPreferenceStore();
    // Horrible Hack warning: This code is copied from parts of Pydev to set up the interpreter and save it.
    // Code copies from Pydev when the user chooses a Jython interpreter - these are the defaults.
    final Path interpreterPath = Paths.get(BundleUtils.getBundleLocation(JYTHON_BUNDLE).getAbsolutePath());
    final String executable = interpreterPath.resolve(JYTHON_JAR).toString();
    if (!(new File(executable)).exists())
        throw new Exception("Jython jar not found  :" + executable);
    final File script = CorePlugin.getScriptWithinPySrc("interpreterInfo.py");
    if (!script.exists()) {
        throw new RuntimeException("The file specified does not exist: " + script);
    }
    monitor.subTask("Creating interpreter");
    // gets the info for the python side
    String encoding = null;
    Tuple<String, String> outTup = new SimpleJythonRunner().runAndGetOutputWithJar(FileUtils.getFileAbsolutePath(script), executable, null, null, null, monitor, encoding);
    InterpreterInfo info = null;
    try {
        // HACK Otherwise Pydev shows a dialog to the user.
        ModulesManagerWithBuild.IN_TESTS = true;
        info = InterpreterInfo.fromString(outTup.o1, false);
    } catch (Exception e) {
        logger.error("Something went wrong creating the InterpreterInfo.", e);
    } finally {
        ModulesManagerWithBuild.IN_TESTS = false;
    }
    if (info == null) {
        // cancelled
        return;
    }
    // the executable is the jar itself
    info.executableOrJar = executable;
    // we have to find the jars before we restore the compiled libs
    if (preferenceStore.getBoolean(PreferenceConstants.GDA_PYDEV_ADD_DEFAULT_JAVA_JARS)) {
        List<File> jars = JavaVmLocationFinder.findDefaultJavaJars();
        for (File jar : jars) {
            info.libs.add(FileUtils.getFileAbsolutePath(jar));
        }
    }
    List<String> allScriptProjectFolders = JythonServerFacade.getInstance().getAllScriptProjectFolders();
    for (String s : allScriptProjectFolders) {
        info.libs.add(s);
    }
    // java, java.lang, etc should be found now
    info.restoreCompiledLibs(monitor);
    info.setName(INTERPRETER_NAME);
    final JythonInterpreterManager man = (JythonInterpreterManager) InterpreterManagersAPI.getJythonInterpreterManager();
    HashSet<String> set = new HashSet<>();
    set.add(INTERPRETER_NAME);
    man.setInfos(new IInterpreterInfo[] { info }, set, monitor);
    logger.info("Jython interpreter registered: {}", INTERPRETER_NAME);
}
Also used : Path(java.nio.file.Path) JythonInterpreterManager(org.python.pydev.ast.interpreter_managers.JythonInterpreterManager) CoreException(org.eclipse.core.runtime.CoreException) SimpleJythonRunner(org.python.pydev.ast.runners.SimpleJythonRunner) InterpreterInfo(org.python.pydev.ast.interpreter_managers.InterpreterInfo) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore) File(java.io.File) HashSet(java.util.HashSet)

Example 2 with SimpleJythonRunner

use of org.python.pydev.ast.runners.SimpleJythonRunner in project Pydev by fabioz.

the class JythonInterpreterManager method doCreateInterpreterInfo.

/**
 * This is the method that creates the interpreter info for jython. It gets the info on the jython side and on the java side
 *
 * @param executable the jar that should be used to get the info
 * @param monitor a monitor, to keep track of what's happening
 * @return the interpreter info, with the default libraries and jars
 *
 * @throws CoreException
 */
public static Tuple<InterpreterInfo, String> doCreateInterpreterInfo(String executable, IProgressMonitor monitor, boolean askUser) throws CoreException, JDTNotAvailableException {
    boolean isJythonExecutable = InterpreterInfo.isJythonExecutable(executable);
    if (!isJythonExecutable) {
        throw new RuntimeException("In order to get the info for the jython interpreter, a jar is needed (e.g.: jython.jar)");
    }
    File script = getInterpreterInfoPy();
    // gets the info for the python side
    Tuple<String, String> outTup = new SimpleJythonRunner().runAndGetOutputWithJar(FileUtils.getFileAbsolutePath(script), executable, null, null, null, monitor, "utf-8");
    String output = outTup.o1;
    InterpreterInfo info = createInfoFromOutput(monitor, outTup, askUser, executable, false);
    if (info == null) {
        // cancelled
        return null;
    }
    // the executable is the jar itself
    info.executableOrJar = executable;
    // we have to find the jars before we restore the compiled libs
    List<File> jars = JavaVmLocationFinder.findDefaultJavaJars();
    for (File jar : jars) {
        info.libs.add(FileUtils.getFileAbsolutePath(jar));
    }
    // java, java.lang, etc should be found now
    info.restoreCompiledLibs(monitor);
    return new Tuple<InterpreterInfo, String>(info, output);
}
Also used : SimpleJythonRunner(org.python.pydev.ast.runners.SimpleJythonRunner) File(java.io.File) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 3 with SimpleJythonRunner

use of org.python.pydev.ast.runners.SimpleJythonRunner in project Pydev by fabioz.

the class JythonTest method exec.

private static Throwable exec(File f) {
    System.out.println(StringUtils.format("Running: %s", f));
    String sep = SimpleRunner.getPythonPathSeparator();
    assertTrue(new File(TestDependent.JYTHON_ANT_JAR_LOCATION).exists());
    assertTrue(new File(TestDependent.JYTHON_JUNIT_JAR_LOCATION).exists());
    String pythonpath = TestDependent.TEST_PYDEV_PLUGIN_LOC + "pysrc/" + sep + TestDependent.JYTHON_ANT_JAR_LOCATION + sep + TestDependent.JYTHON_JUNIT_JAR_LOCATION;
    Tuple<String, String> output = new SimpleJythonRunner().runAndGetOutputWithJar(new File(TestDependent.JAVA_LOCATION), f.toString(), TestDependent.JYTHON_JAR_LOCATION, null, f.getParentFile(), null, null, pythonpath, "utf-8");
    System.out.println(StringUtils.format("stdout:%s\nstderr:%s", output.o1, output.o2));
    if (output.o2.toLowerCase().indexOf("failed") != -1 || output.o2.toLowerCase().indexOf("traceback") != -1) {
        throw new AssertionError(output.toString());
    }
    return null;
}
Also used : SimpleJythonRunner(org.python.pydev.ast.runners.SimpleJythonRunner) File(java.io.File)

Example 4 with SimpleJythonRunner

use of org.python.pydev.ast.runners.SimpleJythonRunner in project Pydev by fabioz.

the class SimpleJythonRunnerTest method testRun.

public void testRun() throws CoreException, IOException {
    SimpleJythonRunner runner = new SimpleJythonRunner();
    File absoluteFile = CorePlugin.getBundleInfo().getRelativePath(new Path("interpreterInfo.py")).getAbsoluteFile();
    String string = runner.runAndGetOutputWithJar(FileUtils.getFileAbsolutePath(absoluteFile), TestDependent.JYTHON_JAR_LOCATION, null, null, null, new NullProgressMonitor(), "utf-8").o1;
    assertNotNull(string);
}
Also used : Path(org.eclipse.core.runtime.Path) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) SimpleJythonRunner(org.python.pydev.ast.runners.SimpleJythonRunner) File(java.io.File)

Aggregations

File (java.io.File)4 SimpleJythonRunner (org.python.pydev.ast.runners.SimpleJythonRunner)4 Path (java.nio.file.Path)1 HashSet (java.util.HashSet)1 CoreException (org.eclipse.core.runtime.CoreException)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 Path (org.eclipse.core.runtime.Path)1 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)1 InterpreterInfo (org.python.pydev.ast.interpreter_managers.InterpreterInfo)1 JythonInterpreterManager (org.python.pydev.ast.interpreter_managers.JythonInterpreterManager)1 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)1 Tuple (org.python.pydev.shared_core.structure.Tuple)1