use of org.python.pydev.core.IInterpreterManager in project Pydev by fabioz.
the class SimpleJythonRunner method makeExecutableCommandStrWithVMArgs.
/**
* @param script
* @return
* @throws IOException
* @throws MisconfigurationException
*/
public static String[] makeExecutableCommandStrWithVMArgs(String jythonJar, String script, String basePythonPath, String vmArgs, String... args) throws IOException, JDTNotAvailableException, MisconfigurationException {
IInterpreterManager interpreterManager = InterpreterManagersAPI.getJythonInterpreterManager();
String javaLoc = FileUtils.getFileAbsolutePath(JavaVmLocationFinder.findDefaultJavaExecutable());
File file = new File(javaLoc);
if (file.exists() == false) {
throw new RuntimeException("The java location found does not exist. " + javaLoc);
}
if (file.isDirectory() == true) {
throw new RuntimeException("The java location found is a directory. " + javaLoc);
}
if (!new File(jythonJar).exists()) {
throw new RuntimeException(StringUtils.format("Error. The default configured interpreter: %s does not exist!", jythonJar));
}
InterpreterInfo info = (InterpreterInfo) interpreterManager.getInterpreterInfo(jythonJar, new NullProgressMonitor());
// pythonpath is: base path + libs path.
String libs = SimpleRunner.makePythonPathEnvFromPaths(info.libs);
FastStringBuffer jythonPath = new FastStringBuffer(basePythonPath, 128);
String pathSeparator = SimpleRunner.getPythonPathSeparator();
if (jythonPath.length() != 0) {
jythonPath.append(pathSeparator);
}
jythonPath.append(libs);
// may have the dir or be null
String cacheDir = null;
try {
AstPlugin plugin = AstPlugin.getDefault();
if (plugin != null) {
IPath stateLocation = plugin.getStateLocation();
File cacheDirFile = new File(stateLocation.toFile(), "jython_cache_dir");
cacheDirFile.mkdirs();
cacheDir = PydevPrefs.getEclipsePreferences().get(IInterpreterManager.JYTHON_CACHE_DIR, cacheDirFile.toString());
}
} catch (AssertionFailedException e) {
// this may happen while running the tests... it should be ok.
cacheDir = null;
}
if (cacheDir != null && cacheDir.trim().length() == 0) {
cacheDir = null;
}
if (cacheDir != null) {
cacheDir = "-Dpython.cachedir=" + cacheDir.trim();
}
String[] vmArgsList = ProcessUtils.parseArguments(vmArgs);
String[] s = new String[] { "-Dpython.path=" + jythonPath.toString(), "-classpath", jythonJar + pathSeparator + jythonPath, "org.python.util.jython", script };
List<String> asList = new ArrayList<String>();
asList.add(javaLoc);
if (cacheDir != null) {
asList.add(cacheDir);
}
asList.addAll(Arrays.asList(vmArgsList));
asList.addAll(Arrays.asList(s));
asList.addAll(Arrays.asList(args));
return asList.toArray(new String[0]);
}
use of org.python.pydev.core.IInterpreterManager in project Pydev by fabioz.
the class PyExceptionBreakPointManager method getBuiltinExceptions.
/**
* @return a list the default 'builtin' exceptions to be presented to the user (i.e.:
* AssertionError, RuntimeError, etc)
*/
public List<String> getBuiltinExceptions(IModuleRequestState moduleRequest) {
ArrayList<String> list = new ArrayList<String>();
IInterpreterManager useManager = ChooseInterpreterManager.chooseInterpreterManager();
if (useManager != null) {
TokensList pythonTokens = useManager.getBuiltinMod(IPythonNature.DEFAULT_INTERPRETER, moduleRequest).getGlobalTokens();
for (IterTokenEntry entry : pythonTokens) {
IToken token = entry.getToken();
String pyToken = token.getRepresentation();
String lower = pyToken.toLowerCase();
if (lower.contains("error") || lower.contains("exception") || lower.contains("warning")) {
list.add(pyToken.trim());
}
}
Collections.sort(list);
}
return list;
}
use of org.python.pydev.core.IInterpreterManager in project Pydev by fabioz.
the class PydevIProcessFactory method createInteractiveLaunch.
/**
* Creates a launch (and its associated IProcess) for the xml-rpc server to be used in the interactive console.
*
* It'll ask the user how to create it:
* - editor
* - python interpreter
* - jython interpreter
*
* @return the Launch, the Process created and the port that'll be used for the server to call back into
* this client for requesting input.
*
* @throws UserCanceledException
* @throws Exception
*/
public PydevConsoleLaunchInfo createInteractiveLaunch() throws UserCanceledException, Exception {
IWorkbenchWindow workbenchWindow = EditorUtils.getActiveWorkbenchWindow();
IEditorPart activeEditor = EditorUtils.getActiveEditor();
PyEdit edit = null;
if (activeEditor instanceof PyEdit) {
edit = (PyEdit) activeEditor;
}
ChooseProcessTypeDialog dialog = new ChooseProcessTypeDialog(getShell(), edit);
String rep = InteractiveConsolePrefs.getDefaultInteractiveConsole();
boolean hasPrefs = false;
if (!rep.isEmpty()) {
dialog.create();
hasPrefs = dialog.setInteractiveConsoleInterpreterPref(rep);
if (hasPrefs) {
dialog.okPressed();
}
}
if (hasPrefs || dialog.open() == ChooseProcessTypeDialog.OK) {
PyStackFrame selectedFrame = dialog.getSelectedFrame();
if (selectedFrame != null) {
// Interpreter not required for Debug Console
String encoding = getEncodingFromFrame(selectedFrame);
return new PydevConsoleLaunchInfo(null, null, 0, null, selectedFrame, new String[] { "Debug connection (no command line)" }, null, encoding);
}
IInterpreterManager interpreterManager = dialog.getInterpreterManager();
if (interpreterManager == null) {
MessageDialog.openError(workbenchWindow.getShell(), "No interpreter manager for creating console", "No interpreter manager was available for creating a console.");
}
IInterpreterInfo[] interpreters = interpreterManager.getInterpreterInfos();
if (interpreters == null || interpreters.length == 0) {
MessageDialog.openError(workbenchWindow.getShell(), "No interpreters for creating console", "No interpreter available for creating a console.");
return null;
}
IInterpreterInfo interpreter = null;
if (interpreters.length == 1) {
// We just have one, so, no point in asking about which one should be there.
interpreter = interpreters[0];
}
if (interpreter == null) {
if (PydevConsoleConstants.ACTIVE_EDITOR_INTERPRETER_REPRESENTATION.equals(dialog.getSelectedInteractiveConsoleInterpreterRep())) {
if (edit != null) {
PythonNature pythonNature = PythonNature.getPythonNature(edit.getProject());
if (pythonNature != null) {
interpreter = pythonNature.getProjectInterpreter();
}
}
}
if (interpreter == null) {
SelectionDialog listDialog = AbstractInterpreterPreferencesPage.createChooseIntepreterInfoDialog(workbenchWindow, interpreters, "Select interpreter to be used.", false);
int open = listDialog.open();
if (open != ListDialog.OK || listDialog.getResult().length > 1) {
return null;
}
Object[] result = listDialog.getResult();
if (result == null || result.length == 0) {
interpreter = interpreters[0];
} else {
interpreter = ((IInterpreterInfo) result[0]);
}
}
}
if (interpreter == null) {
return null;
}
Tuple<Collection<String>, IPythonNature> pythonpathAndNature = dialog.getPythonpathAndNature(interpreter);
if (pythonpathAndNature == null) {
return null;
}
return createLaunch(interpreterManager, interpreter, pythonpathAndNature.o1, pythonpathAndNature.o2, dialog.getNatures());
}
return null;
}
use of org.python.pydev.core.IInterpreterManager in project Pydev by fabioz.
the class DjangoSettingsPage method discoverDefaultVersion.
protected void discoverDefaultVersion(final String projectType, final String projectInterpreter) {
// It should be discovered below, but if not found for some reason, this will be the default.
defaultVersion = DJANGO_14;
SystemPythonNature nature;
try {
final int interpreterType = PythonNature.getInterpreterTypeFromVersion(projectType);
IInterpreterManager interpreterManagerFromType = InterpreterManagersAPI.getInterpreterManagerFromType(interpreterType);
IInterpreterInfo interpreterInfo;
if (IPythonNature.DEFAULT_INTERPRETER.equals(projectInterpreter)) {
interpreterInfo = interpreterManagerFromType.getDefaultInterpreterInfo(false);
} else {
interpreterInfo = interpreterManagerFromType.getInterpreterInfo(projectInterpreter, null);
}
nature = new SystemPythonNature(interpreterManagerFromType, interpreterInfo);
AbstractRunner runner = UniversalRunner.getRunner(nature);
Tuple<String, String> output = runner.runCodeAndGetOutput(GET_DJANGO_VERSION, new String[] {}, null, new NullProgressMonitor());
String err = output.o2.trim();
String out = output.o1.trim();
if (err.length() > 0) {
Log.log("Error attempting to determine Django version: " + err);
} else {
// System.out.println("Gotten version: "+out);
if (out.startsWith("0.")) {
setDefaultVersion(DjangoSettingsPage.DJANGO_11_OR_EARLIER);
} else if (out.startsWith("1.")) {
out = out.substring(2);
if (out.startsWith("0") || out.startsWith("1")) {
setDefaultVersion(DjangoSettingsPage.DJANGO_11_OR_EARLIER);
} else if (out.startsWith("2") || out.startsWith("3")) {
setDefaultVersion(DjangoSettingsPage.DJANGO_12_OR_13);
} else {
// Later version
setDefaultVersion(DjangoSettingsPage.DJANGO_14);
}
}
}
} catch (Exception e) {
Log.log("Unable to determine Django version.", e);
}
}
use of org.python.pydev.core.IInterpreterManager in project Pydev by fabioz.
the class PythonShell method createServerProcess.
@Override
protected synchronized ProcessCreationInfo createServerProcess(IInterpreterInfo interpreter, int port) throws IOException {
File file = new File(interpreter.getExecutableOrJar());
if (file.isDirectory()) {
throw new RuntimeException("The interpreter location found is a directory. " + interpreter);
}
String[] parameters = SimplePythonRunner.preparePythonCallParameters(interpreter.getExecutableOrJar(), FileUtils.getFileAbsolutePath(serverFile), new String[] { "" + port });
IInterpreterManager manager = InterpreterManagersAPI.getPythonInterpreterManager();
String[] envp = null;
try {
envp = SimpleRunner.getEnvironment(null, interpreter, manager);
} catch (CoreException e) {
Log.log(e);
}
File workingDir = serverFile.getParentFile();
return new ProcessCreationInfo(parameters, envp, workingDir, SimpleRunner.createProcess(parameters, envp, workingDir));
}
Aggregations