Skip to content

Commit

Permalink
Updated to pants 2.20 and added small migration tool (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
sureshjoshi authored May 9, 2024
1 parent 31e238c commit c874ae9
Show file tree
Hide file tree
Showing 26 changed files with 3,224 additions and 831 deletions.
476 changes: 185 additions & 291 deletions build-support/pants-plugins/lock.txt

Large diffs are not rendered by default.

3,402 changes: 2,889 additions & 513 deletions build-support/python/default_lock.txt

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion pants-plugins/experimental/ansible/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import logging
from abc import ABCMeta
from collections.abc import Iterable
from dataclasses import dataclass
from typing import Any, Iterable
from typing import Any

from pants.core.goals.publish import (
NoApplicableTargetsBehavior,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from __future__ import annotations

from typing import Iterable
from collections.abc import Iterable

from experimental.ansible.lint.ansible_lint import rules as ansible_lint_rules
from experimental.ansible.lint.ansible_lint import skip_field, subsystem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from __future__ import annotations

import logging
from collections.abc import Iterable
from dataclasses import dataclass
from typing import Iterable

from experimental.ansible.lint.ansible_lint.subsystem import AnsibleLint
from experimental.ansible.target_types import AnsibleSourceField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from __future__ import annotations

from collections.abc import Iterable
from textwrap import dedent
from typing import Iterable

import pytest
from experimental.ansible.lint.ansible_lint import rules as ansible_lint_rules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from __future__ import annotations

from typing import Iterable
from collections.abc import Iterable

from experimental.ansible.target_types import (
AnsibleSourcesGeneratorTarget,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import os
from typing import Iterable
from collections.abc import Iterable

from pants.backend.python.goals.export import ExportPythonTool, ExportPythonToolSentinel
from pants.backend.python.subsystems.python_tool_base import PythonToolBase
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/experimental/ansible/register.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Iterable
from collections.abc import Iterable

from experimental.ansible.deploy import rules as deploy_rules
from experimental.ansible.goals import tailor
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/experimental/ansible/rules.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

import logging
from collections.abc import Iterable
from dataclasses import dataclass
from typing import Iterable

from experimental.ansible.deploy import DeploymentFieldSet, DeployResult, DeployResults
from experimental.ansible.subsystems.ansible import Ansible
Expand Down
4 changes: 4 additions & 0 deletions pants-plugins/experimental/migrate/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

python_sources()
Empty file.
14 changes: 14 additions & 0 deletions pants-plugins/experimental/migrate/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

from collections.abc import Iterable

from experimental.migrate.rules import rules as migrate_rules
from pants.engine.rules import Rule
from pants.engine.unions import UnionRule


def rules() -> Iterable[Rule | UnionRule]:
return (*migrate_rules(),)
79 changes: 79 additions & 0 deletions pants-plugins/experimental/migrate/rules.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

from collections.abc import Iterable
from pathlib import Path

import libcst
import libcst.matchers as m
from experimental.migrate.subsystems import Migrate, MigrateSubsystem
from libcst import RemovalSentinel, RemoveFromParent
from pants.engine.console import Console
from pants.engine.rules import Rule, collect_rules, goal_rule
from pants.engine.target import UnexpandedTargets


class RemoveRuleTransformer(m.MatcherDecoratableTransformer):
@m.leave(
m.ImportFrom(
module=m.DoNotCare(),
names=[
m.ZeroOrMore(),
m.ImportAlias(name=m.Name("rule_helper")),
m.ZeroOrMore(),
],
)
)
def handle_imports(
self, original_node: libcst.ImportFrom, updated_node: libcst.ImportFrom
) -> libcst.ImportFrom | RemovalSentinel:
assert not isinstance(original_node.names, libcst.ImportStar)

if len(original_node.names) == 1:
return RemoveFromParent()

return updated_node.with_changes(
names=[n for n in original_node.names if n.evaluated_name != "rule_helper"],
# This is a workaround for https://github.com/Instagram/LibCST/issues/532
# Formatters/isort will clean this up, but it doesn't compile without this
lpar=libcst.LeftParen(),
rpar=libcst.RightParen(),
)

@m.leave(m.Decorator(decorator=m.Name("rule_helper")))
def handle_decorator(
self, original_node: libcst.Decorator, updated_node: libcst.Decorator
) -> libcst.Decorator | RemovalSentinel:
return RemoveFromParent()


# TODO: This will need to become a BuiltinGoal, so just hacking around to get a list of Targets
# Normally, will use the same code for "call-by-name-migration"
@goal_rule
async def migrate(
console: Console, subsystem: MigrateSubsystem, targets: UnexpandedTargets
) -> Migrate:
filenames = [t.address.filename for t in targets if t.address.is_file_target]

for f in sorted(filenames):
file = Path(f)
if file.suffix != ".py":
continue
with open(file) as f:
source = f.read()
tree = libcst.parse_module(source)
new_tree = tree.visit(RemoveRuleTransformer())
new_source = new_tree.code

if source != new_source:
console.print_stderr(f"Rewriting {file}")
with open(file, "w") as f:
f.write(new_source)

return Migrate(exit_code=0)


def rules() -> Iterable[Rule]:
return collect_rules()
26 changes: 26 additions & 0 deletions pants-plugins/experimental/migrate/subsystems.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).

from __future__ import annotations

from collections.abc import Iterable

from pants.engine.goal import Goal, GoalSubsystem
from pants.engine.rules import Rule, collect_rules


class MigrateSubsystem(GoalSubsystem):
name = "migrate"
help = "???"


class Migrate(Goal):
subsystem_cls = MigrateSubsystem
environment_behavior = Goal.EnvironmentBehavior.LOCAL_ONLY


def rules() -> Iterable[Rule]:
return (
*collect_rules(),
*MigrateSubsystem.rules(), # type: ignore[call-arg]
)
2 changes: 1 addition & 1 deletion pants-plugins/experimental/scie/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from __future__ import annotations

from typing import Iterable
from collections.abc import Iterable

from experimental.scie.rules import rules as scie_rules
from experimental.scie.target_types import ScieTarget
Expand Down
9 changes: 4 additions & 5 deletions pants-plugins/experimental/scie/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@

import logging
import os
from dataclasses import asdict, dataclass, replace
from collections.abc import Iterable, Mapping
from dataclasses import asdict, dataclass
from pathlib import PurePath
from typing import Final, Iterable, Mapping
from typing import Final

import toml
from experimental.scie.config import Command, Config, File, Interpreter, LiftConfig
Expand Down Expand Up @@ -39,7 +40,7 @@
)
from pants.engine.platform import Platform
from pants.engine.process import Process, ProcessResult
from pants.engine.rules import Get, MultiGet, Rule, collect_rules, rule, rule_helper
from pants.engine.rules import Get, MultiGet, Rule, collect_rules, rule
from pants.engine.target import (
DependenciesRequest,
DescriptionField,
Expand Down Expand Up @@ -70,7 +71,6 @@ class ScieFieldSet(PackageFieldSet, RunFieldSet):
lift: ScieLiftSourceField


@rule_helper
async def _get_interpreter_config(targets: Targets) -> Interpreter:
# Get the interpreter_constraints for the Pex to determine which version of the Python Standalone to use
constraints = await Get(
Expand Down Expand Up @@ -112,7 +112,6 @@ def _contains_pex(built_package: BuiltPackage) -> bool:
)


@rule_helper
async def _parse_lift_source(source: ScieLiftSourceField) -> Config:
hydrated_source = await Get(HydratedSources, HydrateSourcesRequest(source))
digest_contents = await Get(DigestContents, Digest, hydrated_source.snapshot.digest)
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/experimental/scie/subsystems.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from __future__ import annotations

from typing import Iterable
from collections.abc import Iterable

from pants.core.util_rules.external_tool import TemplatedExternalTool
from pants.engine.rules import Rule, collect_rules
Expand Down
2 changes: 0 additions & 2 deletions pants-plugins/experimental/scie/target_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from pants.engine.target import (
COMMON_TARGET_FIELDS,
Dependencies,
DictStringToStringField,
NestedDictStringToStringField,
OptionalSingleSourceField,
StringSequenceField,
Target,
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/experimental/swift/goals/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from __future__ import annotations

import logging
from collections.abc import Iterable
from itertools import groupby
from typing import Iterable

from experimental.swift.subsystems.toolchain import SwiftSubsystem
from experimental.swift.target_types import SwiftFieldSet, SwiftSourceField
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/experimental/swift/goals/check_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from __future__ import annotations

from collections.abc import Iterable
from textwrap import dedent
from typing import Iterable

import pytest
from experimental.swift.goals import check
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/experimental/swift/goals/tailor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from __future__ import annotations

from collections.abc import Iterable
from dataclasses import dataclass
from typing import Iterable

from experimental.swift.target_types import (
SWIFT_FILE_EXTENSIONS,
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/experimental/swift/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from __future__ import annotations

from typing import Iterable
from collections.abc import Iterable

from experimental.swift.goals import check, tailor
from experimental.swift.subsystems import toolchain
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/experimental/swift/subsystems/toolchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from __future__ import annotations

import os
from collections.abc import Iterable
from dataclasses import dataclass
from typing import Iterable

from pants.core.util_rules.system_binaries import (
BinaryNotFoundError,
Expand Down
2 changes: 1 addition & 1 deletion pants-plugins/experimental/swift/util_rules/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

from __future__ import annotations

from collections.abc import Iterable
from dataclasses import dataclass
from typing import Iterable

from experimental.swift.subsystems.toolchain import SwiftToolchain
from experimental.swift.target_types import SwiftFieldSet, SwiftSourceField
Expand Down
10 changes: 6 additions & 4 deletions pants.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[GLOBAL]
pants_version = "2.16.0rc3"
pants_version = "2.20.1"
pantsd = true
build_patterns = ["BUILD.pants", "BUILD"]
pythonpath = ["pants-plugins"]
print_stacktrace = true
plugins=["libcst==1.3.1"]

backend_packages = [
"pants.backend.plugin_development",
Expand All @@ -23,6 +24,7 @@ backend_packages = [
"pants.backend.shell",
"pants.backend.shell.lint.shellcheck",
"pants.backend.shell.lint.shfmt",
"experimental.migrate",
"experimental.scie",
# "experimental.mypyc",
#"experimental.ansible",
Expand Down Expand Up @@ -65,9 +67,9 @@ args = "--profile black"
[pyupgrade]
args = "--py39-plus"

[setuptools]
extra_requirements = ["wheel", "mypy"]
lockfile = "build-support/setuptools.txt"
#[setuptools]
#extra_requirements = ["wheel", "mypy"]
#lockfile = "build-support/setuptools.txt"

[shfmt]
# See https://github.com/mvdan/sh/blob/master/cmd/shfmt/shfmt.1.scd#printer-flags.
Expand Down

0 comments on commit c874ae9

Please sign in to comment.