Bug 1436639 - [lint] Make sure flake8 is run with same python as |mach lint| was, r=jmaher

This fixes a bug which can happen when the default version of python differs from
the version of python used with mach.

For example, mach explicitly looks for python2.7. This means running |mach lint -l flake8|
should also run flake8 with version 2.7. But if the default is python3, and flake8 is also
installed there, the subprocess call that invokes flake8 will run under python3. This can
lead to errors like "undefined name 'basestring'" and other 2to3 gotchas.

This patch ensures that we run:
python2.7 -m flake8

which explicitly runs flake8 against the same interpreter as mach, no matter the default.

MozReview-Commit-ID: HSuMzDsAvsW
This commit is contained in:
Andrew Halberstadt
2018-02-07 23:28:33 -05:00
parent 5e4825c312
commit 02d7261051

View File

@@ -6,9 +6,9 @@ import json
import os
import signal
import subprocess
import sys
from collections import defaultdict
import which
from mozprocess import ProcessHandlerMixin
from mozlint import result
@@ -90,23 +90,12 @@ class Flake8Process(ProcessHandlerMixin):
signal.signal(signal.SIGINT, orig)
def get_flake8_binary():
"""
Returns the path of the first flake8 binary available
if not found returns None
"""
try:
return which.which('flake8')
except which.WhichError:
return None
def _run_pip(*args):
"""
Helper function that runs pip with subprocess
"""
try:
subprocess.check_output(['pip'] + list(args),
subprocess.check_output([sys.executable, '-m', 'pip'] + list(args),
stderr=subprocess.STDOUT)
return True
except subprocess.CalledProcessError as e:
@@ -143,9 +132,8 @@ def setup(root):
def lint(paths, config, **lintargs):
binary = get_flake8_binary()
cmdargs = [
binary,
sys.executable, '-m', 'flake8',
'--format', '{"path":"%(path)s","lineno":%(row)s,'
'"column":%(col)s,"rule":"%(code)s","message":"%(text)s"}',
]