Skip to content

Commit ce7888e

Browse files
authored
Merge pull request #29309 from Rostepher/cleanup-python-lint
[NFC][Build System: Python] Cleaned-up the python_lint.py script.
2 parents f8f3324 + b904179 commit ce7888e

File tree

1 file changed

+66
-26
lines changed

1 file changed

+66
-26
lines changed

utils/python_lint.py

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
#!/usr/bin/env python
2-
# python_lint.py - Runs flake8 linting over the repository ------*- python -*-
3-
#
2+
43
# This source file is part of the Swift.org open source project
54
#
6-
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
76
# Licensed under Apache License v2.0 with Runtime Library Exception
87
#
98
# See https://swift.org/LICENSE.txt for license information
109
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1110
#
1211

13-
from __future__ import print_function
12+
13+
"""
14+
Utility script used to run the flake8 linter over all the project Python
15+
sources.
16+
"""
17+
18+
19+
from __future__ import absolute_import, print_function, unicode_literals
1420

1521
import os
1622
import subprocess
1723
import sys
1824

1925

20-
def lint(arguments, verbose=True):
21-
flake8_result = subprocess.call(
22-
[sys.executable, "-c", "import flake8; import flake8_import_order"]
23-
)
24-
if flake8_result != 0:
25-
if verbose:
26-
print("""
26+
__all__ = [
27+
'lint',
28+
]
29+
30+
31+
# -----------------------------------------------------------------------------
32+
# Constants
33+
34+
_UTILS_DIR = os.path.abspath(os.path.dirname(__file__))
35+
_PROJECT_DIR = os.path.dirname(_UTILS_DIR)
36+
37+
_REQUIRED_PACKAGES = [
38+
'flake8',
39+
'flake8-import-order',
40+
]
41+
42+
_INSTALL_FLAKE8_MESSAGE = """
2743
The flake8 and flake8-import-order Python packages are required for linting,
2844
but these were not found on your system.
2945
@@ -32,25 +48,49 @@ def lint(arguments, verbose=True):
3248
python -m pip install flake8
3349
python -m pip install flake8-import-order
3450
35-
For more help, see http://flake8.pycqa.org.""")
51+
For more help, see http://flake8.pycqa.org.
52+
"""
3653

37-
# We should be returning `flake8_result` from here. However,
38-
# some Python files lint themselves using embedded doctests,
39-
# which causes CI smoke tests to fail because the Linux nodes
40-
# do not have these modules installed.
54+
55+
# -----------------------------------------------------------------------------
56+
# Helpers
57+
58+
def _is_package_installed(name):
59+
"""Runs the pip command to check if a package is installed.
60+
"""
61+
62+
command = [
63+
sys.executable,
64+
'-m', 'pip',
65+
'show', '--quiet',
66+
name,
67+
]
68+
69+
with open(os.devnull, 'w') as devnull:
70+
status = subprocess.call(command, stderr=devnull)
71+
72+
return not status
73+
74+
75+
# -----------------------------------------------------------------------------
76+
77+
def lint(args, verbose=False):
78+
all_packages_installed = all([
79+
_is_package_installed(name)
80+
for name in _REQUIRED_PACKAGES
81+
])
82+
83+
if not all_packages_installed:
84+
if verbose:
85+
print(_INSTALL_FLAKE8_MESSAGE)
4186

4287
return 0
4388

44-
utils_directory = os.path.dirname(os.path.abspath(__file__))
45-
parent_directory = os.path.dirname(utils_directory)
46-
linting_result = subprocess.call(
47-
[sys.executable, "-m", "flake8"] + arguments,
48-
cwd=parent_directory,
49-
universal_newlines=True
50-
)
51-
return linting_result
89+
return subprocess.call(
90+
[sys.executable, '-m', 'flake8'] + args,
91+
cwd=_PROJECT_DIR,
92+
universal_newlines=True)
5293

5394

5495
if __name__ == '__main__':
55-
linting_result = lint(sys.argv[1:])
56-
sys.exit(linting_result)
96+
sys.exit(lint(sys.argv[1:], verbose=True))

0 commit comments

Comments
 (0)