Skip to content

[Python: black] Update the utils/python_format.py script to accept a list of input paths which narrows down the complete list of known Python sources in the project. #29718

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 1 commit into from
Feb 8, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions utils/python_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
_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",
Expand Down Expand Up @@ -100,6 +102,14 @@ def _is_package_installed(name):
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",
Expand All @@ -110,7 +120,7 @@ def parse_args():
"-v",
"--verbose",
action="store_true",
help="Also emit messages to stderr about files that were not changed",
help="Emit messages to stderr about files that were not changed.",
)

return parser.parse_args()
Expand All @@ -136,7 +146,23 @@ def main():
if args.verbose:
command.append("--verbose")

command += [str(path) for path in _get_python_sources()]
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)

Expand Down