-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Python] Added a new python_format.py script to the utils directory we can use to automatically check the formatting for all the Python sources in the project. #29701
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# This source file is part of the Swift.org open source project | ||
# | ||
# 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 | ||
|
||
|
||
""" | ||
Utility script used to run the black code formatter over all the Python scripts in the | ||
project sources. | ||
""" | ||
|
||
|
||
import argparse | ||
import os | ||
import subprocess | ||
import sys | ||
from pathlib import Path | ||
|
||
|
||
_SWIFT_PATH = Path(__file__).resolve().parents[1] | ||
|
||
_KNOWN_SCRIPT_PATHS = [ | ||
_SWIFT_PATH / "benchmark/scripts/Benchmark_Driver", | ||
_SWIFT_PATH / "benchmark/scripts/Benchmark_DTrace.in", | ||
_SWIFT_PATH / "benchmark/scripts/Benchmark_GuardMalloc.in", | ||
_SWIFT_PATH / "benchmark/scripts/Benchmark_RuntimeLeaksRunner.in", | ||
_SWIFT_PATH / "docs/scripts/ns-html2rst", | ||
_SWIFT_PATH / "test/Driver/Inputs/fake-toolchain/ld", | ||
_SWIFT_PATH / "utils/80+-check", | ||
_SWIFT_PATH / "utils/backtrace-check", | ||
_SWIFT_PATH / "utils/build-parser-lib", | ||
_SWIFT_PATH / "utils/build-script", | ||
_SWIFT_PATH / "utils/check-incremental", | ||
_SWIFT_PATH / "utils/coverage/coverage-build-db", | ||
_SWIFT_PATH / "utils/coverage/coverage-generate-data", | ||
_SWIFT_PATH / "utils/coverage/coverage-query-db", | ||
_SWIFT_PATH / "utils/coverage/coverage-touch-tests", | ||
_SWIFT_PATH / "utils/dev-scripts/blockifyasm", | ||
_SWIFT_PATH / "utils/dev-scripts/split-cmdline", | ||
_SWIFT_PATH / "utils/gyb", | ||
_SWIFT_PATH / "utils/line-directive", | ||
_SWIFT_PATH / "utils/PathSanitizingFileCheck", | ||
_SWIFT_PATH / "utils/recursive-lipo", | ||
_SWIFT_PATH / "utils/round-trip-syntax-test", | ||
_SWIFT_PATH / "utils/rth", | ||
_SWIFT_PATH / "utils/run-test", | ||
_SWIFT_PATH / "utils/scale-test", | ||
_SWIFT_PATH / "utils/submit-benchmark-results", | ||
_SWIFT_PATH / "utils/swift_build_support/tests/mock-distcc", | ||
_SWIFT_PATH / "utils/symbolicate-linux-fatal", | ||
_SWIFT_PATH / "utils/update-checkout", | ||
_SWIFT_PATH / "utils/viewcfg", | ||
] | ||
|
||
|
||
_INSTALL_BLACK_MESSAGE = """\ | ||
The black Python package is required for formatting, but it was not found on | ||
your system. | ||
|
||
You can install it using: | ||
|
||
python3 -m pip install black | ||
|
||
For more help, see https://black.readthedocs.io. | ||
""" | ||
|
||
|
||
def _get_python_sources(): | ||
"""Returns a list of path objects for all known Python sources in the Swift | ||
project. | ||
""" | ||
|
||
return list(_SWIFT_PATH.rglob("*.py")) + _KNOWN_SCRIPT_PATHS | ||
|
||
|
||
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 parse_args(): | ||
parser = argparse.ArgumentParser() | ||
|
||
parser.add_argument( | ||
"--check", | ||
action="store_true", | ||
help="Don't format the file, just retun the status.", | ||
) | ||
|
||
parser.add_argument( | ||
"-v", | ||
"--verbose", | ||
action="store_true", | ||
help="Also emit messages to stderr about files that were not changed", | ||
) | ||
|
||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
args = parse_args() | ||
|
||
if not _is_package_installed("black"): | ||
print(_INSTALL_BLACK_MESSAGE) | ||
return 1 | ||
|
||
command = [ | ||
sys.executable, | ||
"-m", | ||
"black", | ||
"--target-version", | ||
"py27", | ||
] | ||
|
||
if args.check: | ||
command.append("--check") | ||
if args.verbose: | ||
command.append("--verbose") | ||
|
||
command += [str(path) for path in _get_python_sources()] | ||
|
||
return subprocess.call(command) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now the majority of our sources should remain Python 2.7 compatible. In the future we can update this to
py37
.