forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.py
169 lines (142 loc) · 6 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
163
164
165
166
167
168
169
# Copyright 2019 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.black.skip_field import SkipBlackField
from pants.backend.python.lint.black.subsystem import Black
from pants.backend.python.subsystems.setup import PythonSetup
from pants.backend.python.target_types import InterpreterConstraintsField, PythonSourceField
from pants.backend.python.util_rules import pex
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
from pants.backend.python.util_rules.pex import PexRequest, VenvPex, VenvPexProcess
from pants.core.goals.fmt import FmtRequest, FmtResult
from pants.core.goals.lint import LintResult, LintResults, LintTargetsRequest
from pants.core.util_rules.config_files import ConfigFiles, ConfigFilesRequest
from pants.core.util_rules.source_files import SourceFiles, SourceFilesRequest
from pants.engine.fs import Digest, MergeDigests
from pants.engine.process import FallibleProcessResult, Process, ProcessResult
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.engine.target import FieldSet, Target
from pants.engine.unions import UnionRule
from pants.util.logging import LogLevel
from pants.util.strutil import pluralize
@dataclass(frozen=True)
class BlackFieldSet(FieldSet):
required_fields = (PythonSourceField,)
source: PythonSourceField
interpreter_constraints: InterpreterConstraintsField
@classmethod
def opt_out(cls, tgt: Target) -> bool:
return tgt.get(SkipBlackField).value
class BlackRequest(FmtRequest, LintTargetsRequest):
field_set_type = BlackFieldSet
name = "Black"
@dataclass(frozen=True)
class SetupRequest:
request: BlackRequest
check_only: bool
@dataclass(frozen=True)
class Setup:
process: Process
original_digest: Digest
def generate_argv(source_files: SourceFiles, black: Black, *, check_only: bool) -> Tuple[str, ...]:
args = []
if check_only:
args.append("--check")
if black.config:
args.extend(["--config", black.config])
args.extend(["-W", "{pants_concurrency}"])
args.extend(black.args)
args.extend(source_files.files)
return tuple(args)
@rule(level=LogLevel.DEBUG)
async def setup_black(
setup_request: SetupRequest, black: Black, python_setup: PythonSetup
) -> Setup:
# Black requires 3.6+ but uses the typed-ast library to work with 2.7, 3.4, 3.5, 3.6, and 3.7.
# However, typed-ast does not understand 3.8+, so instead we must run Black with Python 3.8+
# when relevant. We only do this if if <3.8 can't be used, as we don't want a loose requirement
# like `>=3.6` to result in requiring Python 3.8, which would error if 3.8 is not installed on
# the machine.
all_interpreter_constraints = InterpreterConstraints.create_from_compatibility_fields(
(field_set.interpreter_constraints for field_set in setup_request.request.field_sets),
python_setup,
)
tool_interpreter_constraints = (
all_interpreter_constraints
if (
black.options.is_default("interpreter_constraints")
and all_interpreter_constraints.requires_python38_or_newer(
python_setup.interpreter_universe
)
)
else black.interpreter_constraints
)
black_pex_get = Get(
VenvPex,
PexRequest(
output_filename="black.pex",
internal_only=True,
requirements=black.pex_requirements(),
interpreter_constraints=tool_interpreter_constraints,
main=black.main,
),
)
source_files_get = Get(
SourceFiles,
SourceFilesRequest(field_set.source for field_set in setup_request.request.field_sets),
)
source_files, black_pex = await MultiGet(source_files_get, black_pex_get)
source_files_snapshot = (
source_files.snapshot
if setup_request.request.prior_formatter_result is None
else setup_request.request.prior_formatter_result
)
config_files = await Get(
ConfigFiles, ConfigFilesRequest, black.config_request(source_files_snapshot.dirs)
)
input_digest = await Get(
Digest, MergeDigests((source_files_snapshot.digest, config_files.snapshot.digest))
)
process = await Get(
Process,
VenvPexProcess(
black_pex,
argv=generate_argv(source_files, black, check_only=setup_request.check_only),
input_digest=input_digest,
output_files=source_files_snapshot.files,
concurrency_available=len(setup_request.request.field_sets),
description=f"Run Black on {pluralize(len(setup_request.request.field_sets), 'file')}.",
level=LogLevel.DEBUG,
),
)
return Setup(process, original_digest=source_files_snapshot.digest)
@rule(desc="Format with Black", level=LogLevel.DEBUG)
async def black_fmt(request: BlackRequest, black: Black) -> FmtResult:
if black.skip:
return FmtResult.skip(formatter_name=request.name)
setup = await Get(Setup, SetupRequest(request, check_only=False))
result = await Get(ProcessResult, Process, setup.process)
return FmtResult.from_process_result(
result,
original_digest=setup.original_digest,
formatter_name=request.name,
strip_chroot_path=True,
)
@rule(desc="Lint with Black", level=LogLevel.DEBUG)
async def black_lint(request: BlackRequest, black: Black) -> LintResults:
if black.skip:
return LintResults([], linter_name=request.name)
setup = await Get(Setup, SetupRequest(request, check_only=True))
result = await Get(FallibleProcessResult, Process, setup.process)
return LintResults(
[LintResult.from_fallible_process_result(result, strip_chroot_path=True)],
linter_name=request.name,
)
def rules():
return [
*collect_rules(),
UnionRule(FmtRequest, BlackRequest),
UnionRule(LintTargetsRequest, BlackRequest),
*pex.rules(),
]