Skip to content

Commit c874ae9

Browse files
authored
Updated to pants 2.20 and added small migration tool (#97)
1 parent 31e238c commit c874ae9

File tree

26 files changed

+3224
-831
lines changed

26 files changed

+3224
-831
lines changed

build-support/pants-plugins/lock.txt

Lines changed: 185 additions & 291 deletions
Large diffs are not rendered by default.

build-support/python/default_lock.txt

Lines changed: 2889 additions & 513 deletions
Large diffs are not rendered by default.

pants-plugins/experimental/ansible/deploy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import logging
44
from abc import ABCMeta
5+
from collections.abc import Iterable
56
from dataclasses import dataclass
6-
from typing import Any, Iterable
7+
from typing import Any
78

89
from pants.core.goals.publish import (
910
NoApplicableTargetsBehavior,

pants-plugins/experimental/ansible/lint/ansible_lint/register.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from __future__ import annotations
88

9-
from typing import Iterable
9+
from collections.abc import Iterable
1010

1111
from experimental.ansible.lint.ansible_lint import rules as ansible_lint_rules
1212
from experimental.ansible.lint.ansible_lint import skip_field, subsystem

pants-plugins/experimental/ansible/lint/ansible_lint/rules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from __future__ import annotations
55

66
import logging
7+
from collections.abc import Iterable
78
from dataclasses import dataclass
8-
from typing import Iterable
99

1010
from experimental.ansible.lint.ansible_lint.subsystem import AnsibleLint
1111
from experimental.ansible.target_types import AnsibleSourceField

pants-plugins/experimental/ansible/lint/ansible_lint/rules_integration_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
from __future__ import annotations
55

6+
from collections.abc import Iterable
67
from textwrap import dedent
7-
from typing import Iterable
88

99
import pytest
1010
from experimental.ansible.lint.ansible_lint import rules as ansible_lint_rules

pants-plugins/experimental/ansible/lint/ansible_lint/skip_field.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from __future__ import annotations
55

6-
from typing import Iterable
6+
from collections.abc import Iterable
77

88
from experimental.ansible.target_types import (
99
AnsibleSourcesGeneratorTarget,

pants-plugins/experimental/ansible/lint/ansible_lint/subsystem.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import annotations
22

33
import os
4-
from typing import Iterable
4+
from collections.abc import Iterable
55

66
from pants.backend.python.goals.export import ExportPythonTool, ExportPythonToolSentinel
77
from pants.backend.python.subsystems.python_tool_base import PythonToolBase

pants-plugins/experimental/ansible/register.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22

3-
from typing import Iterable
3+
from collections.abc import Iterable
44

55
from experimental.ansible.deploy import rules as deploy_rules
66
from experimental.ansible.goals import tailor

pants-plugins/experimental/ansible/rules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import annotations
22

33
import logging
4+
from collections.abc import Iterable
45
from dataclasses import dataclass
5-
from typing import Iterable
66

77
from experimental.ansible.deploy import DeploymentFieldSet, DeployResult, DeployResults
88
from experimental.ansible.subsystems.ansible import Ansible
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
python_sources()

pants-plugins/experimental/migrate/__init__.py

Whitespace-only changes.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
from __future__ import annotations
5+
6+
from collections.abc import Iterable
7+
8+
from experimental.migrate.rules import rules as migrate_rules
9+
from pants.engine.rules import Rule
10+
from pants.engine.unions import UnionRule
11+
12+
13+
def rules() -> Iterable[Rule | UnionRule]:
14+
return (*migrate_rules(),)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
from __future__ import annotations
5+
6+
from collections.abc import Iterable
7+
from pathlib import Path
8+
9+
import libcst
10+
import libcst.matchers as m
11+
from experimental.migrate.subsystems import Migrate, MigrateSubsystem
12+
from libcst import RemovalSentinel, RemoveFromParent
13+
from pants.engine.console import Console
14+
from pants.engine.rules import Rule, collect_rules, goal_rule
15+
from pants.engine.target import UnexpandedTargets
16+
17+
18+
class RemoveRuleTransformer(m.MatcherDecoratableTransformer):
19+
@m.leave(
20+
m.ImportFrom(
21+
module=m.DoNotCare(),
22+
names=[
23+
m.ZeroOrMore(),
24+
m.ImportAlias(name=m.Name("rule_helper")),
25+
m.ZeroOrMore(),
26+
],
27+
)
28+
)
29+
def handle_imports(
30+
self, original_node: libcst.ImportFrom, updated_node: libcst.ImportFrom
31+
) -> libcst.ImportFrom | RemovalSentinel:
32+
assert not isinstance(original_node.names, libcst.ImportStar)
33+
34+
if len(original_node.names) == 1:
35+
return RemoveFromParent()
36+
37+
return updated_node.with_changes(
38+
names=[n for n in original_node.names if n.evaluated_name != "rule_helper"],
39+
# This is a workaround for https://github.com/Instagram/LibCST/issues/532
40+
# Formatters/isort will clean this up, but it doesn't compile without this
41+
lpar=libcst.LeftParen(),
42+
rpar=libcst.RightParen(),
43+
)
44+
45+
@m.leave(m.Decorator(decorator=m.Name("rule_helper")))
46+
def handle_decorator(
47+
self, original_node: libcst.Decorator, updated_node: libcst.Decorator
48+
) -> libcst.Decorator | RemovalSentinel:
49+
return RemoveFromParent()
50+
51+
52+
# TODO: This will need to become a BuiltinGoal, so just hacking around to get a list of Targets
53+
# Normally, will use the same code for "call-by-name-migration"
54+
@goal_rule
55+
async def migrate(
56+
console: Console, subsystem: MigrateSubsystem, targets: UnexpandedTargets
57+
) -> Migrate:
58+
filenames = [t.address.filename for t in targets if t.address.is_file_target]
59+
60+
for f in sorted(filenames):
61+
file = Path(f)
62+
if file.suffix != ".py":
63+
continue
64+
with open(file) as f:
65+
source = f.read()
66+
tree = libcst.parse_module(source)
67+
new_tree = tree.visit(RemoveRuleTransformer())
68+
new_source = new_tree.code
69+
70+
if source != new_source:
71+
console.print_stderr(f"Rewriting {file}")
72+
with open(file, "w") as f:
73+
f.write(new_source)
74+
75+
return Migrate(exit_code=0)
76+
77+
78+
def rules() -> Iterable[Rule]:
79+
return collect_rules()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
from __future__ import annotations
5+
6+
from collections.abc import Iterable
7+
8+
from pants.engine.goal import Goal, GoalSubsystem
9+
from pants.engine.rules import Rule, collect_rules
10+
11+
12+
class MigrateSubsystem(GoalSubsystem):
13+
name = "migrate"
14+
help = "???"
15+
16+
17+
class Migrate(Goal):
18+
subsystem_cls = MigrateSubsystem
19+
environment_behavior = Goal.EnvironmentBehavior.LOCAL_ONLY
20+
21+
22+
def rules() -> Iterable[Rule]:
23+
return (
24+
*collect_rules(),
25+
*MigrateSubsystem.rules(), # type: ignore[call-arg]
26+
)

pants-plugins/experimental/scie/register.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from __future__ import annotations
1010

11-
from typing import Iterable
11+
from collections.abc import Iterable
1212

1313
from experimental.scie.rules import rules as scie_rules
1414
from experimental.scie.target_types import ScieTarget

pants-plugins/experimental/scie/rules.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
import logging
77
import os
8-
from dataclasses import asdict, dataclass, replace
8+
from collections.abc import Iterable, Mapping
9+
from dataclasses import asdict, dataclass
910
from pathlib import PurePath
10-
from typing import Final, Iterable, Mapping
11+
from typing import Final
1112

1213
import toml
1314
from experimental.scie.config import Command, Config, File, Interpreter, LiftConfig
@@ -39,7 +40,7 @@
3940
)
4041
from pants.engine.platform import Platform
4142
from pants.engine.process import Process, ProcessResult
42-
from pants.engine.rules import Get, MultiGet, Rule, collect_rules, rule, rule_helper
43+
from pants.engine.rules import Get, MultiGet, Rule, collect_rules, rule
4344
from pants.engine.target import (
4445
DependenciesRequest,
4546
DescriptionField,
@@ -70,7 +71,6 @@ class ScieFieldSet(PackageFieldSet, RunFieldSet):
7071
lift: ScieLiftSourceField
7172

7273

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

114114

115-
@rule_helper
116115
async def _parse_lift_source(source: ScieLiftSourceField) -> Config:
117116
hydrated_source = await Get(HydratedSources, HydrateSourcesRequest(source))
118117
digest_contents = await Get(DigestContents, Digest, hydrated_source.snapshot.digest)

pants-plugins/experimental/scie/subsystems.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from __future__ import annotations
55

6-
from typing import Iterable
6+
from collections.abc import Iterable
77

88
from pants.core.util_rules.external_tool import TemplatedExternalTool
99
from pants.engine.rules import Rule, collect_rules

pants-plugins/experimental/scie/target_types.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
from pants.engine.target import (
1010
COMMON_TARGET_FIELDS,
1111
Dependencies,
12-
DictStringToStringField,
13-
NestedDictStringToStringField,
1412
OptionalSingleSourceField,
1513
StringSequenceField,
1614
Target,

pants-plugins/experimental/swift/goals/check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from __future__ import annotations
55

66
import logging
7+
from collections.abc import Iterable
78
from itertools import groupby
8-
from typing import Iterable
99

1010
from experimental.swift.subsystems.toolchain import SwiftSubsystem
1111
from experimental.swift.target_types import SwiftFieldSet, SwiftSourceField

pants-plugins/experimental/swift/goals/check_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
from __future__ import annotations
55

6+
from collections.abc import Iterable
67
from textwrap import dedent
7-
from typing import Iterable
88

99
import pytest
1010
from experimental.swift.goals import check

pants-plugins/experimental/swift/goals/tailor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
from __future__ import annotations
55

6+
from collections.abc import Iterable
67
from dataclasses import dataclass
7-
from typing import Iterable
88

99
from experimental.swift.target_types import (
1010
SWIFT_FILE_EXTENSIONS,

pants-plugins/experimental/swift/register.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
from __future__ import annotations
55

6-
from typing import Iterable
6+
from collections.abc import Iterable
77

88
from experimental.swift.goals import check, tailor
99
from experimental.swift.subsystems import toolchain

pants-plugins/experimental/swift/subsystems/toolchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from __future__ import annotations
55

66
import os
7+
from collections.abc import Iterable
78
from dataclasses import dataclass
8-
from typing import Iterable
99

1010
from pants.core.util_rules.system_binaries import (
1111
BinaryNotFoundError,

pants-plugins/experimental/swift/util_rules/compile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
from __future__ import annotations
55

6+
from collections.abc import Iterable
67
from dataclasses import dataclass
7-
from typing import Iterable
88

99
from experimental.swift.subsystems.toolchain import SwiftToolchain
1010
from experimental.swift.target_types import SwiftFieldSet, SwiftSourceField

pants.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
[GLOBAL]
2-
pants_version = "2.16.0rc3"
2+
pants_version = "2.20.1"
33
pantsd = true
44
build_patterns = ["BUILD.pants", "BUILD"]
55
pythonpath = ["pants-plugins"]
66
print_stacktrace = true
7+
plugins=["libcst==1.3.1"]
78

89
backend_packages = [
910
"pants.backend.plugin_development",
@@ -23,6 +24,7 @@ backend_packages = [
2324
"pants.backend.shell",
2425
"pants.backend.shell.lint.shellcheck",
2526
"pants.backend.shell.lint.shfmt",
27+
"experimental.migrate",
2628
"experimental.scie",
2729
# "experimental.mypyc",
2830
#"experimental.ansible",
@@ -65,9 +67,9 @@ args = "--profile black"
6567
[pyupgrade]
6668
args = "--py39-plus"
6769

68-
[setuptools]
69-
extra_requirements = ["wheel", "mypy"]
70-
lockfile = "build-support/setuptools.txt"
70+
#[setuptools]
71+
#extra_requirements = ["wheel", "mypy"]
72+
#lockfile = "build-support/setuptools.txt"
7173

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

0 commit comments

Comments
 (0)