Skip to content

Commit

Permalink
fix: use PyInfo from rules_package, fixes #60
Browse files Browse the repository at this point in the history
  • Loading branch information
oxidase committed Jun 1, 2024
1 parent 52c20d9 commit 0b4ca4b
Show file tree
Hide file tree
Showing 24 changed files with 127 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
startup --windows_enable_symlinks
common --enable_bzlmod
query --deleted_packages=examples/simple,examples/torch,examples/transitions,examples/markers
build --deleted_packages=examples/simple,examples/torch,examples/transitions,examples/markers
query --deleted_packages=examples/simple,examples/torch,examples/transitions,examples/markers,examples/aspect_rules_py
build --deleted_packages=examples/simple,examples/torch,examples/transitions,examples/markers,examples/aspect_rules_py
build --enable_runfiles
build --incompatible_default_to_explicit_init_py
test --test_output=errors
1 change: 1 addition & 0 deletions .bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7.1.2
5 changes: 3 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.5.0")
bazel_dep(name = "platforms", version = "0.0.8")
bazel_dep(name = "bazel_skylib", version = "1.7.0")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "rules_python", version = "0.32.2")

internal_deps = use_extension("@rules_poetry//python:extensions.bzl", "internal_deps")
use_repo(internal_deps, "rules_poetry_deps", "rules_poetry_pip")
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This allows to use platform information of resolved Python toolchains and build
To import `rules_poetry` in your project, you first need to add it to your `MODULE.bazel` file

```python
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "rules_python", version = "0.32.2")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.12")
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions examples/aspect_rules_py/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build --sandbox_default_allow_network=false
9 changes: 9 additions & 0 deletions examples/aspect_rules_py/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
load("@aspect_rules_py//py:defs.bzl", "py_binary")

py_test(
name = "test",
srcs = ["test.py"],
deps = [
"@poetry//:absl-py",
],
)
27 changes: 27 additions & 0 deletions examples/aspect_rules_py/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module(
name = "poetry_demo",
version = "0.1.0",
)

bazel_dep(name = "aspect_rules_py", version = "0.7.3")
bazel_dep(name = "rules_python", version = "0.32.2")

# Register a hermetic Python toolchain rather than rely on a locally-installed
# interpreter.
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
configure_coverage_tool = True,
is_default = True,
python_version = "3.11",
)
use_repo(python, "python_3_11")

# https://registry.bazel.build/modules/rules_poetry
bazel_dep(name = "rules_poetry", version = "0.3.4")

poetry = use_extension("@rules_poetry//python:extensions.bzl", "poetry")
poetry.parse(
name = "poetry",
lock = "//:poetry.lock",
)
use_repo(poetry, "poetry")
File renamed without changes.
17 changes: 17 additions & 0 deletions examples/aspect_rules_py/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/aspect_rules_py/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tool.poetry]
name = "poetry-demo"
version = "0.1.0"
authors = []
description = "Bazel Python example using Poetry"
package-mode = false

[tool.poetry.dependencies]
python = "~3.11"
absl-py = "^2.1.0"
10 changes: 10 additions & 0 deletions examples/aspect_rules_py/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from absl import app


def main(argv):
print("Hello, Python and Poetry!")
return 0


if __name__ == "__main__":
app.run(main)
2 changes: 1 addition & 1 deletion examples/markers/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "rules_python", version = "0.32.2")

python_version = "3_12"

Expand Down
1 change: 0 additions & 1 deletion examples/simple/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion examples/simple/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "rules_python", version = "0.32.2")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.12")
Expand Down
16 changes: 8 additions & 8 deletions examples/simple/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/torch/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "rules_python", version = "0.32.2")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.12")
Expand Down
1 change: 0 additions & 1 deletion examples/transitions/.gitignore

This file was deleted.

2 changes: 1 addition & 1 deletion examples/transitions/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bazel_dep(name = "rules_python", version = "0.31.0")
bazel_dep(name = "rules_python", version = "0.32.2")
bazel_dep(name = "platforms", version = "0.0.8")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
Expand Down
3 changes: 2 additions & 1 deletion lib/private/py_zip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Support for serverless deployments.
"""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("//python:poetry_deps.bzl", _get_imports = "get_imports")
load(":runfiles.bzl", _matches = "matches")

DEFAULT_STUB_SHEBANG = "#!/usr/bin/env python3"
Expand Down Expand Up @@ -66,7 +67,7 @@ def _py_zip_impl(ctx):
ctx.actions.write(manifest_file, "\n".join(args))

## Genrate a JSON files with enviroment variables for downstream consumers
python_paths = [workspace_dir] + [path for path in target[PyInfo].imports.to_list()]
python_paths = [workspace_dir] + _get_imports(target).to_list()
json_file = ctx.actions.declare_file(basename + ".json")
ctx.actions.write(json_file, json.encode({"environment": {"PYTHONPATH": ":".join(python_paths)}}))

Expand Down
3 changes: 2 additions & 1 deletion python/poetry.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load("@rules_poetry_deps//:defs.bzl", _python = "python")
load("//python:poetry_deps.bzl", _get_imports = "get_imports")

def _poetry_update_impl(ctx):
script = """#!{python}
Expand All @@ -23,7 +24,7 @@ if __name__ == "__main__":
runpy.run_module("poetry", run_name="__main__", alter_sys=True)
""".format(
python = _python,
deps = repr(["../{}".format(path) for path in ctx.attr._poetry_deps[PyInfo].imports.to_list()]),
deps = repr(["../{}".format(path) for path in _get_imports(ctx.attr._poetry_deps).to_list()]),
toml = ctx.attr.toml.files.to_list().pop().short_path,
lock = ctx.attr.lock.files.to_list().pop().short_path,
update = "" if ctx.attr.update else ', "--no-update"',
Expand Down
23 changes: 19 additions & 4 deletions python/poetry_deps.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_skylib//lib:versions.bzl", "versions")
load("@rules_python//python:defs.bzl", StarPyInfo = "PyInfo")
load("//python:markers.bzl", "evaluate", "parse")

# Environment Markers https://peps.python.org/pep-0508/#environment-markers
Expand Down Expand Up @@ -122,18 +123,20 @@ def _package_impl(ctx):
)

deps = [dep for dep in ctx.attr.deps if _include_dep(dep, ctx.attr.markers, tags)]
transitive_imports = [dep[PyInfo].imports for dep in deps]
transitive_depsets = [dep[PyInfo].transitive_sources for dep in deps]
transitive_imports = [get_imports(dep) for dep in deps]
transitive_depsets = [get_transitive_sources(dep) for dep in deps]
runfiles = [output] + [item for dep in transitive_depsets for item in dep.to_list()]
files = depset([output], transitive = transitive_depsets)
imports = depset([output.short_path.replace("../", "")], transitive = transitive_imports)
return [
DefaultInfo(files = files, runfiles = ctx.runfiles(files = runfiles)),
PyInfo(transitive_sources = files, imports = depset([output.short_path.replace("../", "")], transitive = transitive_imports)),
PyInfo(transitive_sources = files, imports = imports),
StarPyInfo(transitive_sources = files, imports = imports),
]

package = rule(
implementation = _package_impl,
provides = [PyInfo],
provides = [PyInfo, StarPyInfo],
attrs = {
"constraint": attr.string(mandatory = True, doc = "The package version constraint string"),
"deps": attr.label_list(doc = "The package dependencies list"),
Expand All @@ -154,3 +157,15 @@ package = rule(
"@bazel_tools//tools/python:toolchain_type",
],
)

def get_imports(target):
for info in [StarPyInfo, PyInfo]:
if info in target:
return target[info].imports
return depset()

def get_transitive_sources(target):
for info in [StarPyInfo, PyInfo]:
if info in target:
return target[info].transitive_sources
return depset()
14 changes: 10 additions & 4 deletions python/py_venv.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
load("@rules_python//python:defs.bzl", StarPyInfo = "PyInfo")
load("//python:poetry_deps.bzl", _get_imports = "get_imports", _get_transitive_sources = "get_transitive_sources")

def _py_venv_impl(ctx):
"""
Rule to link Python package into a virtual environment.
Expand All @@ -17,7 +20,8 @@ def _py_venv_impl(ctx):

deps = ctx.attr.deps
output = ctx.actions.declare_directory("venv/{}".format(ctx.label.name))
import_paths = ["{}/external/{}".format(ctx.bin_dir.path, path) for dep in deps for path in dep[PyInfo].imports.to_list()]
import_depsets = depset(transitive = [_get_imports(dep) for dep in deps])
import_paths = ["{}/external/{}".format(ctx.bin_dir.path, path) for path in import_depsets.to_list()]

ctx.actions.run(
outputs = [output],
Expand All @@ -29,18 +33,20 @@ def _py_venv_impl(ctx):
executable = ctx.executable._py_venv,
)

transitive_depsets = [dep[PyInfo].transitive_sources for dep in deps]
transitive_depsets = [_get_transitive_sources(dep) for dep in deps]
runfiles = [output] + [item for dep in transitive_depsets for item in dep.to_list()]
files = depset([output], transitive = transitive_depsets)
imports = depset(["_main/" + output.short_path])

return [
DefaultInfo(files = files, runfiles = ctx.runfiles(files = runfiles)),
PyInfo(transitive_sources = files, imports = depset(["_main/" + output.short_path])),
PyInfo(transitive_sources = files, imports = imports),
StarPyInfo(transitive_sources = files, imports = imports),
]

py_venv = rule(
implementation = _py_venv_impl,
provides = [PyInfo],
provides = [PyInfo, StarPyInfo],
attrs = {
"deps": attr.label_list(doc = "The package dependencies list"),
"_py_venv": attr.label(default = ":py_venv", cfg = "exec", executable = True),
Expand Down
2 changes: 1 addition & 1 deletion python/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ _POETRY_INTERNAL_DEPS = [
]

def _poetry_deps_repo_impl(ctx):
poetry_version = "1.8.2"
poetry_version = "1.8.3"

# Intentionally use a host default interpreter as the repository only used in host tooling targets
# This may lead to inconsistency if the repository will be used with a different toolchain
Expand Down

0 comments on commit 0b4ca4b

Please sign in to comment.