-
-
Notifications
You must be signed in to change notification settings - Fork 652
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add lint-v2 implementation for python with black, and script to be us…
…ed in pre-commit hook (#8553) * Add lint-v2 implementation for python with black, and script to be used in pre-commit hook
- Loading branch information
1 parent
5750037
commit 0e15a34
Showing
8 changed files
with
253 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,31 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
import argparse | ||
import subprocess | ||
import sys | ||
|
||
from common import git_merge_base | ||
|
||
|
||
def create_parser() -> argparse.ArgumentParser: | ||
parser = argparse.ArgumentParser( | ||
description="Formats all python files since master with black through pants.", | ||
) | ||
parser.add_argument("-f", "--fix", action="store_true", | ||
help="Instead of erroring on files with incorrect formatting, fix those files." | ||
) | ||
return parser | ||
|
||
|
||
def main() -> None: | ||
args= create_parser().parse_args() | ||
merge_base = git_merge_base() | ||
goal = "fmt-v2" if args.fix else "lint-v2" | ||
command = ["./pants", f"--changed-parent={merge_base}", goal] | ||
process = subprocess.run(command) | ||
sys.exit(process.returncode) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,54 @@ | ||
# Copyright 2019 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from dataclasses import dataclass | ||
|
||
from pants.engine.console import Console | ||
from pants.engine.goal import Goal | ||
from pants.engine.legacy.graph import HydratedTargets | ||
from pants.engine.rules import UnionMembership, console_rule | ||
from pants.engine.selectors import Get | ||
from pants.rules.core.fmt import TargetWithSources | ||
|
||
|
||
@dataclass(frozen=True) | ||
class LintResult: | ||
exit_code: int | ||
stdout: str | ||
stderr: str | ||
|
||
|
||
class Lint(Goal): | ||
"""Lint source code.""" | ||
|
||
# TODO: make this "lint" | ||
# Blocked on https://github.com/pantsbuild/pants/issues/8351 | ||
name = 'lint-v2' | ||
|
||
|
||
@console_rule | ||
def lint(console: Console, targets: HydratedTargets, union_membership: UnionMembership) -> Lint: | ||
results = yield [ | ||
Get(LintResult, TargetWithSources, target.adaptor) | ||
for target in targets | ||
# TODO: make TargetAdaptor return a 'sources' field with an empty snapshot instead of | ||
# raising to remove the hasattr() checks here! | ||
if union_membership.is_member(TargetWithSources, target.adaptor) and hasattr(target.adaptor, "sources") | ||
] | ||
|
||
exit_code = 0 | ||
for result in results: | ||
if result.stdout: | ||
console.print_stdout(result.stdout) | ||
if result.stderr: | ||
console.print_stderr(result.stderr) | ||
if result.exit_code != 0: | ||
exit_code = result.exit_code | ||
|
||
yield Lint(exit_code) | ||
|
||
|
||
def rules(): | ||
return [ | ||
lint, | ||
] |
This file contains 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
Oops, something went wrong.