Skip to content

Commit

Permalink
feat: expose py_venv rule, allowing creation of virtual envs from mul…
Browse files Browse the repository at this point in the history
…tiple targets
  • Loading branch information
mattem committed Sep 8, 2023
1 parent f389597 commit 0ed8603
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
28 changes: 25 additions & 3 deletions docs/rules.md

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

8 changes: 6 additions & 2 deletions py/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ load("//py/private:py_pytest_main.bzl", _py_pytest_main = "py_pytest_main")
load("//py/private:py_wheel.bzl", "py_wheel_lib")
load("//py/private/venv:venv.bzl", _py_venv = "py_venv")

def py_library(name, imports = ["."], **kwargs):
def py_library(name, **kwargs):
"""Wrapper macro for the py_library rule, setting a default for imports
Args:
name: name of the rule
**kwargs: see [py_library attributes](./py_library)
"""

_py_library(
name = name,
imports = imports,
**kwargs
)

Expand All @@ -29,8 +29,10 @@ def py_binary(name, srcs = [], main = None, imports = ["."], **kwargs):
name: name of the rule
srcs: python source files
main: the entry point. If absent, then the first entry in srcs is used.
imports: List of import paths to add for this binary.
**kwargs: see [py_binary attributes](./py_binary)
"""

if not main and not len(srcs):
fail("When 'main' is not specified, 'srcs' must be non-empty")
_py_binary(
Expand All @@ -51,6 +53,7 @@ def py_binary(name, srcs = [], main = None, imports = ["."], **kwargs):

def py_test(name, main = None, srcs = [], imports = ["."], **kwargs):
"Identical to py_binary, but produces a target that can be used with `bazel test`."

_py_test(
name = name,
srcs = srcs,
Expand All @@ -74,3 +77,4 @@ py_wheel = rule(
)

py_pytest_main = _py_pytest_main
py_venv = _py_venv
3 changes: 3 additions & 0 deletions py/private/py_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_manifest
load("@aspect_bazel_lib//lib:expand_make_vars.bzl", "expand_locations", "expand_variables")
load("//py/private:py_library.bzl", _py_library = "py_library_utils")
load("//py/private:providers.bzl", "PyWheelInfo")
load("//py/private:py_wheel.bzl", py_wheel = "py_wheel_lib")
load("//py/private:utils.bzl", "PY_TOOLCHAIN", "SH_TOOLCHAIN", "dict_to_exports", "resolve_toolchain")
load("//py/private/venv:venv.bzl", _py_venv = "py_venv_utils")

Expand Down Expand Up @@ -78,6 +79,7 @@ def _py_binary_rule_imp(ctx):
ctx,
extra_source_attributes = ["main"]
)
py_wheel_info = py_wheel.make_py_wheel_info(ctx, ctx.attr.deps)

return [
DefaultInfo(
Expand All @@ -93,6 +95,7 @@ def _py_binary_rule_imp(ctx):
uses_shared_libraries = False,
),
instrumented_files_info,
py_wheel_info,
]

_attrs = dict({
Expand Down
4 changes: 2 additions & 2 deletions py/private/venv/venv.bzl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
load("@aspect_bazel_lib//lib:paths.bzl", "BASH_RLOCATION_FUNCTION", "to_manifest_path")
load("//py/private:providers.bzl", "PyWheelInfo")
load("//py/private:py_library.bzl", _py_library = "py_library_utils")
load("//py/private:utils.bzl", "PY_TOOLCHAIN", "SH_TOOLCHAIN", "dict_to_exports", "resolve_toolchain")
load("//py/private:utils.bzl", "PY_TOOLCHAIN", "SH_TOOLCHAIN", "resolve_toolchain")

def _wheel_path_map(file):
return file.path
Expand All @@ -16,7 +16,7 @@ def _get_attr(ctx, attr, override):
else:
return override

def _make_venv(ctx, name = None, main = None, strip_pth_workspace_root = None):
def _make_venv(ctx, name = None, strip_pth_workspace_root = None):
bash_bin = ctx.toolchains[SH_TOOLCHAIN].path
interpreter = resolve_toolchain(ctx)

Expand Down
2 changes: 1 addition & 1 deletion py/tests/external-deps/expected_pathing
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ sys path:
(py_toolchain)/lib/python3.9/lib-dynload
(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/pathing.venv/lib/python3.9/site-packages
(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles
(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps
(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py
(pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps

Entrypoint Path: (pwd)/bazel-out/[exec]/bin/py/tests/external-deps/pathing.runfiles/aspect_rules_py/py/tests/external-deps/pathing.py

Expand Down

0 comments on commit 0ed8603

Please sign in to comment.