[Checkins] SVN: z3c.coverage/trunk/src/z3c/coverage/ Use subprocess instead of os.popen().

Marius Gedminas cvs-admin at zope.org
Wed Sep 5 13:02:10 UTC 2012


Log message for revision 127714:
  Use subprocess instead of os.popen().
  
  Suppress error output from svnversion.

Changed:
  U   z3c.coverage/trunk/src/z3c/coverage/README.txt
  U   z3c.coverage/trunk/src/z3c/coverage/coveragereport.py

-=-
Modified: z3c.coverage/trunk/src/z3c/coverage/README.txt
===================================================================
--- z3c.coverage/trunk/src/z3c/coverage/README.txt	2012-09-05 13:02:01 UTC (rev 127713)
+++ z3c.coverage/trunk/src/z3c/coverage/README.txt	2012-09-05 13:02:07 UTC (rev 127714)
@@ -164,6 +164,7 @@
   ...     os.path.split(z3c.coverage.__file__)[0], '__init__.py')
 
   >>> print coveragereport.syntax_highlight(filename)
+  ... # this will fail if you don't have enscript in your $PATH
   <BLANKLINE>
   <I><FONT COLOR="#B22222"># Make a package.
   </FONT></I>
@@ -171,7 +172,7 @@
 If the highlighing command is not available, no coloration is done:
 
   >>> command_orig = coveragereport.HIGHLIGHT_COMMAND
-  >>> coveragereport.HIGHLIGHT_COMMAND = 'foobar %s'
+  >>> coveragereport.HIGHLIGHT_COMMAND = ['aflkakhalkjdsjdhf']
 
   >>> print coveragereport.syntax_highlight(filename)
   # Make a package.

Modified: z3c.coverage/trunk/src/z3c/coverage/coveragereport.py
===================================================================
--- z3c.coverage/trunk/src/z3c/coverage/coveragereport.py	2012-09-05 13:02:01 UTC (rev 127713)
+++ z3c.coverage/trunk/src/z3c/coverage/coveragereport.py	2012-09-05 13:02:07 UTC (rev 127714)
@@ -40,10 +40,12 @@
 import os
 import datetime
 import cgi
+import subprocess
 
 
-HIGHLIGHT_COMMAND = ('enscript -q --footer --header -h --language=html'
-                     ' --highlight=python --color -o - "%s"')
+HIGHLIGHT_COMMAND = ['enscript', '-q', '--footer', '--header', '-h',
+                     '--language=html', '--highlight=python', '--color',
+                     '-o', '-']
 
 class CoverageNode(dict):
     """Tree node.
@@ -307,11 +309,14 @@
 
 def syntax_highlight(filename):
     """Return HTML with syntax-highlighted Python code from a file."""
-    # XXX can get painful if filenames contain unsafe characters
-    pipe = os.popen(HIGHLIGHT_COMMAND % filename,
-                    'r')
-    text = pipe.read()
-    if pipe.close():
+    # TODO: use pygments instead
+    try:
+        pipe = subprocess.Popen(HIGHLIGHT_COMMAND + [filename],
+                            stdout=subprocess.PIPE)
+        text, stderr = pipe.communicate()
+        if pipe.returncode != 0:
+            raise OSError
+    except OSError:
         # Failed to run enscript; maybe it is not installed?  Disable
         # syntax highlighting then.
         text = cgi.escape(file(filename).read())
@@ -418,7 +423,14 @@
 
 def get_svn_revision(path):
     """Return the Subversion revision number for a working directory."""
-    rev = os.popen('svnversion "%s"' % path, 'r').readline().strip()
+    devnull = open('/dev/null', 'w')
+    try:
+        pipe = subprocess.Popen(['svnversion', path], stdout=subprocess.PIPE,
+                                stderr=devnull)
+        stdout, stderr = pipe.communicate()
+        rev = stdout.strip()
+    except OSError:
+        rev = ""
     if not rev:
         rev = "UNKNOWN"
     return rev



More information about the checkins mailing list