Skip to content

Commit 028efa3

Browse files
UebelAndreJonathon Belotti
andauthored
changes to pip_repository source files now re-trigger the repo rule (#601)
* changes to pip_repository source files now re-trigger the repo rule * update diff test message. * added note about auto-gen. * Updated docs * remove unnecessary snippet * installer -> updater Co-authored-by: Jonathon Belotti <[email protected]>
1 parent d7ce569 commit 028efa3

File tree

12 files changed

+244
-5
lines changed

12 files changed

+244
-5
lines changed

docs/BUILD

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ bzl_library(
6161
bzl_library(
6262
name = "pip_install_bzl",
6363
srcs = [
64-
"//python/pip_install:pip_repository.bzl",
65-
"//python/pip_install:repositories.bzl",
66-
"//python/pip_install:requirements.bzl",
64+
"//python/pip_install:bzl",
6765
],
6866
deps = [
6967
":defs",

python/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ filegroup(
4646
"defs.bzl",
4747
"packaging.bzl",
4848
"pip.bzl",
49+
"//python/pip_install:bzl",
4950
"//python/private:bzl",
5051
],
5152
visibility = ["//:__pkg__"],

python/pip_install/BUILD

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,26 @@ filegroup(
77
"pip_compile.py",
88
"//python/pip_install/extract_wheels:distribution",
99
"//python/pip_install/parse_requirements_to_bzl:distribution",
10+
"//python/pip_install/private:distribution",
1011
],
1112
visibility = ["//:__pkg__"],
1213
)
1314

1415
filegroup(
1516
name = "bzl",
16-
srcs = glob(["*.bzl"]),
17-
visibility = ["//:__pkg__"],
17+
srcs = glob(["*.bzl"]) + [
18+
"//python/pip_install/private:bzl_srcs",
19+
],
20+
visibility = ["//:__subpackages__"],
21+
)
22+
23+
filegroup(
24+
name = "py_srcs",
25+
srcs = [
26+
"//python/pip_install/extract_wheels:py_srcs",
27+
"//python/pip_install/parse_requirements_to_bzl:py_srcs",
28+
],
29+
visibility = ["//visibility:public"],
1830
)
1931

2032
exports_files(

python/pip_install/extract_wheels/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,14 @@ filegroup(
1717
],
1818
visibility = ["//python/pip_install:__subpackages__"],
1919
)
20+
21+
filegroup(
22+
name = "py_srcs",
23+
srcs = glob(
24+
include = ["**/*.py"],
25+
exclude = ["**/*_test.py"],
26+
) + [
27+
"//python/pip_install/extract_wheels/lib:py_srcs",
28+
],
29+
visibility = ["//python/pip_install:__subpackages__"],
30+
)

python/pip_install/extract_wheels/lib/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,12 @@ filegroup(
110110
),
111111
visibility = ["//python/pip_install:__subpackages__"],
112112
)
113+
114+
filegroup(
115+
name = "py_srcs",
116+
srcs = glob(
117+
include = ["**/*.py"],
118+
exclude = ["**/*_test.py"],
119+
),
120+
visibility = ["//python/pip_install:__subpackages__"],
121+
)

python/pip_install/parse_requirements_to_bzl/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ filegroup(
4141
],
4242
visibility = ["//python/pip_install:__subpackages__"],
4343
)
44+
45+
filegroup(
46+
name = "py_srcs",
47+
srcs = glob(
48+
include = ["**/*.py"],
49+
exclude = ["**/*_test.py"],
50+
) + [
51+
"//python/pip_install/parse_requirements_to_bzl/extract_single_wheel:py_srcs",
52+
],
53+
visibility = ["//python/pip_install:__subpackages__"],
54+
)

python/pip_install/parse_requirements_to_bzl/extract_single_wheel/BUILD

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ filegroup(
66
),
77
visibility = ["//python/pip_install:__subpackages__"],
88
)
9+
10+
filegroup(
11+
name = "py_srcs",
12+
srcs = glob(
13+
include = ["**/*.py"],
14+
exclude = ["**/*_test.py"],
15+
),
16+
visibility = ["//python/pip_install:__subpackages__"],
17+
)

python/pip_install/pip_repository.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""
22

33
load("//python/pip_install:repositories.bzl", "all_requirements")
4+
load("//python/pip_install/private:srcs.bzl", "PIP_INSTALL_PY_SRCS")
45

56
def _construct_pypath(rctx):
67
"""Helper function to construct a PYTHONPATH.
@@ -250,6 +251,11 @@ For incremental mode the packages will be of the form
250251
default = 600,
251252
doc = "Timeout (in seconds) on the rule's execution duration.",
252253
),
254+
"_py_srcs": attr.label_list(
255+
doc = "Python sources used in the repository rule",
256+
allow_files = True,
257+
default = PIP_INSTALL_PY_SRCS,
258+
),
253259
}
254260

255261
pip_repository_attrs = {

python/pip_install/private/BUILD

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
load(":pip_install_utils.bzl", "srcs_module")
2+
3+
package(default_visibility = ["//:__subpackages__"])
4+
5+
exports_files([
6+
"srcs.bzl",
7+
])
8+
9+
filegroup(
10+
name = "distribution",
11+
srcs = glob(["*"]),
12+
visibility = ["//python/pip_install:__subpackages__"],
13+
)
14+
15+
filegroup(
16+
name = "bzl_srcs",
17+
srcs = glob(["*.bzl"]),
18+
)
19+
20+
srcs_module(
21+
name = "srcs_module",
22+
srcs = "//python/pip_install:py_srcs",
23+
dest = ":srcs.bzl",
24+
)
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
"""Utilities for `rules_python` pip rules"""
2+
3+
_SRCS_TEMPLATE = """\
4+
\"\"\"A generate file containing all source files used for `@rules_python//python/pip_install:pip_repository.bzl` rules
5+
6+
This file is auto-generated from the `@rules_python//python/pip_install/private:srcs_module.install` target. Please
7+
`bazel run` this target to apply any updates. Note that doing so will discard any local modifications.
8+
"\"\"
9+
10+
# Each source file is tracked as a target so `pip_repository` rules will know to automatically rebuild if any of the
11+
# sources changed.
12+
PIP_INSTALL_PY_SRCS = [
13+
{srcs}
14+
]
15+
"""
16+
17+
def _src_label(file):
18+
dir_path, file_name = file.short_path.rsplit("/", 1)
19+
20+
return "@rules_python//{}:{}".format(
21+
dir_path,
22+
file_name,
23+
)
24+
25+
def _srcs_module_impl(ctx):
26+
srcs = [_src_label(src) for src in ctx.files.srcs]
27+
if not srcs:
28+
fail("`srcs` cannot be empty")
29+
output = ctx.actions.declare_file(ctx.label.name)
30+
31+
ctx.actions.write(
32+
output = output,
33+
content = _SRCS_TEMPLATE.format(
34+
srcs = "\n ".join(["\"{}\",".format(src) for src in srcs]),
35+
),
36+
)
37+
38+
return DefaultInfo(
39+
files = depset([output]),
40+
)
41+
42+
_srcs_module = rule(
43+
doc = "A rule for writing a list of sources to a templated file",
44+
implementation = _srcs_module_impl,
45+
attrs = {
46+
"srcs": attr.label(
47+
doc = "A filegroup of source files",
48+
allow_files = True,
49+
),
50+
},
51+
)
52+
53+
_INSTALLER_TEMPLATE = """\
54+
#!/bin/bash
55+
set -euo pipefail
56+
cp -f "{path}" "${{BUILD_WORKSPACE_DIRECTORY}}/{dest}"
57+
"""
58+
59+
def _srcs_updater_impl(ctx):
60+
output = ctx.actions.declare_file(ctx.label.name + ".sh")
61+
target_file = ctx.file.input
62+
dest = ctx.file.dest.short_path
63+
64+
ctx.actions.write(
65+
output = output,
66+
content = _INSTALLER_TEMPLATE.format(
67+
path = target_file.short_path,
68+
dest = dest,
69+
),
70+
is_executable = True,
71+
)
72+
73+
return DefaultInfo(
74+
files = depset([output]),
75+
runfiles = ctx.runfiles(files = [target_file]),
76+
executable = output,
77+
)
78+
79+
_srcs_updater = rule(
80+
doc = "A rule for writing a `srcs.bzl` file back to the repository",
81+
implementation = _srcs_updater_impl,
82+
attrs = {
83+
"dest": attr.label(
84+
doc = "The target file to write the new `input` to.",
85+
allow_single_file = ["srcs.bzl"],
86+
mandatory = True,
87+
),
88+
"input": attr.label(
89+
doc = "The file to write back to the repository",
90+
allow_single_file = True,
91+
mandatory = True,
92+
),
93+
},
94+
executable = True,
95+
)
96+
97+
def srcs_module(name, dest, **kwargs):
98+
"""A helper rule to ensure `pip_repository` rules are always up to date
99+
100+
Args:
101+
name (str): The name of the sources module
102+
dest (str): The filename the module should be written as in the current package.
103+
**kwargs (dict): Additional keyword arguments
104+
"""
105+
tags = kwargs.pop("tags", [])
106+
107+
_srcs_module(
108+
name = name,
109+
tags = tags,
110+
**kwargs
111+
)
112+
113+
_srcs_updater(
114+
name = name + ".update",
115+
input = name,
116+
dest = dest,
117+
tags = tags,
118+
)

0 commit comments

Comments
 (0)