forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_format.py
171 lines (132 loc) · 4.59 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
#!/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_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-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(
"paths",
type=Path,
metavar="PATH",
nargs="*",
help="Source path to format.",
)
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="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")
requested_paths = [path.resolve() for path in args.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
for path in requested_paths
for known_path in _get_python_sources()
if path == known_path or path in known_path.parents
}
# Add requested paths that exists, but aren't included in the format set.
for path in requested_paths:
if path not in format_paths and path.exists():
format_paths.add(path)
command += sorted([str(path) for path in format_paths])
return subprocess.call(command)
if __name__ == "__main__":
sys.exit(main())