[Checkins] SVN: z3c.coverage/trunk/ New option: --path-alias, so you can use .coverage files created elsewhere.

Marius Gedminas cvs-admin at zope.org
Fri Sep 7 13:40:25 UTC 2012


Log message for revision 127775:
  New option: --path-alias, so you can use .coverage files created elsewhere.
  
  Also make the tests pass run on anybody else's machine.  ;)

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

-=-
Modified: z3c.coverage/trunk/CHANGES.txt
===================================================================
--- z3c.coverage/trunk/CHANGES.txt	2012-09-07 13:23:46 UTC (rev 127774)
+++ z3c.coverage/trunk/CHANGES.txt	2012-09-07 13:40:21 UTC (rev 127775)
@@ -5,7 +5,7 @@
 1.3.1 (unreleased)
 ------------------
 
-- Nothing changed yet.
+- ``coveragereport`` now accepts ``--path-alias``.
 
 
 1.3.0 (2012-09-06)

Modified: z3c.coverage/trunk/src/z3c/coverage/README.txt
===================================================================
--- z3c.coverage/trunk/src/z3c/coverage/README.txt	2012-09-07 13:23:46 UTC (rev 127774)
+++ z3c.coverage/trunk/src/z3c/coverage/README.txt	2012-09-07 13:40:21 UTC (rev 127775)
@@ -51,8 +51,14 @@
     ...     os.path.dirname(z3c.coverage.__file__), 'sampleinput.pickle')
 
     >>> from z3c.coverage import coveragereport
-    >>> coveragereport.main([inputFile, outputDir, '-q',
-    ...                      '--strip-prefix=/home/mg/src/z3c.coverage/src'])
+    >>> coveragereport.main([
+    ...     inputFile, outputDir, '-q',
+    ...     '--path-alias=/home/mg/src/z3c.coverage/src/z3c/coverage=%s'
+    ...         % os.path.dirname(z3c.coverage.__file__),
+    ...     '--strip-prefix=%s'
+    ...         % os.path.dirname(os.path.dirname(
+    ...             os.path.dirname(z3c.coverage.__file__))),
+    ... ])
     >>> print '\n'.join(sorted(os.listdir(outputDir)))
     all.html
     z3c.coverage.__init__.html

Modified: z3c.coverage/trunk/src/z3c/coverage/coveragereport.py
===================================================================
--- z3c.coverage/trunk/src/z3c/coverage/coveragereport.py	2012-09-07 13:23:46 UTC (rev 127774)
+++ z3c.coverage/trunk/src/z3c/coverage/coveragereport.py	2012-09-07 13:40:21 UTC (rev 127775)
@@ -240,7 +240,7 @@
     return root
 
 
-def create_tree_from_coverage(cov, strip_prefix=None):
+def create_tree_from_coverage(cov, strip_prefix=None, path_aliases=None):
     """Create a tree with coverage statistics.
 
     Takes a coverage.coverage() instance.
@@ -248,6 +248,9 @@
     Returns the root node of the tree.
     """
     root = CoverageNode()
+    if path_aliases:
+        apply_path_aliases(cov, dict([alias.partition('=')[::2]
+                                      for alias in path_aliases]))
     for filename in cov.data.measured_files():
         if strip_prefix and filename.startswith(strip_prefix):
             short_name = filename[len(strip_prefix):]
@@ -262,6 +265,24 @@
     return root
 
 
+def apply_path_aliases(cov, aliases):
+    """Adjust filenames in coverage data."""
+    # XXX: fragile: we're touching the internal data structures directly
+    aliases = aliases.items()
+    aliases.sort(key=lambda (k, v): len(k), reverse=True) # longest key first
+    def fixup_filename(filename):
+        for alias, local in aliases:
+            return local + filename[len(alias):]
+        return filename
+    cov.data.lines = map_dict_keys(fixup_filename, cov.data.lines)
+    cov.data.arcs = map_dict_keys(fixup_filename, cov.data.arcs)
+
+
+def map_dict_keys(fn, d):
+    """Transform {x: y} to {fn(x): y}."""
+    return dict((fn(k), v) for k, v in d.items())
+
+
 def traverse_tree(tree, index, function):
     """Preorder traversal of a tree.
 
@@ -512,7 +533,8 @@
         import coverage
         cov = coverage.coverage(data_file=path, config_file=False)
         cov.load()
-        tree = create_tree_from_coverage(cov, strip_prefix=opts.strip_prefix)
+        tree = create_tree_from_coverage(cov, strip_prefix=opts.strip_prefix,
+                                         path_aliases=opts.path_alias)
         return tree
 
 
@@ -566,6 +588,9 @@
                       action='store_const', const=1, dest='verbose', default=1)
     parser.add_option('--strip-prefix', metavar='PREFIX',
                       help='strip base directory from filenames loaded from .coverage')
+    parser.add_option('--path-alias', metavar='PATH=LOCALPATH',
+                      help='define path mappings for filenames loaded from .coverage',
+                      action='append')
 
     if args is None:
         args = sys.argv[1:]



More information about the checkins mailing list