Skip to content

Commit

Permalink
hack together exporting tools
Browse files Browse the repository at this point in the history
  • Loading branch information
lilatomic committed Feb 22, 2024
1 parent 6f254ca commit b6d763e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
31 changes: 23 additions & 8 deletions src/python/pants/backend/python/goals/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
import uuid
from dataclasses import dataclass
from enum import Enum
from typing import Any
from typing import Any, cast

from pants.backend.python.goals.lockfile_generation import GeneratePythonLockfile
from pants.backend.python.subsystems.python_tool_base import PythonToolRequirementsBase
from pants.backend.python.subsystems.setup import PythonSetup
from pants.backend.python.target_types import PexLayout
from pants.backend.python.util_rules.interpreter_constraints import InterpreterConstraints
Expand All @@ -32,12 +33,13 @@
ExportSubsystem,
PostProcessingCommand,
)
from pants.core.goals.resolve_helpers import ExportableTool
from pants.engine.engine_aware import EngineAwareParameter
from pants.engine.internals.native_engine import AddPrefix, Digest, MergeDigests, Snapshot
from pants.engine.internals.selectors import Get
from pants.engine.process import ProcessCacheScope, ProcessResult
from pants.engine.rules import collect_rules, rule
from pants.engine.unions import UnionRule
from pants.engine.unions import UnionMembership, UnionRule
from pants.option.option_types import EnumOption, StrListOption
from pants.util.strutil import path_safe, softwrap

Expand Down Expand Up @@ -308,18 +310,31 @@ async def export_virtualenv_for_resolve(
request: _ExportVenvForResolveRequest,
python_setup: PythonSetup,
export_subsys: ExportSubsystem,
union_membership: UnionMembership,
) -> MaybeExportResult:
resolve = request.resolve
lockfile_path = python_setup.resolves.get(resolve)
if not lockfile_path:
if lockfile_path:
lockfile = Lockfile(
url=lockfile_path,
url_description_of_origin=f"the resolve `{resolve}`",
resolve_name=resolve,
)
else:
tools = union_membership.get(ExportableTool)
maybe_exportable = {e.resolve_name: e for e in tools}.get(resolve)
if maybe_exportable:
lockfile = cast(
PythonToolRequirementsBase, maybe_exportable.subsystem_cls
).pex_requirements_for_default_lockfile()

else:
lockfile = None

if not lockfile:
raise ExportError(
f"No resolve named {resolve} found in [{python_setup.options_scope}].resolves."
)
lockfile = Lockfile(
url=lockfile_path,
url_description_of_origin=f"the resolve `{resolve}`",
resolve_name=resolve,
)

# TODO: from request?
interpreter_constraints = InterpreterConstraints(
Expand Down
14 changes: 13 additions & 1 deletion src/python/pants/backend/python/goals/lockfile_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collections import defaultdict
from dataclasses import dataclass

from pants.backend.python.lint.black.rules import BlackExportableTool
from pants.backend.python.subsystems.setup import PythonSetup
from pants.backend.python.target_types import (
PythonRequirementFindLinksField,
Expand Down Expand Up @@ -216,6 +217,15 @@ def determine_python_user_resolves(
)


@rule
def determine_python_exportable_tools(req: BlackExportableTool) -> KnownUserResolveNames:
return KnownUserResolveNames(
names=(req.resolve_name,),
option_name="<internal>",
requested_resolve_names_cls=RequestedPythonUserResolveNames,
)


@rule
async def setup_user_lockfile_requests(
requested: RequestedPythonUserResolveNames, all_targets: AllTargets, python_setup: PythonSetup
Expand Down Expand Up @@ -244,7 +254,9 @@ async def setup_user_lockfile_requests(
)
),
resolve_name=resolve,
lockfile_dest=python_setup.resolves[resolve],
lockfile_dest=python_setup.resolves.get(
resolve, "TODO_dest"
), # TODO: real thing for internal lockfiles
diff=False,
)
for resolve in requested
Expand Down
8 changes: 8 additions & 0 deletions src/python/pants/backend/python/lint/black/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
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 AbstractFmtRequest, FmtResult, FmtTargetsRequest, Partitions
from pants.core.goals.resolve_helpers import ExportableTool
from pants.core.util_rules.config_files import ConfigFiles, ConfigFilesRequest
from pants.engine.fs import Digest, MergeDigests
from pants.engine.process import ProcessResult
from pants.engine.rules import Get, MultiGet, collect_rules, rule
from pants.engine.unions import UnionRule
from pants.util.logging import LogLevel
from pants.util.strutil import pluralize, softwrap

Expand Down Expand Up @@ -115,9 +117,15 @@ async def black_fmt(request: BlackRequest.Batch, black: Black) -> FmtResult:
)


class BlackExportableTool(ExportableTool):
resolve_name = Black.options_scope
subsystem_cls = Black


def rules():
return [
*collect_rules(),
*BlackRequest.rules(),
*pex.rules(),
UnionRule(ExportableTool, BlackExportableTool),
]
19 changes: 17 additions & 2 deletions src/python/pants/core/goals/resolve_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ class KnownUserResolveNames:
requested_resolve_names_cls: type[RequestedUserResolveNames]


@union
class ExportableTool:
resolve_name: ClassVar[str]
subsystem_cls: ClassVar[type]


@union(in_scope_types=[EnvironmentName])
class RequestedUserResolveNames(Collection[str]):
"""The user resolves requested for a particular language ecosystem.
Expand Down Expand Up @@ -353,9 +359,18 @@ async def determine_requested_resolves(
Get(KnownUserResolveNames, KnownUserResolveNamesRequest, request())
for request in union_membership.get(KnownUserResolveNamesRequest)
)
logger.debug(f"Found known user resolves {known_user_resolve_names}")
known_internal_resolve_names = await MultiGet(
Get(KnownUserResolveNames, ExportableTool, tool())
for tool in union_membership.get(ExportableTool)
)

logger.debug(
f"Found known user resolves {known_user_resolve_names} and internal resolves {known_internal_resolve_names}"
)
all_known_resolves = [*known_user_resolve_names, *known_internal_resolve_names]

requested_user_resolve_names, requested_tool_sentinels = determine_resolves_to_generate(
known_user_resolve_names,
tuple(all_known_resolves),
union_membership.get(GenerateToolLockfileSentinel),
set(requested_resolves.val),
)
Expand Down

0 comments on commit b6d763e

Please sign in to comment.