- Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathpython_format.py
executable file
·187 lines (146 loc) · 5.03 KB
/
python_format.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/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.
"""
importargparse
importos
importsubprocess
importsys
frompathlibimportPath
_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_QuickCheck.in",
_SWIFT_PATH/"benchmark/scripts/Benchmark_RuntimeLeaksRunner.in",
_SWIFT_PATH/"benchmark/scripts/run_smoke_bench",
_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-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.
"""
returnlist(_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,
]
withopen(os.devnull, "w") asdevnull:
status=subprocess.call(command, stderr=devnull)
returnnotstatus
defparse_args():
parser=argparse.ArgumentParser()
parser.add_argument(
"paths",
type=Path,
metavar="PATH",
nargs="*",
help="Source path to format.",
)
parser.add_argument(
"--check",
action="store_true",
help="Don't write the files back, just return the status.",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Emit messages to stderr about files that were not changed.",
)
parser.add_argument(
"--diff",
action="store_true",
help="Don't write the files back, just output a diff for each file on stdout.",
)
parser.add_argument(
"-S",
"--skip-string-normalization",
action="store_true",
help="Don't normalize string quotes or prefixes.",
)
returnparser.parse_args()
defmain():
args=parse_args()
ifnot_is_package_installed("black"):
print(_INSTALL_BLACK_MESSAGE)
return1
command= [
sys.executable,
"-m",
"black",
"--target-version",
"py38",
]
ifargs.check:
command.append("--check")
ifargs.verbose:
command.append("--verbose")
ifargs.diff:
command.append("--diff")
ifargs.skip_string_normalization:
command.append("--skip-string-normalization")
requested_paths= [path.resolve() forpathinargs.paths]
# Narrow down the set of paths to format to only those paths which are either
# included in the set of requested paths or are subpaths of the requested paths.
format_paths= {
known_path
forpathinrequested_paths
forknown_pathin_get_python_sources()
ifpath==known_pathorpathinknown_path.parents
}
# Add requested paths that exists, but aren't included in the format set.
forpathinrequested_paths:
ifpathnotinformat_pathsandpath.exists():
format_paths.add(path)
command+=sorted([str(path) forpathinformat_paths])
returnsubprocess.call(command)
if__name__=="__main__":
sys.exit(main())