-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated to pants 2.20 and added small migration tool (#97)
- Loading branch information
1 parent
31e238c
commit c874ae9
Showing
26 changed files
with
3,224 additions
and
831 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
pants-plugins/experimental/ansible/lint/ansible_lint/subsystem.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(),) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters