From 8660351ee9aeada349e1d6566e9021be5cf377cf Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 3 Jun 2019 21:48:52 +0100 Subject: [PATCH 1/2] Run Python 'coverage' tool as a module if possible This means coverage can still be called to convert its data to XML even if it's not on PATH. Closes #71. --- codecov/__init__.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/codecov/__init__.py b/codecov/__init__.py index 39d9a980..6fd9f197 100644 --- a/codecov/__init__.py +++ b/codecov/__init__.py @@ -183,6 +183,20 @@ def try_to_run(cmd): except subprocess.CalledProcessError as e: write(' Error running `%s`: %s' % (cmd, str(getattr(e, 'output', str(e))))) +def run_python_coverage(args): + """Run the Python coverage tool + + If it's importable in this Python, launch it using 'python -m'. + Otherwise, look it up on PATH like any other command. + """ + try: + import coverage + except ImportError: + # Coverage is not installed on this Python. Hope it's on PATH. + try_to_run(['coverage'] + args) + else: + # Coverage is installed on this Python. Run it as a module. + try_to_run([sys.executable, '-m', 'coverage'] + args) def remove_non_ascii(data): try: @@ -670,12 +684,12 @@ def main(*argv, **kwargs): # The `-a` option is mandatory here. If we # have a `.coverage` in the current directory, calling # without the option would delete the previous data - try_to_run('coverage combine -a') + run_python_coverage(['combine', '-a']) if os.path.exists(opj(os.getcwd(), '.coverage')) and not os.path.exists(opj(os.getcwd(), 'coverage.xml')): write(' Generating coverage xml reports for Python') # using `-i` to ignore "No source for code" error - try_to_run('coverage xml -i') + run_python_coverage(['xml', '-i']) reports.append(read(opj(os.getcwd(), 'coverage.xml'))) reports = list(filter(bool, reports)) From 39331a6afba5a7fe178e92145dcefe7a647ebfad Mon Sep 17 00:00:00 2001 From: Thomas Kluyver Date: Mon, 3 Jun 2019 22:04:23 +0100 Subject: [PATCH 2/2] Run Python coverage tool with shell=False --- codecov/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/codecov/__init__.py b/codecov/__init__.py index 6fd9f197..c1edd19a 100644 --- a/codecov/__init__.py +++ b/codecov/__init__.py @@ -177,9 +177,9 @@ def check_output(cmd, **popen_args): return output.decode('utf-8') -def try_to_run(cmd): +def try_to_run(cmd, shell=True): try: - return check_output(cmd, shell=True) + return check_output(cmd, shell=shell) except subprocess.CalledProcessError as e: write(' Error running `%s`: %s' % (cmd, str(getattr(e, 'output', str(e))))) @@ -193,10 +193,10 @@ def run_python_coverage(args): import coverage except ImportError: # Coverage is not installed on this Python. Hope it's on PATH. - try_to_run(['coverage'] + args) + try_to_run(['coverage'] + args, shell=False) else: # Coverage is installed on this Python. Run it as a module. - try_to_run([sys.executable, '-m', 'coverage'] + args) + try_to_run([sys.executable, '-m', 'coverage'] + args, shell=False) def remove_non_ascii(data): try: