mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[Build System: Python] Cleaned-up the python_lint.py script.
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -32,25 +48,49 @@ You can install these using:
|
||||
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.
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# 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 0
|
||||
|
||||
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))
|
||||
|
||||
Reference in New Issue
Block a user