From 55594a4a7041f7897dfa04833732f54620c369b9 Mon Sep 17 00:00:00 2001 From: Alexey Tereshenkov <50622389+AlexTereshenkov@users.noreply.github.com> Date: Tue, 24 Jan 2023 21:36:53 +0000 Subject: [PATCH] plugins: extended inferred dependencies from a plugin --- helloworld/greet/greeting.py | 2 + pants-plugins/__init__.py | 0 pants-plugins/internal_plugins/__init__.py | 0 .../unowned_dependencies/__init__.py | 0 .../unowned_dependencies/register.py | 5 +++ .../unowned_dependencies/rules.py | 38 +++++++++++++++++++ pants.toml | 7 +++- 7 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 pants-plugins/__init__.py create mode 100644 pants-plugins/internal_plugins/__init__.py create mode 100644 pants-plugins/internal_plugins/unowned_dependencies/__init__.py create mode 100644 pants-plugins/internal_plugins/unowned_dependencies/register.py create mode 100644 pants-plugins/internal_plugins/unowned_dependencies/rules.py diff --git a/helloworld/greet/greeting.py b/helloworld/greet/greeting.py index 6a5eb1a..3c95c9a 100644 --- a/helloworld/greet/greeting.py +++ b/helloworld/greet/greeting.py @@ -10,6 +10,8 @@ from helloworld.translator.translator import LanguageTranslator +from helloworld.translator.foobar import baz + class Greeter: def __init__( diff --git a/pants-plugins/__init__.py b/pants-plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pants-plugins/internal_plugins/__init__.py b/pants-plugins/internal_plugins/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pants-plugins/internal_plugins/unowned_dependencies/__init__.py b/pants-plugins/internal_plugins/unowned_dependencies/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/pants-plugins/internal_plugins/unowned_dependencies/register.py b/pants-plugins/internal_plugins/unowned_dependencies/register.py new file mode 100644 index 0000000..f7bdc91 --- /dev/null +++ b/pants-plugins/internal_plugins/unowned_dependencies/register.py @@ -0,0 +1,5 @@ +from internal_plugins.unowned_dependencies.rules import rules as unowned_deps_rules + + +def rules(): + return [*unowned_deps_rules()] diff --git a/pants-plugins/internal_plugins/unowned_dependencies/rules.py b/pants-plugins/internal_plugins/unowned_dependencies/rules.py new file mode 100644 index 0000000..14b754b --- /dev/null +++ b/pants-plugins/internal_plugins/unowned_dependencies/rules.py @@ -0,0 +1,38 @@ +from dataclasses import dataclass + +from pants.backend.python.target_types import PythonSourcesGeneratingSourcesField +from pants.engine.internals.graph import Owners, OwnersRequest +from pants.engine.internals.selectors import Get +from pants.engine.rules import rule, collect_rules +from pants.engine.target import InferDependenciesRequest, InferredDependencies, FieldSet, MultipleSourcesField +from pants.engine.unions import UnionRule + +import logging + +logger = logging.getLogger(__name__) + + +@dataclass(frozen=True) +class SpecialDependenciesInferenceFieldSet(FieldSet): + required_fields = (PythonSourcesGeneratingSourcesField,) + sources: MultipleSourcesField + + +class InferSpecialDependenciesRequest(InferDependenciesRequest): + infer_from = SpecialDependenciesInferenceFieldSet + + +@rule +async def infer_special_dependencies(request: InferSpecialDependenciesRequest) -> InferredDependencies: + logger.warning(request) + owners = await Get(Owners, OwnersRequest(("helloworld/translator/foobar.py",))) + logger.warning(owners) + return InferredDependencies(include=owners) + + +def rules(): + return [ + *collect_rules(), + UnionRule(InferDependenciesRequest, InferSpecialDependenciesRequest), + ] + diff --git a/pants.toml b/pants.toml index fc5bff0..2457339 100644 --- a/pants.toml +++ b/pants.toml @@ -3,6 +3,7 @@ [GLOBAL] pants_version = "2.14.0" +pythonpath = ["%(buildroot)s/pants-plugins"] backend_packages.add = [ "pants.backend.build_files.fmt.black", "pants.backend.python", @@ -11,6 +12,7 @@ backend_packages.add = [ "pants.backend.python.lint.flake8", "pants.backend.python.lint.isort", "pants.backend.python.typecheck.mypy", + "internal_plugins.unowned_dependencies", ] # Pants' sponsor, Toolchain, offers remote caching, which can improve your CI performance with minimal configuration. @@ -37,7 +39,7 @@ repo_id = "3B1D361B-E9F1-49A8-B761-03DCC41FD58E" [source] # The Python source root is the repo root. See https://www.pantsbuild.org/docs/source-roots. -root_patterns = ["/"] +root_patterns = ["/", "/pants-plugins"] [python] # The default interpreter constraints for code in this repo. Individual targets can override @@ -62,3 +64,6 @@ resolves = { python-default = "python-default.lock"} # problematic system Pythons. See # https://www.pantsbuild.org/docs/python-interpreter-compatibility#changing-the-interpreter-search-path. search_path = ["", ""] + +[python-infer] +unowned_dependency_behavior = "error"