Bug 1460402 - Update the flake8 support to use the pip class r?ahal draft
authorSylvestre Ledru <sledru@mozilla.com>
Wed, 09 May 2018 21:55:21 +0200
changeset 794010 d1523f128f639284a8f4be34b78285929d7fed92
parent 794009 c3ff29fc5dfb4dc1b867f7bdf66b511e939f8d79
child 794011 c546ee5a34880a8057c70180f00280a119254317
push id109563
push userbmo:sledru@mozilla.com
push dateFri, 11 May 2018 08:19:13 +0000
reviewersahal
bugs1460402
milestone62.0a1
Bug 1460402 - Update the flake8 support to use the pip class r?ahal MozReview-Commit-ID: 9BWa0cnWJdS
tools/lint/python/__init__.py
tools/lint/python/flake8.py
--- a/tools/lint/python/__init__.py
+++ b/tools/lint/python/__init__.py
@@ -1,22 +1,22 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 import json
 import os
 import signal
-import subprocess
 from collections import defaultdict
 
 import which
 from mozprocess import ProcessHandlerMixin
 
 from mozlint import result
+from mozlint.util import pip
 from mozlint.pathutils import get_ancestors_by_name
 
 
 here = os.path.abspath(os.path.dirname(__file__))
 FLAKE8_REQUIREMENTS_PATH = os.path.join(here, 'flake8_requirements.txt')
 
 FLAKE8_NOT_FOUND = """
 Could not find flake8! Install flake8 and try again.
@@ -100,54 +100,28 @@ def get_flake8_binary():
         return binary
 
     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),
-                                stderr=subprocess.STDOUT)
-        return True
-    except subprocess.CalledProcessError as e:
-        print(e.output)
-        return False
-
-
-def reinstall_flake8():
-    """
-    Try to install flake8 at the target version, returns True on success
-    otherwise prints the otuput of the pip command and returns False
-    """
-    if _run_pip('install', '-U',
-                '--require-hashes', '-r',
-                FLAKE8_REQUIREMENTS_PATH):
-        return True
-
-    return False
-
-
 def run_process(config, cmd):
     proc = Flake8Process(config, cmd)
     proc.run()
     try:
         proc.wait()
     except KeyboardInterrupt:
         proc.kill()
 
 
 def lint(paths, config, **lintargs):
 
-    if not reinstall_flake8():
+    if not pip.reinstall_program(FLAKE8_REQUIREMENTS_PATH):
         print(FLAKE8_INSTALL_ERROR)
         return 1
 
     binary = get_flake8_binary()
 
     cmdargs = [
         binary,
         '--format', '{"path":"%(path)s","lineno":%(row)s,'
--- a/tools/lint/python/flake8.py
+++ b/tools/lint/python/flake8.py
@@ -1,23 +1,23 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 import json
 import os
 import platform
 import signal
-import subprocess
 import sys
 from collections import defaultdict
 
 from mozprocess import ProcessHandlerMixin
 
 from mozlint import result
+from mozlint.util import pip
 from mozlint.pathutils import get_ancestors_by_name
 
 
 here = os.path.abspath(os.path.dirname(__file__))
 FLAKE8_REQUIREMENTS_PATH = os.path.join(here, 'flake8_requirements.txt')
 
 FLAKE8_NOT_FOUND = """
 Could not find flake8! Install flake8 and try again.
@@ -93,54 +93,28 @@ class Flake8Process(ProcessHandlerMixin)
     def run(self, *args, **kwargs):
         # flake8 seems to handle SIGINT poorly. Handle it here instead
         # so we can kill the process without a cryptic traceback.
         orig = signal.signal(signal.SIGINT, signal.SIG_IGN)
         ProcessHandlerMixin.run(self, *args, **kwargs)
         signal.signal(signal.SIGINT, orig)
 
 
-def _run_pip(*args):
-    """
-    Helper function that runs pip with subprocess
-    """
-    try:
-        subprocess.check_output([os.path.join(bindir, 'pip')] + list(args),
-                                stderr=subprocess.STDOUT)
-        return True
-    except subprocess.CalledProcessError as e:
-        print(e.output)
-        return False
-
-
-def reinstall_flake8():
-    """
-    Try to install flake8 at the target version, returns True on success
-    otherwise prints the otuput of the pip command and returns False
-    """
-    if _run_pip('install', '-U',
-                '--require-hashes', '-r',
-                FLAKE8_REQUIREMENTS_PATH):
-        return True
-
-    return False
-
-
 def run_process(config, cmd):
     proc = Flake8Process(config, cmd)
     proc.run()
     try:
         proc.wait()
     except KeyboardInterrupt:
         proc.kill()
         return 1
 
 
 def setup(root):
-    if not reinstall_flake8():
+    if not pip.reinstall_program(FLAKE8_REQUIREMENTS_PATH):
         print(FLAKE8_INSTALL_ERROR)
         return 1
 
 
 def lint(paths, config, **lintargs):
     # TODO don't store results in a global
     global results
     results = []