Search in sources :

Example 1 with IProcessInfo

use of org.python.pydev.shared_core.utils.IProcessInfo in project Pydev by fabioz.

the class ProcessListWin32 method parseListTasks.

public IProcessInfo[] parseListTasks(InputStreamReader reader) {
    BufferedReader br = new BufferedReader(reader);
    CSVReader csvReader = new CSVReader(br);
    List<ProcessInfo> processList = new ArrayList<>();
    String[] next;
    do {
        try {
            next = csvReader.readNext();
            if (next != null) {
                int pid = Integer.parseInt(next[1]);
                String name = StringUtils.join(" - ", next[0], next[next.length - 1]);
                processList.add(new ProcessInfo(pid, name));
            }
        } catch (IOException e) {
            break;
        }
    } while (next != null);
    return processList.toArray(new IProcessInfo[processList.size()]);
}
Also used : BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) ProcessInfo(org.python.pydev.shared_core.utils.internal.ProcessInfo) IProcessInfo(org.python.pydev.shared_core.utils.IProcessInfo) IOException(java.io.IOException)

Example 2 with IProcessInfo

use of org.python.pydev.shared_core.utils.IProcessInfo in project Pydev by fabioz.

the class ProcessListWin32Internal method getProcessList.

@Override
public IProcessInfo[] getProcessList() {
    Process p = null;
    String command = null;
    InputStream in = null;
    Bundle bundle = Platform.getBundle(SharedCorePlugin.SHARED_CORE_PLUGIN_ID);
    IProcessInfo[] procInfos = NOPROCESS;
    try {
        File file;
        IPath relative = new Path("win32").addTrailingSeparator().append("listtasks.exe");
        file = BundleUtils.getRelative(relative, bundle);
        if (file != null && file.exists()) {
            command = FileUtils.getFileAbsolutePath(file);
            if (command != null) {
                try {
                    p = ProcessUtils.createProcess(new String[] { command }, null, null);
                    in = p.getInputStream();
                    InputStreamReader reader = new InputStreamReader(in);
                    procInfos = parseListTasks(reader);
                } finally {
                    if (in != null) {
                        in.close();
                    }
                    if (p != null) {
                        p.destroy();
                    }
                }
            }
        }
    } catch (IOException e) {
    }
    return procInfos;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Bundle(org.osgi.framework.Bundle) IProcessInfo(org.python.pydev.shared_core.utils.IProcessInfo) IOException(java.io.IOException) File(java.io.File)

Example 3 with IProcessInfo

use of org.python.pydev.shared_core.utils.IProcessInfo in project Pydev by fabioz.

the class ProcessListWin32Internal method parseListTasks.

public IProcessInfo[] parseListTasks(InputStreamReader reader) {
    BufferedReader br = new BufferedReader(reader);
    List<IProcessInfo> processList = new ArrayList<>();
    try {
        String line;
        while ((line = br.readLine()) != null) {
            int tab = line.indexOf('\t');
            if (tab != -1) {
                String proc = line.substring(0, tab).trim();
                String name = line.substring(tab).trim();
                if (proc.length() > 0 && name.length() > 0) {
                    try {
                        int pid = Integer.parseInt(proc);
                        processList.add(new ProcessInfo(pid, name));
                    } catch (NumberFormatException e) {
                    }
                }
            }
        }
    } catch (IOException e) {
    }
    return processList.toArray(new IProcessInfo[processList.size()]);
}
Also used : BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) IProcessInfo(org.python.pydev.shared_core.utils.IProcessInfo) ProcessInfo(org.python.pydev.shared_core.utils.internal.ProcessInfo) IProcessInfo(org.python.pydev.shared_core.utils.IProcessInfo) IOException(java.io.IOException)

Example 4 with IProcessInfo

use of org.python.pydev.shared_core.utils.IProcessInfo in project Pydev by fabioz.

the class ProcessListLinux method getProcessList.

/**
 * Insert the method's description here.
 * @see IProcessList#getProcessList
 */
@Override
public IProcessInfo[] getProcessList() {
    // $NON-NLS-1$
    File proc = new File("/proc");
    File[] pidFiles = null;
    // We are only interested in the pid so filter the rest out.
    try {
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                boolean isPID = false;
                try {
                    Integer.parseInt(name);
                    isPID = true;
                } catch (NumberFormatException e) {
                }
                return isPID;
            }
        };
        pidFiles = proc.listFiles(filter);
    } catch (SecurityException e) {
    }
    ProcessInfo[] processInfo = empty;
    if (pidFiles != null) {
        processInfo = new ProcessInfo[pidFiles.length];
        for (int i = 0; i < pidFiles.length; i++) {
            // $NON-NLS-1$
            File cmdLine = new File(pidFiles[i], "cmdline");
            String name = FileUtils.getFileContents(cmdLine).replace('\0', ' ');
            if (name.length() == 0) {
                // $NON-NLS-1$
                name = "Unknown";
            }
            processInfo[i] = new ProcessInfo(pidFiles[i].getName(), name);
        }
    } else {
        pidFiles = new File[0];
    }
    return processInfo;
}
Also used : FilenameFilter(java.io.FilenameFilter) ProcessInfo(org.python.pydev.shared_core.utils.internal.ProcessInfo) IProcessInfo(org.python.pydev.shared_core.utils.IProcessInfo) File(java.io.File)

Example 5 with IProcessInfo

use of org.python.pydev.shared_core.utils.IProcessInfo in project Pydev by fabioz.

the class AttachToProcess method doIt.

protected void doIt() throws Exception {
    IProcessList processList = PlatformUtils.getProcessList();
    IProcessInfo[] processList2 = processList.getProcessList();
    TreeNode<Object> root = new TreeNode<Object>(null, null);
    for (IProcessInfo iProcessInfo : processList2) {
        new TreeNode<>(root, iProcessInfo);
    }
    TreeNode<Object> element = new Select1Dialog() {

        @Override
        protected String getInitialFilter() {
            return "*python*";
        }

        @Override
        protected ILabelProvider getLabelProvider() {
            return new TreeNodeLabelProvider() {

                @Override
                public Image getImage(Object element) {
                    return ImageCache.asImage(SharedUiPlugin.getImageCache().get(UIConstants.PUBLIC_ATTR_ICON));
                }

                @SuppressWarnings("unchecked")
                @Override
                public String getText(Object element) {
                    if (element == null) {
                        return "null";
                    }
                    TreeNode<Object> node = (TreeNode<Object>) element;
                    Object data = node.data;
                    if (data instanceof IProcessInfo) {
                        IProcessInfo iProcessInfo = (IProcessInfo) data;
                        return iProcessInfo.getPid() + " - " + iProcessInfo.getName();
                    }
                    return "Unexpected: " + data;
                }
            };
        }
    }.selectElement(root);
    if (element != null) {
        IProcessInfo p = (IProcessInfo) element.data;
        int pid = p.getPid();
        if (!PydevRemoteDebuggerServer.isRunning()) {
            // I.e.: the remote debugger server must be on so that we can attach to it.
            PydevRemoteDebuggerServer.startServer();
        }
        // Select interpreter
        IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        IInterpreterManager interpreterManager = InterpreterManagersAPI.getPythonInterpreterManager();
        if (interpreterManager == null) {
            MessageDialog.openError(workbenchWindow.getShell(), "No interpreter manager.", "No interpreter manager was available for attaching to a process.");
        }
        IInterpreterInfo[] interpreters = interpreterManager.getInterpreterInfos();
        if (interpreters == null || interpreters.length == 0) {
            MessageDialog.openError(workbenchWindow.getShell(), "No interpreters for creating console", "An interpreter that matches the architecture of the target process must be configured in the interpreter preferences.");
            return;
        }
        SelectionDialog listDialog = AbstractInterpreterPreferencesPage.createChooseIntepreterInfoDialog(workbenchWindow, interpreters, "Select interpreter which matches the architecture of the target process (i.e.: 32/64 bits).", false);
        int open = listDialog.open();
        if (open != ListDialog.OK || listDialog.getResult().length != 1) {
            return;
        }
        Object[] result = listDialog.getResult();
        IInterpreterInfo interpreter;
        if (result == null || result.length == 0) {
            interpreter = interpreters[0];
        } else {
            interpreter = ((IInterpreterInfo) result[0]);
        }
        SimplePythonRunner runner = new SimplePythonRunner();
        IPath relative = new Path("pysrc").append("pydevd_attach_to_process").append("attach_pydevd.py");
        String script = CorePlugin.getBundleInfo().getRelativePath(relative).getAbsolutePath();
        String[] args = new String[] { "--port", "" + DebugPluginPrefsInitializer.getRemoteDebuggerPort(), "--pid", "" + pid, "--protocol", "http" };
        IPythonNature nature = new SystemPythonNature(interpreterManager, interpreter);
        String[] s = SimplePythonRunner.preparePythonCallParameters(interpreter.getExecutableOrJar(), script, args);
        Tuple<Process, String> run = runner.run(s, (File) null, nature, new NullProgressMonitor());
        if (run.o1 != null) {
            ShowProcessOutputDialog dialog = new ShowProcessOutputDialog(UIUtils.getActiveShell(), run.o1);
            dialog.open();
        }
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProcessList(org.python.pydev.shared_core.utils.IProcessList) IPythonNature(org.python.pydev.core.IPythonNature) Image(org.eclipse.swt.graphics.Image) SelectionDialog(org.eclipse.ui.dialogs.SelectionDialog) TreeNode(org.python.pydev.shared_core.structure.TreeNode) TreeNodeLabelProvider(org.python.pydev.ui.dialogs.TreeNodeLabelProvider) IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IPath(org.eclipse.core.runtime.IPath) Select1Dialog(org.python.pydev.ui.dialogs.Select1Dialog) SystemPythonNature(org.python.pydev.plugin.nature.SystemPythonNature) IProcessInfo(org.python.pydev.shared_core.utils.IProcessInfo) ILabelProvider(org.eclipse.jface.viewers.ILabelProvider) IInterpreterManager(org.python.pydev.core.IInterpreterManager) IInterpreterInfo(org.python.pydev.core.IInterpreterInfo) SimplePythonRunner(org.python.pydev.ast.runners.SimplePythonRunner)

Aggregations

IProcessInfo (org.python.pydev.shared_core.utils.IProcessInfo)8 IOException (java.io.IOException)5 ProcessInfo (org.python.pydev.shared_core.utils.internal.ProcessInfo)5 BufferedReader (java.io.BufferedReader)4 InputStreamReader (java.io.InputStreamReader)4 ArrayList (java.util.ArrayList)4 InputStream (java.io.InputStream)3 File (java.io.File)2 IPath (org.eclipse.core.runtime.IPath)2 Path (org.eclipse.core.runtime.Path)2 FilenameFilter (java.io.FilenameFilter)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1 ILabelProvider (org.eclipse.jface.viewers.ILabelProvider)1 Image (org.eclipse.swt.graphics.Image)1 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)1 SelectionDialog (org.eclipse.ui.dialogs.SelectionDialog)1 Bundle (org.osgi.framework.Bundle)1 SimplePythonRunner (org.python.pydev.ast.runners.SimplePythonRunner)1 IInterpreterInfo (org.python.pydev.core.IInterpreterInfo)1 IInterpreterManager (org.python.pydev.core.IInterpreterManager)1