[Pydev-cvs] org.python.pydev.debug/src/org/python/pydev/debug/codecoverage ErrorFileNode.java,NONE,1
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-10-19 11:12:30
|
Update of /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/codecoverage In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28341/src/org/python/pydev/debug/codecoverage Modified Files: CoverageCache.java PyCoverage.java FileNode.java Added Files: ErrorFileNode.java Log Message: --- NEW FILE: ErrorFileNode.java --- /* * Created on Oct 19, 2004 * * @author Fabio Zadrozny */ package org.python.pydev.debug.codecoverage; /** * @author Fabio Zadrozny */ public class ErrorFileNode { public Object node; public String desc; /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ public boolean equals(Object obj) { if(!(obj instanceof ErrorFileNode)){ return false; } ErrorFileNode f = (ErrorFileNode) obj; return f.node.equals(node) && f.desc == desc; } /* (non-Javadoc) * @see java.lang.Object#toString() */ public String toString() { return FileNode.getName(node.toString()) + " " +desc; } } Index: FileNode.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/codecoverage/FileNode.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** FileNode.java 15 Oct 2004 19:53:28 -0000 1.2 --- FileNode.java 19 Oct 2004 11:12:18 -0000 1.3 *************** *** 72,76 **** public static String calcCover( int stmts, int exec){ ! double v = ((double)exec) / ((double)stmts) * 100.0; DecimalFormat format = new DecimalFormat("##.#"); String str = format.format(v); --- 72,79 ---- public static String calcCover( int stmts, int exec){ ! double v = 0; ! if(stmts != 0){ ! v = ((double)exec) / ((double)stmts) * 100.0; ! } DecimalFormat format = new DecimalFormat("##.#"); String str = format.format(v); Index: PyCoverage.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/codecoverage/PyCoverage.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** PyCoverage.java 18 Oct 2004 19:13:13 -0000 1.4 --- PyCoverage.java 19 Oct 2004 11:12:18 -0000 1.5 *************** *** 12,20 **** 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; --- 12,17 ---- *************** *** 29,86 **** * 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 { ! public CoverageCache cache = new CoverageCache(); ! ! /** ! * This method contacts the python server so that we get the information on ! * the files that are below the directory passed as a parameter and stores the information ! * needed on the cache. * ! * @param file should be the root folder from where we want cache info. */ public void refreshCoverageInfo(File file, IProgressMonitor monitor) { cache.clear(); ! if(file == null){ return; } try { ! if(file.isDirectory() == false){ throw new RuntimeException("We can only get information on a dir."); } ! ! List pyFilesBelow[] = new List[]{new ArrayList(), new ArrayList()}; ! if (file.exists()) { pyFilesBelow = getPyFilesBelow(file, monitor, true); ! } ! if(pyFilesBelow[0].size() == 0){ //no files return; } ! //add the folders to the cache boolean added = false; for (Iterator it = pyFilesBelow[1].iterator(); it.hasNext();) { File f = (File) it.next(); ! if(!added){ cache.addFolder(f); added = true; ! }else{ cache.addFolder(f, f.getParentFile()); } } ! ! //now that we have the file information, we have to get the // coverage information on these files and --- 26,79 ---- * 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 { ! public CoverageCache cache = new CoverageCache(); ! /** ! * This method contacts the python server so that we get the information on the files that are below the directory passed as a parameter ! * and stores the information needed on the cache. * ! * @param file ! * should be the root folder from where we want cache info. */ public void refreshCoverageInfo(File file, IProgressMonitor monitor) { cache.clear(); ! if (file == null) { return; } try { ! if (file.isDirectory() == false) { throw new RuntimeException("We can only get information on a dir."); } ! ! List pyFilesBelow[] = new List[] { new ArrayList(), new ArrayList() }; ! if (file.exists()) { pyFilesBelow = getPyFilesBelow(file, monitor, true); ! } ! if (pyFilesBelow[0].size() == 0) { //no files return; } ! //add the folders to the cache boolean added = false; for (Iterator it = pyFilesBelow[1].iterator(); it.hasNext();) { File f = (File) it.next(); ! if (!added) { cache.addFolder(f); added = true; ! } else { cache.addFolder(f, f.getParentFile()); } } ! //now that we have the file information, we have to get the // coverage information on these files and *************** *** 99,113 **** //python coverage.py -r -m files.... ! String[] cmdLine = new String[3]; cmdLine[0] = PydevPrefs.getDefaultInterpreter(); cmdLine[1] = profileScript; ! cmdLine[2] = "-waitfor"; monitor.setTaskName("Starting shell to get info..."); monitor.worked(1); ! Process p=null; ! try { ! p = execute(cmdLine); //we have the process... --- 92,107 ---- //python coverage.py -r -m files.... ! String[] cmdLine = new String[4]; cmdLine[0] = PydevPrefs.getDefaultInterpreter(); cmdLine[1] = profileScript; ! cmdLine[2] = getCoverageFileLocation(); ! cmdLine[3] = "-waitfor"; monitor.setTaskName("Starting shell to get info..."); monitor.worked(1); ! Process p = null; ! try { ! p = execute(cmdLine); //we have the process... *************** *** 116,126 **** String files = ""; ! for (Iterator iter = pyFilesBelow[0].iterator(); iter.hasNext();) { String fStr = iter.next().toString(); ! files += fStr+"|"; } files += "\r"; monitor.setTaskName("Writing to shell..."); monitor.worked(1); p.getOutputStream().write(files.getBytes()); --- 110,123 ---- String files = ""; ! for (Iterator iter = pyFilesBelow[0].iterator(); iter.hasNext();) { String fStr = iter.next().toString(); ! files += fStr + "|"; } files += "\r"; monitor.setTaskName("Writing to shell..."); + + // System.out.println("Writing to shell... " + files); + monitor.worked(1); p.getOutputStream().write(files.getBytes()); *************** *** 131,164 **** monitor.worked(1); while ((str = in.readLine()) != null) { // System.out.println("STDOUT: " + str);//get the data... - StringTokenizer tokenizer = new StringTokenizer(str); - int nTokens = tokenizer.countTokens(); - if(nTokens == 5 || nTokens == 4){ ! String []strings = new String[5]; ! int k = 0; ! while(tokenizer.hasMoreElements()){ ! strings[k] = tokenizer.nextToken(); ! k++; ! } ! ! if(strings[1].equals("Stmts") == false && strings[0].equals("TOTAL") == 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... ! File f = new File(strings[0]); ! if(nTokens == 4){ ! cache.addFile(f, f.getParentFile(), Integer.parseInt(strings[1]), Integer.parseInt(strings[2]), ""); ! }else{ ! cache.addFile(f, f.getParentFile(), Integer.parseInt(strings[1]), Integer.parseInt(strings[2]), strings[4]); ! } ! String[] strs = f.toString().replaceAll("/", " ").replaceAll("\\\\"," ").split(" "); ! if (strs.length > 1){ ! monitor.setTaskName("Getting coverage info..."+strs[strs.length -1]); ! }else{ ! monitor.setTaskName("Getting coverage info..."+f.toString()); ! } ! monitor.worked(1); ! } ! } } in.close(); --- 128,135 ---- monitor.worked(1); while ((str = in.readLine()) != null) { + // System.out.println("STDOUT: " + str);//get the data... ! analyzeReadLine(monitor, str); } in.close(); *************** *** 168,172 **** monitor.setTaskName("Finished"); } catch (Exception e) { ! if(p!=null){ p.destroy(); } --- 139,143 ---- monitor.setTaskName("Finished"); } catch (Exception e) { ! if (p != null) { p.destroy(); } *************** *** 174,179 **** } - - } catch (Exception e1) { e1.printStackTrace(); --- 145,148 ---- *************** *** 182,188 **** } /** ! * */ public void clearInfo() { --- 151,229 ---- } + /** + * @param monitor + * @param str + */ + private void analyzeReadLine(IProgressMonitor monitor, String str) { + boolean added = false; + StringTokenizer tokenizer = new StringTokenizer(str); + int nTokens = tokenizer.countTokens(); + String[] strings = new String[nTokens]; + + int k = 0; + while (tokenizer.hasMoreElements()) { + strings[k] = tokenizer.nextToken(); + k++; + } + if (nTokens == 5 || nTokens == 4) { + + try { + if (strings[1].equals("Stmts") == false && strings[0].equals("TOTAL") == false) { + //information in the format: D:\dev_programs\test\test1.py 11 0 0% 1,2,4-23 + File f = new File(strings[0]); + if (nTokens == 4) { + cache.addFile(f, f.getParentFile(), Integer.parseInt(strings[1]), Integer.parseInt(strings[2]), ""); + added = true; + } else { + cache.addFile(f, f.getParentFile(), Integer.parseInt(strings[1]), Integer.parseInt(strings[2]), strings[4]); + added = true; + } + String[] strs = f.toString().replaceAll("/", " ").replaceAll("\\\\", " ").split(" "); + if (strs.length > 1) { + monitor.setTaskName("Getting coverage info..." + strs[strs.length - 1]); + } else { + monitor.setTaskName("Getting coverage info..." + f.toString()); + } + monitor.worked(1); + } + } catch (RuntimeException e2) { + //maybe there is something similar, but isn't quite the same, so, parse int could give us some problems... + e2.printStackTrace(); + } + } + + //we may have gotten an error in the following format: + //X:\coilib30\python\coilib\geom\Box3D.py exceptions.IndentationError: unindent does not match any outer indentation level (line + // 97) + //X:\coilib30\python\coilib\x3d\layers\cacherenderer.py exceptions.SyntaxError: invalid syntax (line 95) + // + //that is: file errorClass desc. + if (added == false) { + try { + File f = new File(strings[0]); + if(f.exists() && f.isFile()){ //this is probably an error... + cache.addFile(f, f.getParentFile(), getError(strings)); + } + + } catch (Exception e) { + e.printStackTrace(); + } + } + } /** ! * @param strings ! * @return ! */ ! private String getError(String[] strings) { ! StringBuffer ret = new StringBuffer(); ! for (int i = 1; i < strings.length; i++) { ! ret.append(strings[i]+" "); ! } ! return ret.toString(); ! } ! ! /** ! * */ public void clearInfo() { *************** *** 190,206 **** 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); } ! } ! /** * @param cmdLine --- 231,248 ---- String profileScript; profileScript = PythonRunnerConfig.getProfileScript(); ! String[] cmdLine = new String[4]; ! cmdLine[0] = PydevPrefs.getDefaultInterpreter(); ! cmdLine[1] = profileScript; ! cmdLine[2] = getCoverageFileLocation(); ! cmdLine[3] = "-e"; ! Process p = execute(cmdLine); ! p.waitFor(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } ! } ! /** * @param cmdLine *************** *** 210,221 **** private Process execute(String[] cmdLine) throws IOException { Process p; ! ! String [] envp = PyCoverage.setCoverageFileEnviromentVariable(null); ! ! p = Runtime.getRuntime().exec(cmdLine, envp); return p; } - /** * Returns the directories and python files in a list. --- 252,260 ---- private Process execute(String[] cmdLine) throws IOException { Process p; ! ! p = Runtime.getRuntime().exec(cmdLine, null); return p; } /** * Returns the directories and python files in a list. *************** *** 224,229 **** * @return tuple with files in pos 0 and folders in pos 1 */ ! public static List[] getPyFilesBelow(File file , FileFilter filter, IProgressMonitor monitor) { ! if(monitor == null){ monitor = new NullProgressMonitor(); } --- 263,268 ---- * @return tuple with files in pos 0 and folders in pos 1 */ ! public static List[] getPyFilesBelow(File file, FileFilter filter, IProgressMonitor monitor) { ! if (monitor == null) { monitor = new NullProgressMonitor(); } *************** *** 236,246 **** folders.add(file); File[] files = null; ! ! if(filter != null){ files = file.listFiles(filter); ! }else{ files = file.listFiles(); } ! for (int i = 0; i < files.length; i++) { List[] below = getPyFilesBelow(files[i], filter, monitor); --- 275,285 ---- folders.add(file); File[] files = null; ! ! if (filter != null) { files = file.listFiles(filter); ! } else { files = file.listFiles(); } ! for (int i = 0; i < files.length; i++) { List[] below = getPyFilesBelow(files[i], filter, monitor); *************** *** 252,260 **** filesToReturn.add(file); monitor.worked(1); ! monitor.setTaskName("Found:"+file.toString()); } } ! return new List[]{filesToReturn, folders}; ! } --- 291,299 ---- filesToReturn.add(file); monitor.worked(1); ! monitor.setTaskName("Found:" + file.toString()); } } ! return new List[] { filesToReturn, folders }; ! } *************** *** 269,273 **** public boolean accept(File pathname) { ! if(includeDirs) return pathname.isDirectory() || pathname.toString().endsWith(".py"); else --- 308,312 ---- public boolean accept(File pathname) { ! if (includeDirs) return pathname.isDirectory() || pathname.toString().endsWith(".py"); else *************** *** 294,298 **** try { File pySrcPath = PydevDebugPlugin.getPySrcPath(); ! return pySrcPath.getAbsolutePath() + "/.coverage"; } catch (CoreException e) { throw new RuntimeException(e); --- 333,337 ---- try { File pySrcPath = PydevDebugPlugin.getPySrcPath(); ! return "\"" + pySrcPath.getAbsolutePath() + "/.coverage" + "\""; } catch (CoreException e) { throw new RuntimeException(e); *************** *** 300,340 **** } - /** - * @param envp - * @return - */ - public static String[] setCoverageFileEnviromentVariable(String[] envp) { - if(envp == null){ - Properties properties = System.getProperties(); - Set set = properties.keySet(); - - 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++; - } - - } - - 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]); - } - return envp; - } - - } \ No newline at end of file --- 339,341 ---- Index: CoverageCache.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev.debug/src/org/python/pydev/debug/codecoverage/CoverageCache.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** CoverageCache.java 18 Oct 2004 19:13:13 -0000 1.3 --- CoverageCache.java 19 Oct 2004 11:12:18 -0000 1.4 *************** *** 106,114 **** files.put(node, fileNode); } public List getFiles(Object node) throws NodeNotFoudException{ FolderNode folderNode = (FolderNode) getFolder(node); if (folderNode == null){ ! FileNode fileNode = (FileNode) getFile(node); if (fileNode == null){ throw new NodeNotFoudException("The node has not been found: "+node.toString()); --- 106,137 ---- files.put(node, fileNode); } + + /** + * + * @param node + * @param parent + * @param stmts + * @param exec + * @param notExecuted + */ + public void addFile(Object node, Object parent, String desc) { + FolderNode folderNode = (FolderNode) getFolder(parent); + + if (folderNode == null){ + throw new RuntimeException("A file node ("+node.toString()+")MUST have a related folder node."); + } + + ErrorFileNode fileNode = new ErrorFileNode(); + fileNode.node = node; + fileNode.desc = desc; + + folderNode.files.put(node, fileNode); + files.put(node, fileNode); + } public List getFiles(Object node) throws NodeNotFoudException{ FolderNode folderNode = (FolderNode) getFolder(node); if (folderNode == null){ ! Object fileNode = getFile(node); if (fileNode == null){ throw new NodeNotFoudException("The node has not been found: "+node.toString()); *************** *** 172,179 **** for (Iterator it = list.iterator(); it.hasNext();) { ! FileNode element = (FileNode) it.next(); buffer.append(element.toString()+"\n"); ! totalExecuted += element.exec; ! totalStmts += element.stmts; } --- 195,204 ---- for (Iterator it = list.iterator(); it.hasNext();) { ! Object element = it.next(); buffer.append(element.toString()+"\n"); ! if(element instanceof FileNode){ //it may have been an error node... ! totalExecuted += ((FileNode)element).exec; ! totalStmts += ((FileNode)element).stmts; ! } } |