Skip to content

Commit 1864931

Browse files
bazel: add py_pex_binary support from aspect_rules_py
1 parent 245418a commit 1864931

File tree

5 files changed

+312
-14
lines changed

5 files changed

+312
-14
lines changed

BUILD.bazel

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
2+
load("@buildifier_prebuilt//:rules.bzl", "buildifier")
23
load("@rules_python//python:py_binary.bzl", "py_binary")
34

5+
buildifier(
6+
name = "buildifier",
7+
lint_mode = "fix",
8+
mode = "fix",
9+
)
10+
411
# use default Python toolchain
512
py_binary(
613
name = "main",
714
srcs = ["main.py"],
815
deps = [
9-
"@pypi//loguru",
16+
"@pypi//loguru",
1017
],
1118
)
1219

@@ -15,37 +22,38 @@ py_binary(
1522
name = "main-3_10",
1623
srcs = ["main.py"],
1724
main = "main.py",
25+
python_version = "3.10",
1826
deps = [
19-
"@pypi//loguru",
27+
"@pypi//loguru",
2028
],
21-
python_version="3.10"
2229
)
2330

2431
# use particular Python toolchain (3.11)
2532
py_binary(
2633
name = "main-3_11",
27-
main = "main.py",
2834
srcs = ["main.py"],
35+
main = "main.py",
36+
python_version = "3.11",
2937
deps = [
30-
"@pypi//loguru",
38+
"@pypi//loguru",
3139
],
32-
python_version="3.11"
3340
)
3441

3542
genrule(
3643
name = "python-helper",
3744
outs = ["data.json"],
38-
toolchains=["@rules_python//python:current_py_toolchain"],
3945
# Path to Python interpreter via a MAKE variable
4046
# https://rules-python.readthedocs.io/en/latest/toolchains.html#toolchain-usage-in-other-rules
4147
cmd = "$(PYTHON3) -c \"import json; print(json.dumps({'a': 'b'}, indent=2))\" > $@",
48+
toolchains = ["@rules_python//python:current_py_toolchain"],
4249
)
4350

4451
# flag to request using a local Python interpreter
4552
config_setting(
4653
name = "is_py_local",
4754
flag_values = {":py": "local-3_12"},
4855
)
56+
4957
string_flag(
5058
name = "py",
5159
build_setting_default = "",

MODULE.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
bazel_dep(name = "buildifier_prebuilt", version = "8.0.3", dev_dependency = True)
2+
13
bazel_dep(name = "bazel_skylib", version = "1.7.1")
24
bazel_dep(name = "rules_python", version = "1.5.0-rc4")
5+
bazel_dep(name = "aspect_rules_py", version = "1.6.0")
36

47
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
58
python.defaults(python_version = "3.11")
69

710
# register multiple toolchains with different Python versions
811
python.toolchain(python_version = "3.10")
9-
python.toolchain(python_version = "3.11", configure_coverage_tool = True)
12+
python.toolchain(
13+
configure_coverage_tool = True,
14+
python_version = "3.11",
15+
)
1016

1117
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
1218

0 commit comments

Comments
 (0)