Skip to content

Commit 70ec5bd

Browse files
authored
Merge pull request #29696 from Rostepher/no-more-flaky-flake8-5.2
[Python: flake8] Update the utils/python_lint.py script to fail with a non-zero exit code if flake8 and flake8-import-order are not installed (5.2 branch).
2 parents 641e040 + f69557a commit 70ec5bd

File tree

6 files changed

+76
-47
lines changed

6 files changed

+76
-47
lines changed

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import sys
1515
from datetime import date
16+
1617
from sphinx.highlighting import lexers
1718

1819
# If extensions (or modules to document with autodoc) are in another directory,

utils/gyb.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,16 +1133,6 @@ def execute_template(
11331133

11341134

11351135
def main():
1136-
"""
1137-
Lint this file.
1138-
>>> import sys
1139-
>>> gyb_path = os.path.realpath(__file__).replace('.pyc', '.py')
1140-
>>> sys.path.append(os.path.dirname(gyb_path))
1141-
>>> import python_lint
1142-
>>> python_lint.lint([gyb_path], verbose=False)
1143-
0
1144-
"""
1145-
11461136
import argparse
11471137
import sys
11481138

utils/line-directive

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,10 +659,6 @@ def run():
659659
>>> raw_output.close()
660660
>>> os.remove(raw_output.name)
661661
662-
Lint this file.
663-
>>> import python_lint
664-
>>> python_lint.lint([os.path.realpath(__file__)], verbose=False)
665-
0
666662
"""
667663
if len(sys.argv) <= 1:
668664
import doctest

utils/python_lint.py

Lines changed: 67 additions & 27 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.
4154

42-
return 0
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)
86+
87+
return 1
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))

validation-test/Python/python-lint.swift

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Continuous integration for the OS X Platform also runs the tests in the
2+
// iPhone, Apple TV and Apple Watch simulators. We only need to run the
3+
// Python lint once per OSX Platform test run, rather than once for each
4+
// supported Apple device.
5+
6+
// REQUIRES: OS=macosx
7+
8+
// RUN: %{python} %utils/python_lint.py

0 commit comments

Comments
 (0)