[checker] try to improve the sources directories given to pycoverage draft
authorDavid Douard <david.douard@logilab.fr>
Fri, 22 Jan 2016 17:08:39 +0100
changeset 1860 61e4fff20c97
parent 1859 b604269e3d23
child 1861 651a4a4dc1dd
[checker] try to improve the sources directories given to pycoverage try to detect proper source directories by running a small python snippet in a subprocess, so that it is executed in the virtualenv (if any) instead of the narval bot execution environment
__pkginfo__.py
_narval/checkers/apycot/python.py
--- a/__pkginfo__.py	Tue Jan 05 17:00:15 2016 +0100
+++ b/__pkginfo__.py	Fri Jan 22 17:08:39 2016 +0100
@@ -25,6 +25,7 @@
                'cubicweb-narval': '>= 4.2',
                'Pygments': None,
                'lxml': None,
+               'six': None,
                }
 __recommends__ = {'cubicweb-tracker': None,
                   'cubicweb-nosylist': '>= 0.5.0',
--- a/_narval/checkers/apycot/python.py	Tue Jan 05 17:00:15 2016 +0100
+++ b/_narval/checkers/apycot/python.py	Fri Jan 22 17:08:39 2016 +0100
@@ -12,13 +12,17 @@
 import os
 import sys
 import re
+import shlex
 from commands import getoutput
 from os.path import join, exists
 from test import test_support
 from warnings import warn
+from subprocess import check_output
 from lxml import etree
 import coverage
 
+import six
+
 from logilab.common.testlib import find_tests, DEFAULT_PREFIXES
 from logilab.common.modutils import get_module_files
 from logilab.common.decorators import cached
@@ -77,6 +81,31 @@
             return path
     return test.project_path(subpath=True)
 
+def find_module(pkg, python):
+    if isinstance(python, six.string_types):
+        python = shlex.split(python)
+    return check_output(python + ['-c', '''
+import imp
+try:
+    print(imp.find_module('{0}')[1])
+except ImportError:
+    print('')
+    '''.format(pkg)]).strip()
+
+def pyinstall_packages_path(test, python):
+    pkginfopath = join(test.tmpdir, test.project_path(), '__pkginfo__.py')
+    result = []
+    if exists(pkginfopath):
+        pkginfo = {}
+        with open(pkginfopath) as f:
+            exec(f.read(), pkginfo)
+        if 'packages' in pkginfo:
+            import imp
+            for pkg in pkginfo['packages']:
+                # do this in a subprocess since we execute our tests
+                # in a virtualenv...
+                result.append(find_module(pkg, python))
+    return [p.strip() for p in result if p]
 
 class PythonSyntaxChecker(AbstractFilteredFileChecker):
     """check syntax of python file
@@ -398,13 +427,17 @@
     def get_command(self, command, python):
         python = [python, '-W', 'ignore']
         if self.enable_coverage():
-            excludes = self.options.get("pycoverage_excludes", [])
-            if not isinstance(excludes, list):
-                excludes = [excludes]
+            installpath = pyinstall_path(self.test)
+            allpath = [installpath] + pyinstall_packages_path(self.test, python)
             python += ['-m', 'coverage', 'run', '-a', '--branch',
-                       '--source=%s' % pyinstall_path(self.test)]
+                       '--source=%s' % ','.join(allpath)]
+
+            excludes = self.options.get("pycoverage_excludes", [])
             if excludes:
+                if not isinstance(excludes, list):
+                    excludes = [excludes]
                 python += ['--omit', ','.join(excludes)]
+
         return python + command
 
 register('checker', PyUnitTestChecker)