Skip to content

Commit

Permalink
feat: use host interpret from rules_python
Browse files Browse the repository at this point in the history
  • Loading branch information
oxidase committed Jul 4, 2024
1 parent 0dea015 commit fc158ee
Show file tree
Hide file tree
Showing 29 changed files with 201 additions and 126 deletions.
11 changes: 7 additions & 4 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_skylib", version = "1.7.0")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.10")
bazel_dep(name = "rules_python", version = "0.32.2")
bazel_dep(name = "rules_python", version = "0.33.2")

internal_deps = use_extension("@rules_ophiuchus//python:extensions.bzl", "internal_deps")
use_repo(internal_deps, "rules_ophiuchus_pip", "rules_ophiuchus_poetry_deps")
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
use_repo(python, "python_3_11_host")

internal_deps = use_extension("@rules_ophiuchus//python:internal_deps.bzl", "internal_deps")
use_repo(internal_deps, "rules_ophiuchus_defs", "rules_ophiuchus_pip", "rules_ophiuchus_poetry_deps")
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This allows to use platform information of resolved Python toolchains and build

Minimum requirements:

* Bazel 6.x
* Bazel 6.x and rules_python with registered Python >= 3.11 toolchain.

## Getting started

Expand All @@ -17,7 +17,7 @@ Minimum requirements:
To import `rules_ophiuchus` in your project, you first need to add it to your `MODULE.bazel` file

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

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.12")
Expand Down
2 changes: 1 addition & 1 deletion examples/aspect_rules_py/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module(
)

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

# Register a hermetic Python toolchain rather than rely on a locally-installed
# interpreter.
Expand Down
2 changes: 1 addition & 1 deletion examples/cc_toolchain/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
bazel_dep(name = "rules_python", version = "0.32.2")
bazel_dep(name = "rules_python", version = "0.33.2")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.12")
Expand Down
2 changes: 1 addition & 1 deletion examples/markers/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
last_downstream_green
last_green
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.32.2")
bazel_dep(name = "rules_python", version = "0.33.2")

python_version = "3_12"

Expand Down
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.32.2")
bazel_dep(name = "rules_python", version = "0.33.2")

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.12")
Expand Down
22 changes: 11 additions & 11 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.32.2")
bazel_dep(name = "rules_python", version = "0.33.2")

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

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

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

python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(python_version = "3.12")
Expand Down
7 changes: 5 additions & 2 deletions examples/workspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ Explicitly set `--enable_bzlmod=false` to disable bzlmod.

Add rules_ophiuchus to `WORKSPACE` file as
```
load("@rules_ophiuchus//python:poetry_parse.bzl", "poetry_parse")
load("@rules_ophiuchus//python:repositories.bzl", install_poetry_dependencies = "install_dependencies")
install_poetry_dependencies()
install_poetry_dependencies("black_mamba", "3.11")
load("@rules_ophiuchus//python:poetry_parse.bzl", "poetry_parse")
poetry_parse(
name = "poetry",
lock = "//:poetry.lock",
)
```
where `black_mamba` and 3.11 are toolchain name and version for lock files processing.
20 changes: 18 additions & 2 deletions examples/workspace/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_python",
sha256 = "e3f1cc7a04d9b09635afb3130731ed82b5f58eadc8233d4efb59944d92ffc06f",
strip_prefix = "rules_python-0.33.2",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.33.2/rules_python-0.33.2.tar.gz",
)

load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains")

py_repositories()

python_register_toolchains("black_mamba", "3.11")

local_repository(
name = "rules_ophiuchus",
path = "../..",
)

load("@rules_ophiuchus//python:poetry_parse.bzl", "poetry_parse")
load("@rules_ophiuchus//python:repositories.bzl", install_poetry_dependencies = "install_dependencies")

install_poetry_dependencies()
install_poetry_dependencies("black_mamba", "3.11")

load("@rules_ophiuchus//python:poetry_parse.bzl", "poetry_parse")

poetry_parse(
name = "poetry",
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
load("@python//3.12:defs.bzl", py_test_3_12 = "py_test")
load("@python//3.11:defs.bzl", py_test_3_11 = "py_test")
load("@python//3.10:defs.bzl", py_test_3_10 = "py_test")
load("@python//3.9:defs.bzl", py_test_3_9 = "py_test")
load("@python//3.8:defs.bzl", py_test_3_8 = "py_test")
load("@copperhead//3.10:defs.bzl", py_test_3_10 = "py_test")
load("@copperhead//3.11:defs.bzl", py_test_3_11 = "py_test")
load("@copperhead//3.12:defs.bzl", py_test_3_12 = "py_test")
load("@copperhead//3.8:defs.bzl", py_test_3_8 = "py_test")
load("@copperhead//3.9:defs.bzl", py_test_3_9 = "py_test")

[
py_test(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@ Explicitly set `--enable_bzlmod=false` to disable bzlmod.

Add rules_ophiuchus to `WORKSPACE` file as
```
load("@rules_ophiuchus//python:poetry_parse.bzl", "poetry_parse")
load("@rules_ophiuchus//python:repositories.bzl", install_poetry_dependencies = "install_dependencies")
install_poetry_dependencies()
install_poetry_dependencies("copperhead_3_11", "3.11")
load("@rules_ophiuchus//python:poetry_parse.bzl", "poetry_parse")
poetry_parse(
name = "poetry",
name = "poetry_repo1",
lock = "//:poetry.lock",
)
poetry_parse(
name = "poetry_repo2",
lock = "//:poetry.lock",
)
```

where `copperhead_3_11` and 3.11 are toolchain name and version for lock files processing.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_python",
sha256 = "4912ced70dc1a2a8e4b86cec233b192ca053e82bc72d877b98e126156e8f228d",
strip_prefix = "rules_python-0.32.2",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.32.2/rules_python-0.32.2.tar.gz",
sha256 = "e3f1cc7a04d9b09635afb3130731ed82b5f58eadc8233d4efb59944d92ffc06f",
strip_prefix = "rules_python-0.33.2",
url = "https://github.com/bazelbuild/rules_python/releases/download/0.33.2/rules_python-0.33.2.tar.gz",
)

load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_multi_toolchains")
Expand All @@ -20,7 +20,7 @@ python_versions = [
]

python_register_multi_toolchains(
name = "python",
name = "copperhead",
default_version = python_versions[0],
python_versions = python_versions,
register_coverage_tool = True,
Expand All @@ -31,10 +31,11 @@ local_repository(
path = "../..",
)

load("@rules_ophiuchus//python:poetry_parse.bzl", "poetry_parse")
load("@rules_ophiuchus//python:repositories.bzl", install_poetry_dependencies = "install_dependencies")

install_poetry_dependencies()
install_poetry_dependencies("copperhead_3_11", "3.11")

load("@rules_ophiuchus//python:poetry_parse.bzl", "poetry_parse")

poetry_parse(
name = "poetry_repo1",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
11 changes: 0 additions & 11 deletions python/extensions.bzl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@rules_ophiuchus//python:repositories.bzl", "install_dependencies")
load("@rules_ophiuchus//python:poetry_parse.bzl", "poetry_parse")

def _poetry_impl(module_ctx):
Expand All @@ -24,13 +23,3 @@ poetry = module_extension(
),
},
)

def _internal_deps_impl(module_ctx):
install_dependencies()

internal_deps = module_extension(
implementation = _internal_deps_impl,
tag_classes = {
"install": tag_class(dict()),
},
)
11 changes: 11 additions & 0 deletions python/internal_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_ophiuchus//python:repositories.bzl", "install_dependencies")

def _internal_deps_impl(module_ctx):
install_dependencies("python_3_11", "3.11")

internal_deps = module_extension(
implementation = _internal_deps_impl,
tag_classes = {
"install": tag_class(dict()),
},
)
11 changes: 8 additions & 3 deletions python/poetry.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
load("@rules_ophiuchus_poetry_deps//:defs.bzl", _python = "python")
load("//python/private:poetry_deps.bzl", _get_imports = "get_imports")

def _poetry_update_impl(ctx):
python_toolchain = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"]
runtime_info = python_toolchain.py3_runtime

script = """#!{python}
import runpy
Expand All @@ -23,7 +25,7 @@ if __name__ == "__main__":
sys.argv = [sys.argv[0], "lock", f"--directory={{dir}}"{update}, *sys.argv[1:]]
runpy.run_module("poetry", run_name="__main__", alter_sys=True)
""".format(
python = _python,
python = runtime_info.interpreter.short_path,
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,
Expand All @@ -32,7 +34,7 @@ if __name__ == "__main__":

output = ctx.actions.declare_file(ctx.label.name + ".update")
ctx.actions.write(output, script, is_executable = True)
runfiles = ctx.runfiles(ctx.attr.toml.files.to_list() + ctx.attr.lock.files.to_list())
runfiles = ctx.runfiles(transitive_files = depset(transitive = [ctx.attr.toml.files, ctx.attr.lock.files, runtime_info.files]))
runfiles = runfiles.merge(ctx.attr._poetry_deps.default_runfiles)

return [
Expand All @@ -48,4 +50,7 @@ poetry_update = rule(
"update": attr.bool(default = True),
"_poetry_deps": attr.label(default = "@rules_ophiuchus_poetry_deps//:pkg"),
},
toolchains = [
"@bazel_tools//tools/python:toolchain_type",
],
)
Loading

0 comments on commit fc158ee

Please sign in to comment.