[Pydev-cvs] org.python.pydev.debug/src/org/python/pydev/debug/codecoverage PyCodeCoverageView.java,1
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-10-13 19:49:47
|
Update of /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/codecoverage In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8185/src/org/python/pydev/debug/codecoverage Modified Files: PyCodeCoverageView.java PyCoverage.java Log Message: Index: PyCodeCoverageView.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/codecoverage/PyCodeCoverageView.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PyCodeCoverageView.java 8 Oct 2004 16:44:41 -0000 1.1 --- PyCodeCoverageView.java 13 Oct 2004 19:49:38 -0000 1.2 *************** *** 7,20 **** import org.eclipse.jface.action.Action; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; ! import org.eclipse.jface.viewers.ITreeContentProvider; ! import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.viewers.Viewer; - import org.eclipse.jface.viewers.ViewerSorter; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; --- 7,20 ---- import org.eclipse.jface.action.Action; + import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.DoubleClickEvent; import org.eclipse.jface.viewers.IDoubleClickListener; import org.eclipse.jface.viewers.ISelection; + import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.IStructuredContentProvider; import org.eclipse.jface.viewers.IStructuredSelection; ! import org.eclipse.jface.viewers.SelectionChangedEvent; import org.eclipse.jface.viewers.TreeViewer; import org.eclipse.jface.viewers.Viewer; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; *************** *** 28,35 **** import org.eclipse.ui.IPropertyListener; import org.eclipse.ui.part.ViewPart; - import org.python.pydev.editor.actions.PyOpenAction; - import org.python.pydev.editor.model.ItemPointer; - import org.python.pydev.editor.model.Location; import org.python.pydev.editor.refactoring.PyRefactoring; /** --- 28,35 ---- import org.eclipse.ui.IPropertyListener; import org.eclipse.ui.part.ViewPart; import org.python.pydev.editor.refactoring.PyRefactoring; + import org.python.pydev.tree.AllowValidPathsFilter; + import org.python.pydev.tree.FileTreeLabelProvider; + import org.python.pydev.tree.FileTreePyFilesProvider; /** *************** *** 48,139 **** */ ! public class PyCodeCoverageView extends ViewPart implements IPropertyListener, ! IStructuredContentProvider{ private TreeViewer viewer; - private Action doubleClickAction; - private Action chooseAction; - protected Action clearAction; - private Button clearButton; - private List elements = new ArrayList(); - private Button chooseButton; - private Composite rComposite; - private Text text; - protected String currentDir; - - /* - * The content provider class is responsible for providing objects to the - * view. It can wrap existing objects in adapters or simply return objects - * as-is. These objects may be sensitive to the current input of the view, - * or ignore it and always show the same content (like Task List, for - * example). - */ - - class ViewLabelProvider extends LabelProvider implements - ITreeContentProvider{ - - /* (non-Javadoc) - * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object) - */ - public Object[] getChildren(Object parentElement) { - // TODO Auto-generated method stub - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object) - */ - public Object getParent(Object element) { - // TODO Auto-generated method stub - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object) - */ - public boolean hasChildren(Object element) { - // TODO Auto-generated method stub - return false; - } - - /* (non-Javadoc) - * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object) - */ - public Object[] getElements(Object inputElement) { - // TODO Auto-generated method stub - return null; - } - - /* (non-Javadoc) - * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object) - */ - public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { - // TODO Auto-generated method stub - - } - } - - class NameSorter extends ViewerSorter { - } - /** * The constructor. --- 48,73 ---- */ ! public class PyCodeCoverageView extends ViewPart implements IPropertyListener, IStructuredContentProvider { private TreeViewer viewer; private Action doubleClickAction; private Action chooseAction; protected Action clearAction; private Button clearButton; private List elements = new ArrayList(); private Button chooseButton; private Composite rComposite; private Text text; protected String currentDir; /** * The constructor. *************** *** 147,151 **** } - /** * This is a callback that will allow us to create the viewer and initialize --- 81,84 ---- *************** *** 153,196 **** */ public void createPartControl(Composite parent) { ! GridLayout layout = new GridLayout(); ! layout.numColumns = 2; ! layout.verticalSpacing = 2; ! layout.marginWidth = 0; ! layout.marginHeight = 2; ! parent.setLayout(layout); ! rComposite = new Composite(parent,SWT.MULTI); ! layout = new GridLayout(); ! layout.numColumns = 1; ! layout.verticalSpacing = 2; ! layout.marginWidth = 0; ! layout.marginHeight = 2; ! GridData layoutData = new GridData(); ! layoutData.grabExcessHorizontalSpace = true; ! layoutData.grabExcessVerticalSpace = true; ! layoutData.horizontalAlignment = GridData.FILL; ! layoutData.verticalAlignment = GridData.FILL; ! rComposite.setLayoutData(layoutData); ! rComposite.setLayout(layout); ! ! text = new Text(parent,SWT.MULTI); ! layoutData = new GridData(); ! layoutData.grabExcessHorizontalSpace = true; ! layoutData.grabExcessVerticalSpace = true; ! layoutData.horizontalAlignment = GridData.FILL; ! layoutData.verticalAlignment = GridData.FILL; ! text.setLayoutData(layoutData); ! text.setEditable(false); ! ! parent = rComposite; ! //choose button chooseButton = new Button(parent, SWT.PUSH); ! chooseAction = new Action() { public void run() { DirectoryDialog dialog = new DirectoryDialog(getSite().getShell()); String string = dialog.open(); ! if (string!=null){ ! text.setText("Chosen dir:"+string); notifyDirChanged(string); } --- 86,129 ---- */ public void createPartControl(Composite parent) { ! GridLayout layout = new GridLayout(); ! layout.numColumns = 2; ! layout.verticalSpacing = 2; ! layout.marginWidth = 0; ! layout.marginHeight = 2; ! parent.setLayout(layout); ! rComposite = new Composite(parent, SWT.MULTI); ! layout = new GridLayout(); ! layout.numColumns = 1; ! layout.verticalSpacing = 2; ! layout.marginWidth = 0; ! layout.marginHeight = 2; ! GridData layoutData = new GridData(); ! layoutData.grabExcessHorizontalSpace = true; ! layoutData.grabExcessVerticalSpace = true; ! layoutData.horizontalAlignment = GridData.FILL; ! layoutData.verticalAlignment = GridData.FILL; ! rComposite.setLayoutData(layoutData); ! rComposite.setLayout(layout); ! text = new Text(parent, SWT.MULTI); ! layoutData = new GridData(); ! layoutData.grabExcessHorizontalSpace = true; ! layoutData.grabExcessVerticalSpace = true; ! layoutData.horizontalAlignment = GridData.FILL; ! layoutData.verticalAlignment = GridData.FILL; ! text.setLayoutData(layoutData); ! text.setEditable(false); ! ! parent = rComposite; ! ! //choose button chooseButton = new Button(parent, SWT.PUSH); ! chooseAction = new Action() { public void run() { DirectoryDialog dialog = new DirectoryDialog(getSite().getShell()); String string = dialog.open(); ! if (string != null) { ! text.setText("Chosen dir:" + string); notifyDirChanged(string); } *************** *** 199,229 **** createButton(parent, chooseButton, "Choose dir!", chooseAction); //end choose button - - - - viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL - | SWT.V_SCROLL); - viewer.setContentProvider(this); - // viewer.setLabelProvider(new ViewLabelProvider()); - // viewer.setSorter(new NameSorter()); - // viewer.setInput(getViewSite()); ! // hookDoubleClickAction(); ! layoutData = new GridData(); ! layoutData.grabExcessHorizontalSpace = true; ! layoutData.grabExcessVerticalSpace = true; ! layoutData.horizontalAlignment = GridData.FILL; ! layoutData.verticalAlignment = GridData.FILL; ! viewer.getControl().setLayoutData(layoutData); ! ! ! ! //clear results button clearButton = new Button(parent, SWT.PUSH); ! clearAction = new Action() { public void run() { ! text.setText("Clear action"); } }; --- 132,175 ---- createButton(parent, chooseButton, "Choose dir!", chooseAction); //end choose button ! viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); ! viewer.setContentProvider(new FileTreePyFilesProvider()); ! viewer.setLabelProvider(new FileTreeLabelProvider()); ! viewer.addFilter(new AllowValidPathsFilter()); ! doubleClickAction = new Action() { ! public void run() { ! ISelection selection = viewer.getSelection(); ! Object obj = ((IStructuredSelection) selection).getFirstElement(); ! File realFile = new File(obj.toString()); ! if (realFile.exists()) { ! System.out.println("opening file:" + obj.toString()); ! // ItemPointer p = new ItemPointer(realFile, new ! // Location(-1, -1), null); ! // new PyOpenAction().run(p); ! } ! } ! }; ! hookViewerActions(); ! ! layoutData = new GridData(); ! layoutData.grabExcessHorizontalSpace = true; ! layoutData.grabExcessVerticalSpace = true; ! layoutData.horizontalAlignment = GridData.FILL; ! layoutData.verticalAlignment = GridData.FILL; ! viewer.getControl().setLayoutData(layoutData); ! ! //clear results button clearButton = new Button(parent, SWT.PUSH); ! clearAction = new Action() { public void run() { ! ! PyCoverage.getPyCoverage().clearInfo(); ! ! MessageDialog.openInformation(getSite().getShell(), "Cleared", ! "All the coverage data has been cleared!"); ! ! text.setText(""); } }; *************** *** 231,255 **** //end choose button - this.refresh(); - } - - - - - - - - - /** * @param string */ ! protected void notifyDirChanged(String string) { ! // TODO Auto-generated method stub ! } --- 177,192 ---- //end choose button this.refresh(); } /** * @param string */ ! protected void notifyDirChanged(String newDir) { ! File file = new File(newDir); ! PyCoverage.getPyCoverage().refreshCoverageInfo(file); ! viewer.setInput(file); ! } *************** *** 262,266 **** GridData layoutData; button.setText(txt); ! button.addSelectionListener(new SelectionListener(){ public void widgetSelected(SelectionEvent e) { --- 199,203 ---- GridData layoutData; button.setText(txt); ! button.addSelectionListener(new SelectionListener() { public void widgetSelected(SelectionEvent e) { *************** *** 270,296 **** public void widgetDefaultSelected(SelectionEvent e) { } ! }); ! layoutData = new GridData(); ! layoutData.grabExcessHorizontalSpace = true; ! layoutData.horizontalAlignment = GridData.FILL; ! button.setLayoutData(layoutData); } ! private void hookDoubleClickAction() { ! doubleClickAction = new Action() { ! public void run() { ! ISelection selection = viewer.getSelection(); ! Object obj = ((IStructuredSelection) selection) ! .getFirstElement(); ! ! File realFile = new File(obj.toString()); ! if (realFile.exists()) { ! ItemPointer p = new ItemPointer(realFile, new Location(-1, -1), null); ! new PyOpenAction().run(p); ! } ! } ! }; viewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { --- 207,220 ---- public void widgetDefaultSelected(SelectionEvent e) { } ! }); ! layoutData = new GridData(); ! layoutData.grabExcessHorizontalSpace = true; ! layoutData.horizontalAlignment = GridData.FILL; ! button.setLayoutData(layoutData); } ! private void hookViewerActions() { viewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event) { *************** *** 298,301 **** --- 222,237 ---- } }); + + viewer.addSelectionChangedListener(new ISelectionChangedListener() { + + public void selectionChanged(SelectionChangedEvent event) { + IStructuredSelection selection = (IStructuredSelection) event.getSelection(); + + Object selected_file = selection.getFirstElement(); + System.out.println("Number of items selected is " + selection.size()); + System.out.println("selected_file = " + selected_file); + } + + }); } *************** *** 308,324 **** public void propertyChanged(Object source, int propId) { ! if (source == null){ return; } ! Object[] sources = (Object[]) source; ! ! if(sources[0]== null || sources[1]== null){ return; } ! ! if (sources[0] == PyRefactoring.getPyRefactoring() ! && propId == PyRefactoring.REFACTOR_RESULT) { ! elements.clear(); elements.addAll((Collection) sources[1]); --- 244,259 ---- public void propertyChanged(Object source, int propId) { ! if (source == null) { return; } ! Object[] sources = (Object[]) source; ! ! if (sources[0] == null || sources[1] == null) { return; } ! ! if (sources[0] == PyRefactoring.getPyRefactoring() && propId == PyRefactoring.REFACTOR_RESULT) { ! elements.clear(); elements.addAll((Collection) sources[1]); *************** *** 333,336 **** } - } \ No newline at end of file --- 268,270 ---- Index: PyCoverage.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/codecoverage/PyCoverage.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PyCoverage.java 8 Oct 2004 16:44:41 -0000 1.1 --- PyCoverage.java 13 Oct 2004 19:49:38 -0000 1.2 *************** *** 6,41 **** package org.python.pydev.debug.codecoverage; import java.io.File; import java.util.Arrays; import java.util.List; import org.eclipse.core.runtime.CoreException; import org.python.pydev.debug.core.PydevDebugPlugin; /** * This class is used to make the code coverage. * ! * It works in this way: when the user requests the coverage for the execution of a module, we create a python ! * process and execute the module using the code coverage module that is packed with pydev. * ! * Other options are: ! * - Erasing the results obtained; ! * - Getting the results when requested (cached in this class). * * @author Fabio Zadrozny */ public class PyCoverage { ! ! private PyCoverage(){ } ! private static PyCoverage pyCoverage; ! /** * @return Returns the pyCoverage. */ public static PyCoverage getPyCoverage() { ! if(pyCoverage == null){ pyCoverage = new PyCoverage(); } --- 6,200 ---- package org.python.pydev.debug.codecoverage; + import java.io.BufferedReader; import java.io.File; + import java.io.FileFilter; + import java.io.IOException; + import java.io.InputStreamReader; + import java.util.ArrayList; import java.util.Arrays; + import java.util.Iterator; import java.util.List; + import java.util.Properties; + import java.util.Set; + import java.util.StringTokenizer; import org.eclipse.core.runtime.CoreException; import org.python.pydev.debug.core.PydevDebugPlugin; + import org.python.pydev.debug.ui.launching.PythonRunnerConfig; + import org.python.pydev.plugin.PydevPrefs; /** * This class is used to make the code coverage. * ! * It works in this way: when the user requests the coverage for the execution ! * of a module, we create a python process and execute the module using the code ! * coverage module that is packed with pydev. * ! * Other options are: - Erasing the results obtained; - Getting the results when ! * requested (cached in this class). * * @author Fabio Zadrozny */ public class PyCoverage { ! ! /** ! * This method contacts the python server so that we get the information on ! * the files that are below the directory passed as a parameter ! * ! * @param file2 ! */ ! public void refreshCoverageInfo(File file) { ! try { ! List pyFilesBelow = null; ! if (file.exists()) { ! pyFilesBelow = getPyFilesBelow(file); ! } else { ! pyFilesBelow = new ArrayList(); ! } ! ! if(pyFilesBelow.size() == 0){ ! return; ! } ! ! //now that we have the file information, we have to get the ! // coverage information on these files and ! //structure them so that we can get the coverage information in an ! // easy and hierarchical way. ! ! String profileScript = PythonRunnerConfig.getProfileScript(); ! ! //we have to make a process to execute the script. it should look ! // like: ! //coverage.py -r [-m] FILE1 FILE2 ... ! //Report on the statement coverage for the given files. With the -m ! //option, show line numbers of the statements that weren't ! // executed. ! ! //python coverage.py -r -m files.... ! ! String[] cmdLine = new String[4+ pyFilesBelow.size()]; ! cmdLine[0] = PydevPrefs.getDefaultInterpreter(); ! cmdLine[1] = profileScript; ! cmdLine[2] = "-r"; ! cmdLine[3] = "-m"; ! int i = 4; ! for (Iterator iter = pyFilesBelow.iterator(); iter.hasNext();) { ! cmdLine[i] = iter.next().toString(); ! i++; ! } ! ! Process p=null; ! ! try { ! ! p = execute(cmdLine); ! //we have the process... ! int bufsize = 64; // small bufsize so that we can see the progress ! BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()), bufsize); ! BufferedReader eIn = new BufferedReader(new InputStreamReader(p.getErrorStream()), bufsize); ! ! ! p.getOutputStream().close(); ! String str = ""; ! while ((str = eIn.readLine()) != null) { ! System.out.println("STDERR: " + str); //ignore this... ! } ! eIn.close(); ! while ((str = in.readLine()) != null) { ! System.out.println("STDOUT: " + str);//get the data... ! StringTokenizer tokenizer = new StringTokenizer(str); ! if(tokenizer.countTokens() ==5){ ! ! String []strings = new String[5]; ! int k = 0; ! while(tokenizer.hasMoreElements()){ ! strings[k] = tokenizer.nextToken(); ! k++; ! } ! ! if(strings[1].equals("Stmts") == false){ ! //information in the format: D:\dev_programs\test\test1.py 11 0 0% 1,2,4-23 ! System.out.println("VALID: " + str);//get the data... ! ! } ! } ! } ! in.close(); ! System.out.println("waiting"); ! p.waitFor(); ! } catch (Exception e) { ! if(p!=null){ ! p.destroy(); ! } ! e.printStackTrace(); ! } ! ! ! ! } catch (Exception e1) { ! e1.printStackTrace(); ! throw new RuntimeException(e1); ! } ! } ! ! /** ! * @param cmdLine ! * @return ! * @throws IOException ! */ ! private Process execute(String[] cmdLine) throws IOException { ! Process p; ! Properties properties = System.getProperties(); ! Set set = properties.keySet(); ! ! String []envp = new String [set.size()]; ! int j = 0; ! for (Iterator iter = set.iterator(); iter.hasNext();) { ! Object element = (Object) iter.next(); ! envp[j] = element+"="+properties.getProperty(element.toString()).toString(); ! j++; ! } ! envp = PyCoverage.setCoverageFileEnviromentVariable(envp); + p = Runtime.getRuntime().exec(cmdLine, envp); + return p; } ! ! ! ! /** ! * ! * @param file ! */ ! private List getPyFilesBelow(File file) { ! List filesToReturn = new ArrayList(); ! ! if (file.exists() == true) { ! ! if (file.isDirectory()) { ! File[] files = file.listFiles(new FileFilter() { ! ! public boolean accept(File pathname) { ! return pathname.isDirectory() || pathname.toString().endsWith(".py"); ! } ! ! }); ! for (int i = 0; i < files.length; i++) { ! filesToReturn.addAll(getPyFilesBelow(files[i])); ! } ! } else if (file.isFile()) { ! filesToReturn.add(file); ! } ! } ! return filesToReturn; ! } ! private static PyCoverage pyCoverage; ! /** * @return Returns the pyCoverage. */ public static PyCoverage getPyCoverage() { ! if (pyCoverage == null) { pyCoverage = new PyCoverage(); } *************** *** 43,51 **** } ! ! public static String getCoverageFileLocation(){ try { File pySrcPath = PydevDebugPlugin.getPySrcPath(); ! return pySrcPath.getAbsolutePath()+"/.coverage"; } catch (CoreException e) { throw new RuntimeException(e); --- 202,209 ---- } ! public static String getCoverageFileLocation() { try { File pySrcPath = PydevDebugPlugin.getPySrcPath(); ! return pySrcPath.getAbsolutePath() + "/.coverage"; } catch (CoreException e) { throw new RuntimeException(e); *************** *** 59,74 **** public static String[] setCoverageFileEnviromentVariable(String[] envp) { boolean added = false; ! for (int i = 0; i < envp.length; i++) { ! ! if (envp[i].startsWith("COVERAGE_FILE")){ ! envp[i] = "COVERAGE_FILE="+getCoverageFileLocation(); added = true; } ! } ! if (!added){ ! List list = Arrays.asList(envp); ! list.add("COVERAGE_FILE="+getCoverageFileLocation()); envp = (String[]) list.toArray(new String[0]); } --- 217,232 ---- public static String[] setCoverageFileEnviromentVariable(String[] envp) { boolean added = false; ! for (int i = 0; i < envp.length; i++) { ! ! if (envp[i].startsWith("COVERAGE_FILE")) { ! envp[i] = "COVERAGE_FILE=" + getCoverageFileLocation(); added = true; } ! } ! if (!added) { ! List list = new ArrayList(Arrays.asList(envp)); ! list.add("COVERAGE_FILE=" + getCoverageFileLocation()); envp = (String[]) list.toArray(new String[0]); } *************** *** 76,83 **** } ! ! ! ! ! } --- 234,256 ---- } + /** + * + */ + public void clearInfo() { + try { + String profileScript; + profileScript = PythonRunnerConfig.getProfileScript(); + String[] cmdLine = new String[3]; + cmdLine[0] = PydevPrefs.getDefaultInterpreter(); + cmdLine[1] = profileScript; + cmdLine[2] = "-e"; + Process p = execute(cmdLine); + p.waitFor(); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + + } ! } \ No newline at end of file |