Skip to content

Commit 167fea2

Browse files
authored
Merge pull request #29701 from Rostepher/python-format-script
[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.
2 parents d92f7c7 + 08225c0 commit 167fea2

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

utils/python_format.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/env python3
2+
3+
# This source file is part of the Swift.org open source project
4+
#
5+
# Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
# Licensed under Apache License v2.0 with Runtime Library Exception
7+
#
8+
# See https://swift.org/LICENSE.txt for license information
9+
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
11+
12+
"""
13+
Utility script used to run the black code formatter over all the Python scripts in the
14+
project sources.
15+
"""
16+
17+
18+
import argparse
19+
import os
20+
import subprocess
21+
import sys
22+
from pathlib import Path
23+
24+
25+
_SWIFT_PATH = Path(__file__).resolve().parents[1]
26+
27+
_KNOWN_SCRIPT_PATHS = [
28+
_SWIFT_PATH / "benchmark/scripts/Benchmark_Driver",
29+
_SWIFT_PATH / "benchmark/scripts/Benchmark_DTrace.in",
30+
_SWIFT_PATH / "benchmark/scripts/Benchmark_GuardMalloc.in",
31+
_SWIFT_PATH / "benchmark/scripts/Benchmark_RuntimeLeaksRunner.in",
32+
_SWIFT_PATH / "docs/scripts/ns-html2rst",
33+
_SWIFT_PATH / "test/Driver/Inputs/fake-toolchain/ld",
34+
_SWIFT_PATH / "utils/80+-check",
35+
_SWIFT_PATH / "utils/backtrace-check",
36+
_SWIFT_PATH / "utils/build-parser-lib",
37+
_SWIFT_PATH / "utils/build-script",
38+
_SWIFT_PATH / "utils/check-incremental",
39+
_SWIFT_PATH / "utils/coverage/coverage-build-db",
40+
_SWIFT_PATH / "utils/coverage/coverage-generate-data",
41+
_SWIFT_PATH / "utils/coverage/coverage-query-db",
42+
_SWIFT_PATH / "utils/coverage/coverage-touch-tests",
43+
_SWIFT_PATH / "utils/dev-scripts/blockifyasm",
44+
_SWIFT_PATH / "utils/dev-scripts/split-cmdline",
45+
_SWIFT_PATH / "utils/gyb",
46+
_SWIFT_PATH / "utils/line-directive",
47+
_SWIFT_PATH / "utils/PathSanitizingFileCheck",
48+
_SWIFT_PATH / "utils/recursive-lipo",
49+
_SWIFT_PATH / "utils/round-trip-syntax-test",
50+
_SWIFT_PATH / "utils/rth",
51+
_SWIFT_PATH / "utils/run-test",
52+
_SWIFT_PATH / "utils/scale-test",
53+
_SWIFT_PATH / "utils/submit-benchmark-results",
54+
_SWIFT_PATH / "utils/swift_build_support/tests/mock-distcc",
55+
_SWIFT_PATH / "utils/symbolicate-linux-fatal",
56+
_SWIFT_PATH / "utils/update-checkout",
57+
_SWIFT_PATH / "utils/viewcfg",
58+
]
59+
60+
61+
_INSTALL_BLACK_MESSAGE = """\
62+
The black Python package is required for formatting, but it was not found on
63+
your system.
64+
65+
You can install it using:
66+
67+
python3 -m pip install black
68+
69+
For more help, see https://black.readthedocs.io.
70+
"""
71+
72+
73+
def _get_python_sources():
74+
"""Returns a list of path objects for all known Python sources in the Swift
75+
project.
76+
"""
77+
78+
return list(_SWIFT_PATH.rglob("*.py")) + _KNOWN_SCRIPT_PATHS
79+
80+
81+
def _is_package_installed(name):
82+
"""Runs the pip command to check if a package is installed.
83+
"""
84+
85+
command = [
86+
sys.executable,
87+
"-m",
88+
"pip",
89+
"show",
90+
"--quiet",
91+
name,
92+
]
93+
94+
with open(os.devnull, "w") as devnull:
95+
status = subprocess.call(command, stderr=devnull)
96+
97+
return not status
98+
99+
100+
def parse_args():
101+
parser = argparse.ArgumentParser()
102+
103+
parser.add_argument(
104+
"--check",
105+
action="store_true",
106+
help="Don't format the file, just retun the status.",
107+
)
108+
109+
parser.add_argument(
110+
"-v",
111+
"--verbose",
112+
action="store_true",
113+
help="Also emit messages to stderr about files that were not changed",
114+
)
115+
116+
return parser.parse_args()
117+
118+
119+
def main():
120+
args = parse_args()
121+
122+
if not _is_package_installed("black"):
123+
print(_INSTALL_BLACK_MESSAGE)
124+
return 1
125+
126+
command = [
127+
sys.executable,
128+
"-m",
129+
"black",
130+
"--target-version",
131+
"py27",
132+
]
133+
134+
if args.check:
135+
command.append("--check")
136+
if args.verbose:
137+
command.append("--verbose")
138+
139+
command += [str(path) for path in _get_python_sources()]
140+
141+
return subprocess.call(command)
142+
143+
144+
if __name__ == "__main__":
145+
sys.exit(main())

0 commit comments

Comments
 (0)