Skip to content

Commit

Permalink
Improved calling of publish commands
Browse files Browse the repository at this point in the history
  • Loading branch information
xlevus committed Sep 27, 2021
1 parent 8d307a7 commit f3a3220
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 26 deletions.
15 changes: 7 additions & 8 deletions src/python/pants/backend/experimental/python/publish_pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from pants.backend.python.target_types import ConsoleScript, PythonDistribution
from pants.backend.python.util_rules.pex import PexRequest, PexRequirements, VenvPex, VenvPexProcess
from pants.core.goals.publish import (
PublishedPackage,
PublishProcess,
PublishRequest,
PublishTarget,
PublishTargetField,
)
from pants.engine.environment import Environment, InterpolatedEnvironmentRequest
from pants.engine.process import Process, ProcessResult
from pants.engine.process import InteractiveProcess, Process
from pants.engine.rules import Get, collect_rules, rule
from pants.engine.target import StringField
from pants.engine.unions import UnionRule
Expand Down Expand Up @@ -102,7 +102,7 @@ async def twine_call_args(target: PypiRepositoryTarget) -> TwineCallArgs:


@rule
async def publish_pypi(request: PypiPublishRequest, twine: Twine) -> PublishedPackage:
async def publish_pypi(request: PypiPublishRequest, twine: Twine) -> PublishProcess:
twine_pex = await Get(
VenvPex,
PexRequest(
Expand All @@ -116,6 +116,9 @@ async def publish_pypi(request: PypiPublishRequest, twine: Twine) -> PublishedPa
call_args = await Get(TwineCallArgs, PypiRepositoryTarget, request.publish_target)
paths = [artifact.relpath for artifact in request.built_package.artifacts]

if not paths:
return PublishProcess(process=None, message="No artifacts found.")

process = await Get(
Process,
VenvPexProcess,
Expand All @@ -128,11 +131,7 @@ async def publish_pypi(request: PypiPublishRequest, twine: Twine) -> PublishedPa
),
)

await Get(ProcessResult, Process, process)

return PublishedPackage(
package=request.built_package, publish_target=request.publish_target.address
)
return PublishProcess(process=InteractiveProcess.from_process(process))


def rules():
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


def test_():
pass
56 changes: 42 additions & 14 deletions src/python/pants/core/goals/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

import logging
from dataclasses import dataclass
from typing import ClassVar, Iterable, List, Tuple, Type, cast
from typing import ClassVar, Iterable, List, Optional, Tuple, Type, cast

from pants.build_graph.address import Address
from pants.core.goals.package import BuiltPackage, PackageFieldSet
from pants.engine.addresses import UnparsedAddressInputs
from pants.engine.console import Console
from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.internals.selectors import MultiGet
from pants.engine.process import InteractiveProcess, InteractiveProcessResult
from pants.engine.rules import Get, collect_rules, goal_rule
from pants.engine.target import (
COMMON_TARGET_FIELDS,
Expand Down Expand Up @@ -60,17 +61,19 @@ class Publish(Goal):


@dataclass(frozen=True)
class PublishedPackage:
package: BuiltPackage
publish_target: Address
class PublishProcess:
# If process is None, consider publish 'skipped'.
# e.g. incompatibility between source and destination.
process: Optional[InteractiveProcess]
message: Optional[str] = None


@frozen_after_init
@dataclass(unsafe_hash=True)
class PublishedPackageSet:
publishes: FrozenOrderedSet[PublishedPackage]
publishes: FrozenOrderedSet[PublishProcess]

def __init__(self, published_packages: Iterable[PublishedPackage]):
def __init__(self, published_packages: Iterable[PublishProcess]):
self.publishes = FrozenOrderedSet(published_packages)


Expand All @@ -88,6 +91,7 @@ def _can_package(target: Target, union_membership: UnionMembership):
async def publish(
targets: Targets,
union_membership: UnionMembership,
console: Console,
) -> Publish:
publishable_targets: List[Tuple[Target, PackageFieldSet]] = []

Expand Down Expand Up @@ -135,7 +139,7 @@ async def publish(
return Publish(exit_code=1)
fieldset = publish_target.publishee_fieldset_type.create(target)

logger.info(f"Publishing {target.address} to {publish_target.address}")
logger.debug(f"Publishing {target.address} to {publish_target.address}")
requests.append(
publish_target.publish_request_type(
built_package,
Expand All @@ -144,14 +148,38 @@ async def publish(
)
)

packages = await MultiGet(
Get(PublishedPackage, PublishRequest, request) for request in requests
)
processes = await MultiGet(Get(PublishProcess, PublishRequest, request) for request in requests)

exit_code = 0

for (target, _), request, process in zip(publishable_targets, requests, processes):
if process.process is None:
sigil = console.sigil_skipped()
status = "Unable to publish"
message = process.message
else:
result = await Get(
InteractiveProcessResult,
InteractiveProcess,
process.process,
)
message = process.message or ""

if result.exit_code == 0:
sigil = console.sigil_succeeded()
status = "Published"
else:
exit_code = 1
sigil = console.sigil_failed()
status = "Failed to publish"

# ???
PublishedPackageSet(packages)
console.print_stderr(
f"{sigil} {status} {target.address} to {request.publish_target.address}{message}"
)
if message:
console.print_stderr(message)

return Publish(exit_code=0)
return Publish(exit_code=exit_code)


def rules():
Expand Down
60 changes: 56 additions & 4 deletions src/python/pants/core/goals/publish_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
from pants.core.goals.package import BuiltPackage, PackageFieldSet
from pants.core.goals.publish import (
Publish,
PublishedPackage,
PublishProcess,
PublishRequest,
PublishTarget,
PublishTargetField,
publish,
)
from pants.engine.addresses import UnparsedAddressInputs
from pants.engine.process import InteractiveProcess, InteractiveProcessResult
from pants.engine.target import Sources, Target, Targets
from pants.engine.unions import UnionMembership
from pants.testutil.rule_runner import MockGet, RuleRunner, mock_console, run_rule_with_mocks
Expand Down Expand Up @@ -75,6 +76,9 @@ def run_publish_rule(
package_fieldset_types: List[Type[PackageFieldSet]],
targets: List[Target],
built_packages: List[BuiltPackage] = (),
publish_process: Optional[InteractiveProcess] = InteractiveProcess(argv=[]),
publish_exit_code: int = 0,
publish_message: Optional[str] = None,
):
def UAI(unparsed_addresses: UnparsedAddressInputs):
return [t for t in targets if t.address.spec in unparsed_addresses.values]
Expand All @@ -92,6 +96,7 @@ def UAI(unparsed_addresses: UnparsedAddressInputs):
rule_args=[
Targets(targets),
union_membership,
console,
],
mock_gets=[
MockGet(
Expand All @@ -105,9 +110,17 @@ def UAI(unparsed_addresses: UnparsedAddressInputs):
mock=lambda _: built_packages.pop(0),
),
MockGet(
output_type=PublishedPackage,
output_type=PublishProcess,
input_type=PublishRequest,
mock=lambda request: PublishedPackage(request.built_package, Address("//")),
mock=lambda request: PublishProcess(
process=publish_process,
message=publish_message,
),
),
MockGet(
output_type=InteractiveProcessResult,
input_type=InteractiveProcess,
mock=lambda process: InteractiveProcessResult(exit_code=publish_exit_code),
),
],
union_membership=union_membership,
Expand Down Expand Up @@ -163,4 +176,43 @@ def test_packageable_with_target(rule_runner):
)

assert exit_code == 0
assert f"Publishing {publishee.address}" in stderr
assert f"Published {publishee.address} to {publish_target.address}" in stderr


def test_none_publish_process(rule_runner):
"""When PublishProcess.process is None."""
publish_target = make_publish_target()
publishee = make_target(publish_targets=[publish_target])

message_sentinel = "SENTINEL"

exit_code, stderr = run_publish_rule(
rule_runner,
publish_request_types=[MockPublishRequest],
package_fieldset_types=[MockTargetPackageFieldSet],
targets=[publish_target, publishee],
built_packages=[make_built_package()],
publish_process=None,
publish_message=message_sentinel,
)

assert exit_code == 0
assert f"Unable to publish {publishee.address} to {publish_target.address}" in stderr
assert message_sentinel in stderr


def test_packageable_with_target_failure(rule_runner):
publish_target = make_publish_target()
publishee = make_target(publish_targets=[publish_target])

exit_code, stderr = run_publish_rule(
rule_runner,
publish_request_types=[MockPublishRequest],
package_fieldset_types=[MockTargetPackageFieldSet],
targets=[publish_target, publishee],
built_packages=[make_built_package()],
publish_exit_code=1,
)

assert exit_code == 1
assert f"Failed to publish {publishee.address} to {publish_target.address}" in stderr

0 comments on commit f3a3220

Please sign in to comment.