forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.py
162 lines (138 loc) · 5.59 KB
/
rules.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
# Copyright 2020 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from dataclasses import dataclass
from typing import Tuple
from pants.backend.python.lint.bandit.subsystem import Bandit
from pants.backend.python.rules import download_pex_bin, pex
from pants.backend.python.rules.pex import (
Pex,
PexInterpreterConstraints,
PexRequest,
PexRequirements,
)
from pants.backend.python.subsystems import python_native_code, subprocess_environment
from pants.backend.python.subsystems.subprocess_environment import SubprocessEnvironment
from pants.backend.python.target_types import PythonInterpreterCompatibility, PythonSources
from pants.core.goals.lint import LintRequest, LintResult, LintResults
from pants.core.util_rules import determine_source_files, strip_source_roots
from pants.core.util_rules.determine_source_files import (
AllSourceFilesRequest,
SourceFiles,
SpecifiedSourceFilesRequest,
)
from pants.engine.fs import Digest, GlobMatchErrorBehavior, MergeDigests, PathGlobs
from pants.engine.process import FallibleProcessResult, Process
from pants.engine.rules import collect_rules, rule
from pants.engine.selectors import Get, MultiGet
from pants.engine.target import FieldSetWithOrigin
from pants.engine.unions import UnionRule
from pants.python.python_setup import PythonSetup
from pants.util.strutil import pluralize
@dataclass(frozen=True)
class BanditFieldSet(FieldSetWithOrigin):
required_fields = (PythonSources,)
sources: PythonSources
compatibility: PythonInterpreterCompatibility
class BanditRequest(LintRequest):
field_set_type = BanditFieldSet
@dataclass(frozen=True)
class BanditPartition:
field_sets: Tuple[BanditFieldSet, ...]
interpreter_constraints: PexInterpreterConstraints
def generate_args(*, specified_source_files: SourceFiles, bandit: Bandit) -> Tuple[str, ...]:
args = []
if bandit.config is not None:
args.append(f"--config={bandit.config}")
args.extend(bandit.args)
args.extend(specified_source_files.files)
return tuple(args)
@rule
async def bandit_lint_partition(
partition: BanditPartition,
bandit: Bandit,
python_setup: PythonSetup,
subprocess_environment: SubprocessEnvironment,
) -> LintResult:
requirements_pex_request = Get(
Pex,
PexRequest(
output_filename="bandit.pex",
requirements=PexRequirements(bandit.all_requirements),
interpreter_constraints=(
partition.interpreter_constraints
or PexInterpreterConstraints(bandit.interpreter_constraints)
),
entry_point=bandit.entry_point,
),
)
config_digest_request = Get(
Digest,
PathGlobs(
globs=[bandit.config] if bandit.config else [],
glob_match_error_behavior=GlobMatchErrorBehavior.error,
description_of_origin="the option `--bandit-config`",
),
)
all_source_files_request = Get(
SourceFiles, AllSourceFilesRequest(field_set.sources for field_set in partition.field_sets)
)
specified_source_files_request = Get(
SourceFiles,
SpecifiedSourceFilesRequest(
(field_set.sources, field_set.origin) for field_set in partition.field_sets
),
)
requirements_pex, config_digest, all_source_files, specified_source_files = await MultiGet(
requirements_pex_request,
config_digest_request,
all_source_files_request,
specified_source_files_request,
)
input_digest = await Get(
Digest,
MergeDigests((all_source_files.snapshot.digest, requirements_pex.digest, config_digest)),
)
address_references = ", ".join(
sorted(field_set.address.reference() for field_set in partition.field_sets)
)
process = requirements_pex.create_process(
python_setup=python_setup,
subprocess_environment=subprocess_environment,
pex_path="./bandit.pex",
pex_args=generate_args(specified_source_files=specified_source_files, bandit=bandit),
input_digest=input_digest,
description=(
f"Run Bandit on {pluralize(len(partition.field_sets), 'target')}: {address_references}."
),
)
result = await Get(FallibleProcessResult, Process, process)
return LintResult.from_fallible_process_result(result, linter_name="Bandit")
@rule(desc="Lint using Bandit")
async def bandit_lint(
request: BanditRequest, bandit: Bandit, python_setup: PythonSetup
) -> LintResults:
if bandit.skip:
return LintResults()
# NB: Bandit output depends upon which Python interpreter version it's run with
# ( https://github.com/PyCQA/bandit#under-which-version-of-python-should-i-install-bandit). We
# batch targets by their constraints to ensure, for example, that all Python 2 targets run
# together and all Python 3 targets run together.
constraints_to_field_sets = PexInterpreterConstraints.group_field_sets_by_constraints(
request.field_sets, python_setup
)
partitioned_results = await MultiGet(
Get(LintResult, BanditPartition(partition_field_sets, partition_compatibility))
for partition_compatibility, partition_field_sets in constraints_to_field_sets.items()
)
return LintResults(partitioned_results)
def rules():
return [
*collect_rules(),
UnionRule(LintRequest, BanditRequest),
*download_pex_bin.rules(),
*determine_source_files.rules(),
*pex.rules(),
*python_native_code.rules(),
*strip_source_roots.rules(),
*subprocess_environment.rules(),
]