Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pantsbuild/pants into remove-leve…
Browse files Browse the repository at this point in the history
…n-option
  • Loading branch information
Eric-Arellano committed Aug 15, 2020
2 parents ea3cd6b + 659ffd6 commit 7170332
Show file tree
Hide file tree
Showing 35 changed files with 718 additions and 483 deletions.
2 changes: 1 addition & 1 deletion pants.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ root_patterns = [

[python-setup]
requirement_constraints = "3rdparty/python/constraints.txt"
resolve_all_constraints = true
resolve_all_constraints = "nondeployables"

[black]
config = "pyproject.toml"
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.0.0.dev7
2.0.0.dev8
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ async def create_python_awslambda(
# available and matching the AMI platform.
"--resolve-local-platforms",
],
for_deployable_binary=True,
)
)

Expand Down
13 changes: 3 additions & 10 deletions src/python/pants/backend/python/dependency_inference/rules_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,14 @@ def test_infer_python_conftests(self) -> None:
)

self.create_file("src/python/root/conftest.py")
self.add_to_build_file("src/python/root", "python_library()")
self.add_to_build_file("src/python/root", "python_library(sources=['conftest.py'])")

self.create_file("src/python/root/mid/conftest.py")
self.add_to_build_file("src/python/root/mid", "python_library()")
self.add_to_build_file("src/python/root/mid", "python_library(sources=['conftest.py'])")

self.create_file("src/python/root/mid/leaf/conftest.py")
self.create_file("src/python/root/mid/leaf/this_is_a_test.py")
self.add_to_build_file(
"src/python/root/mid/leaf", "python_tests()\npython_library(name='conftest')"
)
self.add_to_build_file("src/python/root/mid/leaf", "python_tests()")

def run_dep_inference(address: Address) -> InferredDependencies:
target = self.request_single_product(
Expand All @@ -197,11 +195,6 @@ def run_dep_inference(address: Address) -> InferredDependencies:
[
Address("src/python/root", relative_file_path="conftest.py", target_name="root"),
Address("src/python/root/mid", relative_file_path="conftest.py", target_name="mid"),
Address(
"src/python/root/mid/leaf",
relative_file_path="conftest.py",
target_name="conftest",
),
],
sibling_dependencies_inferrable=False,
)
57 changes: 22 additions & 35 deletions src/python/pants/backend/python/lint/bandit/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,10 @@
PexRequirements,
)
from pants.backend.python.target_types import PythonInterpreterCompatibility, PythonSources
from pants.core.goals.lint import (
LintRequest,
LintResult,
LintResultFile,
LintResults,
LintSubsystem,
)
from pants.core.goals.lint import LintReport, LintRequest, LintResult, LintResults, LintSubsystem
from pants.core.util_rules import source_files, stripped_source_files
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.engine.fs import (
Digest,
DigestSubset,
GlobMatchErrorBehavior,
MergeDigests,
PathGlobs,
Snapshot,
)
from pants.engine.fs import Digest, DigestSubset, GlobMatchErrorBehavior, MergeDigests, PathGlobs
from pants.engine.process import FallibleProcessResult
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.engine.target import FieldSet
Expand Down Expand Up @@ -58,13 +45,13 @@ class BanditPartition:


def generate_args(
*, source_files: SourceFiles, bandit: Bandit, output_file: Optional[str]
*, source_files: SourceFiles, bandit: Bandit, report_file_name: Optional[str]
) -> Tuple[str, ...]:
args = []
if bandit.config is not None:
args.append(f"--config={bandit.config}")
if output_file:
args.append(f"--output={output_file}")
if report_file_name:
args.append(f"--output={report_file_name}")
args.extend(bandit.args)
args.extend(source_files.files)
return tuple(args)
Expand Down Expand Up @@ -111,13 +98,9 @@ async def bandit_lint_partition(
address_references = ", ".join(
sorted(field_set.address.spec for field_set in partition.field_sets)
)
report_path = (
lint_subsystem.reports_dir / "bandit_report.txt" if lint_subsystem.reports_dir else None
)
report_file_name = "bandit_report.txt" if lint_subsystem.reports_dir else None
args = generate_args(
source_files=source_files,
bandit=bandit,
output_file=report_path.name if report_path else None,
source_files=source_files, bandit=bandit, report_file_name=report_file_name
)

result = await Get(
Expand All @@ -130,22 +113,26 @@ async def bandit_lint_partition(
f"Run Bandit on {pluralize(len(partition.field_sets), 'target')}: "
f"{address_references}."
),
output_files=(report_path.name,) if report_path else None,
output_files=(report_file_name,) if report_file_name else None,
),
)

results_file = None
if report_path:
report_file_snapshot = await Get(
Snapshot, DigestSubset(result.output_digest, PathGlobs([report_path.name]))
report = None
if report_file_name:
report_digest = await Get(
Digest,
DigestSubset(
result.output_digest,
PathGlobs(
[report_file_name],
glob_match_error_behavior=GlobMatchErrorBehavior.warn,
description_of_origin="Bandit report file",
),
),
)
if len(report_file_snapshot.files) != 1:
raise Exception(f"Unexpected report file snapshot: {report_file_snapshot.files}")
results_file = LintResultFile(output_path=report_path, digest=report_file_snapshot.digest)
report = LintReport(report_file_name, report_digest)

return LintResult.from_fallible_process_result(
result, linter_name="Bandit", results_file=results_file
)
return LintResult.from_fallible_process_result(result, linter_name="Bandit", report=report)


@rule(desc="Lint using Bandit")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ def test_passing_source(self) -> None:
assert len(result) == 1
assert result[0].exit_code == 0
assert "No issues identified." in result[0].stdout.strip()
assert result[0].results_file is None
assert result[0].report is None

def test_failing_source(self) -> None:
target = self.make_target([self.bad_source])
result = self.run_bandit([target])
assert len(result) == 1
assert result[0].exit_code == 1
assert "Issue: [B303:blacklist] Use of insecure MD2, MD4, MD5" in result[0].stdout
assert result[0].results_file is None
assert result[0].report is None

def test_mixed_sources(self) -> None:
target = self.make_target([self.good_source, self.bad_source])
Expand All @@ -88,7 +88,7 @@ def test_mixed_sources(self) -> None:
assert result[0].exit_code == 1
assert "good.py" not in result[0].stdout
assert "Issue: [B303:blacklist] Use of insecure MD2, MD4, MD5" in result[0].stdout
assert result[0].results_file is None
assert result[0].report is None

def test_multiple_targets(self) -> None:
targets = [
Expand All @@ -100,7 +100,7 @@ def test_multiple_targets(self) -> None:
assert result[0].exit_code == 1
assert "good.py" not in result[0].stdout
assert "Issue: [B303:blacklist] Use of insecure MD2, MD4, MD5" in result[0].stdout
assert result[0].results_file is None
assert result[0].report is None

@skip_unless_python27_and_python3_present
def test_uses_correct_python_version(self) -> None:
Expand Down Expand Up @@ -138,15 +138,15 @@ def test_respects_config_file(self) -> None:
assert len(result) == 1
assert result[0].exit_code == 0
assert "No issues identified." in result[0].stdout.strip()
assert result[0].results_file is None
assert result[0].report is None

def test_respects_passthrough_args(self) -> None:
target = self.make_target([self.bad_source])
result = self.run_bandit([target], passthrough_args="--skip B303")
assert len(result) == 1
assert result[0].exit_code == 0
assert "No issues identified." in result[0].stdout.strip()
assert result[0].results_file is None
assert result[0].report is None

def test_skip(self) -> None:
target = self.make_target([self.bad_source])
Expand All @@ -166,18 +166,18 @@ def test_3rdparty_plugin(self) -> None:
assert len(result) == 1
assert result[0].exit_code == 1
assert "Issue: [C100:hardcoded_aws_key]" in result[0].stdout
assert result[0].results_file is None
assert result[0].report is None

def test_output_file(self) -> None:
def test_report_file(self) -> None:
target = self.make_target([self.bad_source])
result = self.run_bandit([target], additional_args=["--lint-reports-dir='.'"])
assert len(result) == 1
assert result[0].exit_code == 1
assert result[0].stdout.strip() == ""
assert result[0].results_file is not None
output_files = self.request_single_product(DigestContents, result[0].results_file.digest)
assert len(output_files) == 1
assert result[0].report is not None
report_files = self.request_single_product(DigestContents, result[0].report.digest)
assert len(report_files) == 1
assert (
"Issue: [B303:blacklist] Use of insecure MD2, MD4, MD5"
in output_files[0].content.decode()
in report_files[0].content.decode()
)
59 changes: 23 additions & 36 deletions src/python/pants/backend/python/lint/flake8/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,10 @@
PexRequirements,
)
from pants.backend.python.target_types import PythonInterpreterCompatibility, PythonSources
from pants.core.goals.lint import (
LintRequest,
LintResult,
LintResultFile,
LintResults,
LintSubsystem,
)
from pants.core.goals.lint import LintReport, LintRequest, LintResult, LintResults, LintSubsystem
from pants.core.util_rules import source_files, stripped_source_files
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.engine.fs import (
Digest,
DigestSubset,
GlobMatchErrorBehavior,
MergeDigests,
PathGlobs,
Snapshot,
)
from pants.engine.fs import Digest, DigestSubset, GlobMatchErrorBehavior, MergeDigests, PathGlobs
from pants.engine.process import FallibleProcessResult
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.engine.target import FieldSet
Expand Down Expand Up @@ -58,13 +45,13 @@ class Flake8Partition:


def generate_args(
*, source_files: SourceFiles, flake8: Flake8, output_file: Optional[str]
*, source_files: SourceFiles, flake8: Flake8, report_file_name: Optional[str]
) -> Tuple[str, ...]:
args = []
if flake8.config:
args.append(f"--config={flake8.config}")
if output_file:
args.append(f"--output-file={output_file}")
if report_file_name:
args.append(f"--output-file={report_file_name}")
args.extend(flake8.args)
args.extend(source_files.files)
return tuple(args)
Expand Down Expand Up @@ -112,40 +99,40 @@ async def flake8_lint_partition(
address_references = ", ".join(
sorted(field_set.address.spec for field_set in partition.field_sets)
)
report_path = (
lint_subsystem.reports_dir / "flake8_report.txt" if lint_subsystem.reports_dir else None
)
report_file_name = "flake8_report.txt" if lint_subsystem.reports_dir else None

result = await Get(
FallibleProcessResult,
PexProcess(
requirements_pex,
argv=generate_args(
source_files=source_files,
flake8=flake8,
output_file=report_path.name if report_path else None,
source_files=source_files, flake8=flake8, report_file_name=report_file_name,
),
input_digest=input_digest,
output_files=(report_path.name,) if report_path else None,
output_files=(report_file_name,) if report_file_name else None,
description=(
f"Run Flake8 on {pluralize(len(partition.field_sets), 'target')}: "
f"{address_references}."
f"{address_references}"
),
),
)

results_file = None
if report_path:
report_file_snapshot = await Get(
Snapshot, DigestSubset(result.output_digest, PathGlobs([report_path.name]))
report = None
if report_file_name:
report_digest = await Get(
Digest,
DigestSubset(
result.output_digest,
PathGlobs(
[report_file_name],
glob_match_error_behavior=GlobMatchErrorBehavior.warn,
description_of_origin="Flake8 report file",
),
),
)
if len(report_file_snapshot.files) != 1:
raise Exception(f"Unexpected report file snapshot: {report_file_snapshot.files}")
results_file = LintResultFile(output_path=report_path, digest=report_file_snapshot.digest)
report = LintReport(report_file_name, report_digest)

return LintResult.from_fallible_process_result(
result, linter_name="Flake8", results_file=results_file
)
return LintResult.from_fallible_process_result(result, linter_name="Flake8", report=report)


@rule(desc="Lint using Flake8")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ def test_passing_source(self) -> None:
assert len(result) == 1
assert result[0].exit_code == 0
assert result[0].stdout.strip() == ""
assert result[0].results_file is None
assert result[0].report is None

def test_failing_source(self) -> None:
target = self.make_target([self.bad_source])
result = self.run_flake8([target])
assert len(result) == 1
assert result[0].exit_code == 1
assert "bad.py:1:1: F401" in result[0].stdout
assert result[0].results_file is None
assert result[0].report is None

def test_mixed_sources(self) -> None:
target = self.make_target([self.good_source, self.bad_source])
Expand Down Expand Up @@ -157,13 +157,13 @@ def test_3rdparty_plugin(self) -> None:
assert result[0].exit_code == 1
assert "bad.py:1:1: PB11" in result[0].stdout

def test_output_file(self) -> None:
def test_report_file(self) -> None:
target = self.make_target([self.bad_source])
result = self.run_flake8([target], additional_args=["--lint-reports-dir='.'"])
assert len(result) == 1
assert result[0].exit_code == 1
assert result[0].stdout.strip() == ""
assert result[0].results_file is not None
output_files = self.request_single_product(DigestContents, result[0].results_file.digest)
assert len(output_files) == 1
assert "bad.py:1:1: F401" in output_files[0].content.decode()
assert result[0].report is not None
report_files = self.request_single_product(DigestContents, result[0].report.digest)
assert len(report_files) == 1
assert "bad.py:1:1: F401" in report_files[0].content.decode()
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ async def create_python_binary(
platforms=PexPlatforms.create_from_platforms_field(field_set.platforms),
output_filename=output_filename,
additional_args=field_set.generate_additional_args(python_binary_defaults),
for_deployable_binary=True,
)
),
)
Expand Down
Loading

0 comments on commit 7170332

Please sign in to comment.