Skip to content

[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). #29696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import sys
from datetime import date

from sphinx.highlighting import lexers

# If extensions (or modules to document with autodoc) are in another directory,
Expand Down
10 changes: 0 additions & 10 deletions utils/gyb.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,16 +1133,6 @@ def execute_template(


def main():
"""
Lint this file.
>>> import sys
>>> gyb_path = os.path.realpath(__file__).replace('.pyc', '.py')
>>> sys.path.append(os.path.dirname(gyb_path))
>>> import python_lint
>>> python_lint.lint([gyb_path], verbose=False)
0
"""

import argparse
import sys

Expand Down
4 changes: 0 additions & 4 deletions utils/line-directive
Original file line number Diff line number Diff line change
Expand Up @@ -659,10 +659,6 @@ def run():
>>> raw_output.close()
>>> os.remove(raw_output.name)
Lint this file.
>>> import python_lint
>>> python_lint.lint([os.path.realpath(__file__)], verbose=False)
0
"""
if len(sys.argv) <= 1:
import doctest
Expand Down
94 changes: 67 additions & 27 deletions utils/python_lint.py
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
#!/usr/bin/env python
# python_lint.py - Runs flake8 linting over the repository ------*- python -*-
#

# This source file is part of the Swift.org open source project
#
# Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#

from __future__ import print_function

"""
Utility script used to run the flake8 linter over all the project Python
sources.
"""


from __future__ import absolute_import, print_function, unicode_literals

import os
import subprocess
import sys


def lint(arguments, verbose=True):
flake8_result = subprocess.call(
[sys.executable, "-c", "import flake8; import flake8_import_order"]
)
if flake8_result != 0:
if verbose:
print("""
__all__ = [
'lint',
]


# -----------------------------------------------------------------------------
# Constants

_UTILS_DIR = os.path.abspath(os.path.dirname(__file__))
_PROJECT_DIR = os.path.dirname(_UTILS_DIR)

_REQUIRED_PACKAGES = [
'flake8',
'flake8-import-order',
]

_INSTALL_FLAKE8_MESSAGE = """
The flake8 and flake8-import-order Python packages are required for linting,
but these were not found on your system.
Expand All @@ -32,25 +48,49 @@ def lint(arguments, verbose=True):
python -m pip install flake8
python -m pip install flake8-import-order
For more help, see http://flake8.pycqa.org.""")
For more help, see http://flake8.pycqa.org.
"""

# We should be returning `flake8_result` from here. However,
# some Python files lint themselves using embedded doctests,
# which causes CI smoke tests to fail because the Linux nodes
# do not have these modules installed.

return 0
# -----------------------------------------------------------------------------
# Helpers

def _is_package_installed(name):
"""Runs the pip command to check if a package is installed.
"""

command = [
sys.executable,
'-m', 'pip',
'show', '--quiet',
name,
]

with open(os.devnull, 'w') as devnull:
status = subprocess.call(command, stderr=devnull)

return not status


# -----------------------------------------------------------------------------

def lint(args, verbose=False):
all_packages_installed = all([
_is_package_installed(name)
for name in _REQUIRED_PACKAGES
])

if not all_packages_installed:
if verbose:
print(_INSTALL_FLAKE8_MESSAGE)

return 1

utils_directory = os.path.dirname(os.path.abspath(__file__))
parent_directory = os.path.dirname(utils_directory)
linting_result = subprocess.call(
[sys.executable, "-m", "flake8"] + arguments,
cwd=parent_directory,
universal_newlines=True
)
return linting_result
return subprocess.call(
[sys.executable, '-m', 'flake8'] + args,
cwd=_PROJECT_DIR,
universal_newlines=True)


if __name__ == '__main__':
linting_result = lint(sys.argv[1:])
sys.exit(linting_result)
sys.exit(lint(sys.argv[1:], verbose=True))
6 changes: 0 additions & 6 deletions validation-test/Python/python-lint.swift

This file was deleted.

8 changes: 8 additions & 0 deletions validation-test/Python/python_lint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Continuous integration for the OS X Platform also runs the tests in the
// iPhone, Apple TV and Apple Watch simulators. We only need to run the
// Python lint once per OSX Platform test run, rather than once for each
// supported Apple device.

// REQUIRES: OS=macosx

// RUN: %{python} %utils/python_lint.py