-
-
Notifications
You must be signed in to change notification settings - Fork 636
feat: add public API for analysis-phase logic #2252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,31 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
|
||
package( | ||
default_visibility = ["//:__subpackages__"], | ||
) | ||
|
||
bzl_library( | ||
name = "api_bzl", | ||
srcs = ["api.bzl"], | ||
visibility = ["//visibility:public"], | ||
deps = ["//python/private/api:api_bzl"], | ||
) | ||
|
||
filegroup( | ||
name = "distribution", | ||
srcs = glob(["**"]), | ||
) |
This file contains hidden or 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,5 @@ | ||
"""Public, analysis phase APIs for Python rules.""" | ||
|
||
load("//python/private/api:api.bzl", _py_common = "py_common") | ||
|
||
py_common = _py_common |
This file contains hidden or 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,43 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("@bazel_skylib//:bzl_library.bzl", "bzl_library") | ||
load(":py_common_api.bzl", "py_common_api") | ||
|
||
package( | ||
default_visibility = ["//:__subpackages__"], | ||
) | ||
|
||
py_common_api( | ||
name = "py_common_api", | ||
# NOTE: Not actually public. Implicit dependency of public rules. | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
bzl_library( | ||
name = "api_bzl", | ||
srcs = ["api.bzl"], | ||
deps = [ | ||
"//python/private:py_info_bzl", | ||
], | ||
) | ||
|
||
bzl_library( | ||
name = "py_common_api_bzl", | ||
srcs = ["py_common_api.bzl"], | ||
deps = [ | ||
":api_bzl", | ||
"//python/private:py_info_bzl", | ||
], | ||
) |
This file contains hidden or 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,55 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Implementation of py_api.""" | ||
|
||
_PY_COMMON_API_LABEL = Label("//python/private/api:py_common_api") | ||
|
||
ApiImplInfo = provider( | ||
doc = "Provider to hold an API implementation", | ||
fields = { | ||
"impl": """ | ||
:type: struct | ||
|
||
The implementation of the API being provided. The object it contains | ||
will depend on the target that is providing the API struct. | ||
""", | ||
}, | ||
) | ||
|
||
def _py_common_get(ctx): | ||
"""Get the py_common API instance. | ||
|
||
NOTE: to use this function, the rule must have added `py_common.API_ATTRS` | ||
to its attributes. | ||
|
||
Args: | ||
ctx: {type}`ctx` current rule ctx | ||
|
||
Returns: | ||
{type}`PyCommonApi` | ||
""" | ||
|
||
# A generic provider is used to decouple the API implementations from | ||
# the loading phase of the rules using an implementation. | ||
return ctx.attr._py_common_api[ApiImplInfo].impl | ||
|
||
py_common = struct( | ||
get = _py_common_get, | ||
API_ATTRS = { | ||
"_py_common_api": attr.label( | ||
default = _PY_COMMON_API_LABEL, | ||
providers = [ApiImplInfo], | ||
), | ||
}, | ||
) |
This file contains hidden or 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,38 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Implementation of py_api.""" | ||
|
||
load("//python/private:py_info.bzl", "PyInfoBuilder") | ||
load("//python/private/api:api.bzl", "ApiImplInfo") | ||
|
||
def _py_common_api_impl(ctx): | ||
_ = ctx # @unused | ||
return [ApiImplInfo(impl = PyCommonApi)] | ||
|
||
py_common_api = rule( | ||
implementation = _py_common_api_impl, | ||
doc = "Rule implementing py_common API.", | ||
) | ||
|
||
def _merge_py_infos(transitive, *, direct = []): | ||
builder = PyInfoBuilder() | ||
builder.merge_all(transitive, direct = direct) | ||
return builder.build() | ||
|
||
# Exposed for doc generation, not directly used. | ||
# buildifier: disable=name-conventions | ||
PyCommonApi = struct( | ||
merge_py_infos = _merge_py_infos, | ||
PyInfoBuilder = PyInfoBuilder, | ||
) |
This file contains hidden or 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 hidden or 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,17 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load(":py_common_tests.bzl", "py_common_test_suite") | ||
|
||
py_common_test_suite(name = "py_common_tests") |
This file contains hidden or 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,68 @@ | ||
# Copyright 2024 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""py_common tests.""" | ||
|
||
load("@rules_python_internal//:rules_python_config.bzl", "config") | ||
load("@rules_testing//lib:analysis_test.bzl", "analysis_test") | ||
load("@rules_testing//lib:test_suite.bzl", "test_suite") | ||
load("@rules_testing//lib:util.bzl", rt_util = "util") | ||
load("//python/api:api.bzl", _py_common = "py_common") | ||
load("//tests/support:py_info_subject.bzl", "py_info_subject") | ||
|
||
_tests = [] | ||
|
||
def _test_merge_py_infos(name): | ||
rt_util.helper_target( | ||
native.filegroup, | ||
name = name + "_subject", | ||
srcs = ["f1.py", "f1.pyc", "f2.py", "f2.pyc"], | ||
) | ||
analysis_test( | ||
name = name, | ||
impl = _test_merge_py_infos_impl, | ||
target = name + "_subject", | ||
attrs = _py_common.API_ATTRS, | ||
) | ||
|
||
def _test_merge_py_infos_impl(env, target): | ||
f1_py, f1_pyc, f2_py, f2_pyc = target[DefaultInfo].files.to_list() | ||
|
||
py_common = _py_common.get(env.ctx) | ||
|
||
py1 = py_common.PyInfoBuilder() | ||
if config.enable_pystar: | ||
py1.direct_pyc_files.add(f1_pyc) | ||
py1.transitive_sources.add(f1_py) | ||
|
||
py2 = py_common.PyInfoBuilder() | ||
if config.enable_pystar: | ||
py1.direct_pyc_files.add(f2_pyc) | ||
py2.transitive_sources.add(f2_py) | ||
|
||
actual = py_info_subject( | ||
py_common.merge_py_infos([py2.build()], direct = [py1.build()]), | ||
meta = env.expect.meta, | ||
) | ||
|
||
actual.transitive_sources().contains_exactly([f1_py.path, f2_py.path]) | ||
if config.enable_pystar: | ||
actual.direct_pyc_files().contains_exactly([f1_pyc.path, f2_pyc.path]) | ||
|
||
_tests.append(_test_merge_py_infos) | ||
|
||
def py_common_test_suite(name): | ||
test_suite( | ||
name = name, | ||
tests = _tests, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.