-
-
Notifications
You must be signed in to change notification settings - Fork 608
/
lockfile.py
359 lines (318 loc) · 14.3 KB
/
lockfile.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import annotations
import logging
from collections import defaultdict
from dataclasses import dataclass
from pathlib import PurePath
from typing import Iterable
from pants.backend.python.pip_requirement import PipRequirement
from pants.backend.python.subsystems.poetry import (
POETRY_LAUNCHER,
PoetrySubsystem,
create_pyproject_toml,
)
from pants.backend.python.subsystems.python_tool_base import PythonToolRequirementsBase
from pants.backend.python.subsystems.repos import PythonRepos
from pants.backend.python.subsystems.setup import PythonSetup
from pants.backend.python.target_types import (
EntryPoint,
PythonRequirementResolveField,
PythonRequirementsField,
)
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
from pants.backend.python.util_rules.lockfile_metadata import PythonLockfileMetadata
from pants.backend.python.util_rules.pex import PexRequest, VenvPex, VenvPexProcess
from pants.backend.python.util_rules.pex_cli import PexCliProcess
from pants.backend.python.util_rules.pex_requirements import PexRequirements
from pants.core.goals.generate_lockfiles import (
GenerateLockfile,
GenerateLockfileResult,
GenerateLockfilesSubsystem,
GenerateToolLockfileSentinel,
KnownUserResolveNames,
KnownUserResolveNamesRequest,
RequestedUserResolveNames,
UserGenerateLockfiles,
WrappedGenerateLockfile,
)
from pants.core.util_rules.lockfile_metadata import calculate_invalidation_digest
from pants.engine.fs import CreateDigest, Digest, DigestContents, FileContent
from pants.engine.process import ProcessCacheScope, ProcessResult
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.engine.target import AllTargets
from pants.engine.unions import UnionRule
from pants.util.docutil import bin_name
from pants.util.logging import LogLevel
from pants.util.ordered_set import FrozenOrderedSet
from pants.util.strutil import softwrap
logger = logging.getLogger(__name__)
@dataclass(frozen=True)
class GeneratePythonLockfile(GenerateLockfile):
requirements: FrozenOrderedSet[str]
interpreter_constraints: InterpreterConstraints
use_pex: bool
@classmethod
def from_tool(
cls,
subsystem: PythonToolRequirementsBase,
interpreter_constraints: InterpreterConstraints | None = None,
*,
use_pex: bool,
extra_requirements: Iterable[str] = (),
) -> GeneratePythonLockfile:
"""Create a request for a dedicated lockfile for the tool.
If the tool determines its interpreter constraints by using the constraints of user code,
rather than the option `--interpreter-constraints`, you must pass the arg
`interpreter_constraints`.
"""
if not subsystem.uses_custom_lockfile:
return cls(
requirements=FrozenOrderedSet(),
interpreter_constraints=InterpreterConstraints(),
resolve_name=subsystem.options_scope,
lockfile_dest=subsystem.lockfile,
use_pex=use_pex,
)
return cls(
requirements=FrozenOrderedSet((*subsystem.all_requirements, *extra_requirements)),
interpreter_constraints=(
interpreter_constraints
if interpreter_constraints is not None
else subsystem.interpreter_constraints
),
resolve_name=subsystem.options_scope,
lockfile_dest=subsystem.lockfile,
use_pex=use_pex,
)
@property
def requirements_hex_digest(self) -> str:
"""Produces a hex digest of the requirements input for this lockfile."""
return calculate_invalidation_digest(self.requirements)
@rule
def wrap_python_lockfile_request(request: GeneratePythonLockfile) -> WrappedGenerateLockfile:
return WrappedGenerateLockfile(request)
class MaybeWarnPythonRepos:
pass
@dataclass(frozen=True)
class MaybeWarnPythonReposRequest:
pass
@rule
def maybe_warn_python_repos(
_: MaybeWarnPythonReposRequest, python_repos: PythonRepos
) -> MaybeWarnPythonRepos:
def warn_python_repos(option: str) -> None:
logger.warning(
softwrap(
f"""
The option `[python-repos].{option}` is configured, but it does not work when using
Poetry for lockfile generation. Lockfile generation will fail if the relevant
requirements cannot be located on PyPI.
Instead, you can use Pex to generate lockfiles by setting
`[python].lockfile_generator = 'pex'.
Alternatively, you can disable lockfiles by setting
`[tool].lockfile = '<none>'`, e.g. setting `[black].lockfile`. You can also manually
generate a lockfile, such as by using pip-compile or `pip freeze`. Set the
`[tool].lockfile` option to the path you manually generated. When manually
maintaining lockfiles, set `[python].invalid_lockfile_behavior = 'ignore'. For user
lockfiles from `[python].resolves`, set
`[python].resolves_generate_lockfiles = false`.
"""
)
)
if python_repos.repos:
warn_python_repos("repos")
if python_repos.indexes != (python_repos.pypi_index,):
warn_python_repos("indexes")
return MaybeWarnPythonRepos()
@rule(desc="Generate Python lockfile", level=LogLevel.DEBUG)
async def generate_lockfile(
req: GeneratePythonLockfile,
poetry_subsystem: PoetrySubsystem,
generate_lockfiles_subsystem: GenerateLockfilesSubsystem,
python_repos: PythonRepos,
python_setup: PythonSetup,
) -> GenerateLockfileResult:
if req.use_pex:
pip_args_file = "__pip_args.txt"
pip_args_file_content = "\n".join(
[f"--no-binary {pkg}" for pkg in python_setup.no_binary]
+ [f"--only-binary {pkg}" for pkg in python_setup.only_binary]
)
pip_args_file_digest = await Get(
Digest, CreateDigest([FileContent(pip_args_file, pip_args_file_content.encode())])
)
header_delimiter = "//"
result = await Get(
ProcessResult,
PexCliProcess(
subcommand=("lock", "create"),
extra_args=(
"--output=lock.json",
"--no-emit-warnings",
# See https://github.com/pantsbuild/pants/issues/12458. For now, we always
# generate universal locks because they have the best compatibility. We may
# want to let users change this, as `style=strict` is safer.
"--style=universal",
"--resolver-version",
"pip-2020-resolver",
# PEX files currently only run on Linux and Mac machines; so we hard code this
# limit on lock universaility to avoid issues locking due to irrelevant
# Windows-only dependency issues. See this Pex issue that originated from a
# Pants user issue presented in Slack:
# https://github.com/pantsbuild/pex/issues/1821
#
# At some point it will probably make sense to expose `--target-system` for
# configuration.
"--target-system",
"linux",
"--target-system",
"mac",
# This makes diffs more readable when lockfiles change.
"--indent=2",
"-r",
pip_args_file,
*python_repos.pex_args,
*python_setup.manylinux_pex_args,
*req.interpreter_constraints.generate_pex_arg_list(),
*req.requirements,
),
additional_input_digest=pip_args_file_digest,
output_files=("lock.json",),
description=f"Generate lockfile for {req.resolve_name}",
# Instead of caching lockfile generation with LMDB, we instead use the invalidation
# scheme from `lockfile_metadata.py` to check for stale/invalid lockfiles. This is
# necessary so that our invalidation is resilient to deleting LMDB or running on a
# new machine.
#
# We disable caching with LMDB so that when you generate a lockfile, you always get
# the most up-to-date snapshot of the world. This is generally desirable and also
# necessary to avoid an awkward edge case where different developers generate
# different lockfiles even when generating at the same time. See
# https://github.com/pantsbuild/pants/issues/12591.
cache_scope=ProcessCacheScope.PER_SESSION,
),
)
else:
header_delimiter = "#"
await Get(MaybeWarnPythonRepos, MaybeWarnPythonReposRequest())
_pyproject_toml = create_pyproject_toml(
req.requirements, req.interpreter_constraints
).encode()
_pyproject_toml_digest, _launcher_digest = await MultiGet(
Get(Digest, CreateDigest([FileContent("pyproject.toml", _pyproject_toml)])),
Get(Digest, CreateDigest([POETRY_LAUNCHER])),
)
_poetry_pex = await Get(
VenvPex,
PexRequest,
poetry_subsystem.to_pex_request(
main=EntryPoint(PurePath(POETRY_LAUNCHER.path).stem), sources=_launcher_digest
),
)
# WONTFIX(#12314): Wire up Poetry to named_caches.
# WONTFIX(#12314): Wire up all the pip options like indexes.
_lock_result = await Get(
ProcessResult,
VenvPexProcess(
_poetry_pex,
argv=("lock",),
input_digest=_pyproject_toml_digest,
output_files=("poetry.lock", "pyproject.toml"),
description=f"Generate lockfile for {req.resolve_name}",
cache_scope=ProcessCacheScope.PER_SESSION,
),
)
result = await Get(
ProcessResult,
VenvPexProcess(
_poetry_pex,
argv=("export", "-o", req.lockfile_dest),
input_digest=_lock_result.output_digest,
output_files=(req.lockfile_dest,),
description=(
f"Exporting Poetry lockfile to requirements.txt format for {req.resolve_name}"
),
level=LogLevel.DEBUG,
),
)
initial_lockfile_digest_contents = await Get(DigestContents, Digest, result.output_digest)
# TODO(#12314) Improve error message on `Requirement.parse`
metadata = PythonLockfileMetadata.new(
req.interpreter_constraints,
{PipRequirement.parse(i) for i in req.requirements},
)
lockfile_with_header = metadata.add_header_to_lockfile(
initial_lockfile_digest_contents[0].content,
regenerate_command=(
generate_lockfiles_subsystem.custom_command
or f"{bin_name()} generate-lockfiles --resolve={req.resolve_name}"
),
delimeter=header_delimiter,
)
final_lockfile_digest = await Get(
Digest, CreateDigest([FileContent(req.lockfile_dest, lockfile_with_header)])
)
return GenerateLockfileResult(final_lockfile_digest, req.resolve_name, req.lockfile_dest)
class RequestedPythonUserResolveNames(RequestedUserResolveNames):
pass
class KnownPythonUserResolveNamesRequest(KnownUserResolveNamesRequest):
pass
@rule
def determine_python_user_resolves(
_: KnownPythonUserResolveNamesRequest, python_setup: PythonSetup
) -> KnownUserResolveNames:
return KnownUserResolveNames(
names=tuple(python_setup.resolves.keys()),
option_name="[python].resolves",
requested_resolve_names_cls=RequestedPythonUserResolveNames,
)
@rule
async def setup_user_lockfile_requests(
requested: RequestedPythonUserResolveNames, all_targets: AllTargets, python_setup: PythonSetup
) -> UserGenerateLockfiles:
if not (python_setup.enable_resolves and python_setup.resolves_generate_lockfiles):
return UserGenerateLockfiles()
resolve_to_requirements_fields = defaultdict(set)
for tgt in all_targets:
if not tgt.has_fields((PythonRequirementResolveField, PythonRequirementsField)):
continue
resolve = tgt[PythonRequirementResolveField].normalized_value(python_setup)
resolve_to_requirements_fields[resolve].add(tgt[PythonRequirementsField])
return UserGenerateLockfiles(
GeneratePythonLockfile(
requirements=PexRequirements.create_from_requirement_fields(
resolve_to_requirements_fields[resolve],
constraints_strings=(),
).req_strings,
interpreter_constraints=InterpreterConstraints(
python_setup.resolves_to_interpreter_constraints.get(
resolve, python_setup.interpreter_constraints
)
),
resolve_name=resolve,
lockfile_dest=python_setup.resolves[resolve],
use_pex=python_setup.generate_lockfiles_with_pex,
)
for resolve in requested
)
class PoetryLockfileSentinel(GenerateToolLockfileSentinel):
resolve_name = PoetrySubsystem.options_scope
@rule
async def setup_poetry_lockfile(
_: PoetryLockfileSentinel, poetry_subsystem: PoetrySubsystem, python_setup: PythonSetup
) -> GeneratePythonLockfile:
# No matter what venv (Python) Poetry lives in, it can still create locks for projects with
# a disjoint set of Pythons as implied by the project's `python` requirement; so we need not
# account for user resolve ICs.
return GeneratePythonLockfile.from_tool(
poetry_subsystem, use_pex=python_setup.generate_lockfiles_with_pex
)
def rules():
return (
*collect_rules(),
UnionRule(GenerateLockfile, GeneratePythonLockfile),
UnionRule(GenerateToolLockfileSentinel, PoetryLockfileSentinel),
UnionRule(KnownUserResolveNamesRequest, KnownPythonUserResolveNamesRequest),
UnionRule(RequestedUserResolveNames, RequestedPythonUserResolveNames),
)