Skip to content

Commit 040f6e5

Browse files
authored
Add support for repo_contents_cache (#15)
1 parent 18792b9 commit 040f6e5

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

gcs/private/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ bzl_library(
2424
"//docs:__pkg__",
2525
"//gcs/private:__subpackages__",
2626
],
27+
deps = ["@bazel_tools//tools/build_defs/repo:utils.bzl"],
2728
)
2829

2930
filegroup(

gcs/private/repo_rules/eager.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ def _eager_impl(repository_ctx):
2424

2525
# start downloads
2626
waiters = []
27+
reproducible = True
2728
for local_path, info in deps.items():
29+
reproducible = reproducible and info_is_reproducible(info)
2830
args = info_to_download_args(repository_ctx.attr.bucket, local_path, info)
2931
waiters.append(repository_ctx.download(**args))
3032

@@ -36,6 +38,11 @@ def _eager_impl(repository_ctx):
3638
for waiter in waiters:
3739
waiter.wait()
3840

41+
if hasattr(repository_ctx, "repo_metadata"):
42+
# allows participating in repo contents cache
43+
return repository_ctx.repo_metadata(reproducible = reproducible)
44+
return None
45+
3946
def info_to_download_args(bucket_name, local_path, info):
4047
args = {
4148
"url": bucket_url(bucket_name, info["remote_path"]),
@@ -51,6 +58,10 @@ def info_to_download_args(bucket_name, local_path, info):
5158
args["integrity"] = info["integrity"]
5259
return args
5360

61+
def info_is_reproducible(info):
62+
digest = info.get("sha256") or info.get("integrity")
63+
return len(digest) > 0
64+
5465
eager = repository_rule(
5566
implementation = _eager_impl,
5667
attrs = {

gcs/private/repo_rules/gcs_archive.bzl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
1717

18-
load("//gcs/private:util.bzl", "bucket_url", "download_and_extract_args", "parse_gs_url")
18+
load("//gcs/private:util.bzl", "download_and_extract_args", "parse_gs_url", "update_integrity_attr")
1919

2020
def _gcs_archive_impl(repository_ctx):
2121
gs_url = repository_ctx.attr.url
@@ -24,7 +24,7 @@ def _gcs_archive_impl(repository_ctx):
2424

2525
# download && extract the repo
2626
args = download_and_extract_args(repository_ctx.attr, target["bucket_name"], target["remote_path"])
27-
repository_ctx.download_and_extract(**args)
27+
download_info = repository_ctx.download_and_extract(**args)
2828

2929
# apply patches after extraction has finished
3030
for patch in repository_ctx.attr.patches:
@@ -39,6 +39,7 @@ def _gcs_archive_impl(repository_ctx):
3939
repository_ctx.file("BUILD.bazel", repository_ctx.attr.build_file_content)
4040
if has_build_file_label:
4141
repository_ctx.file("BUILD.bazel", repository_ctx.read(repository_ctx.attr.build_file))
42+
return update_integrity_attr(repository_ctx, _gcs_archive_attrs, download_info)
4243

4344
_gcs_archive_doc = """Downloads a Bazel repository as a compressed archive file from a GCS bucket, decompresses it,
4445
and makes its targets available for binding.

gcs/private/repo_rules/gcs_file.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
1717

18-
load("//gcs/private:url_encoding.bzl", "url_encode")
19-
load("//gcs/private:util.bzl", "bucket_url", "download_args", "parse_gs_url", "have_unblocked_downloads")
18+
load("//gcs/private:util.bzl", "download_args", "parse_gs_url", "have_unblocked_downloads", "update_integrity_attr")
2019

2120
def _gcs_file_impl(repository_ctx):
2221
gs_url = repository_ctx.attr.url
@@ -26,6 +25,7 @@ def _gcs_file_impl(repository_ctx):
2625
# start download
2726
args = download_args(repository_ctx.attr, target["bucket_name"], target["remote_path"])
2827
waiter = repository_ctx.download(**args)
28+
download_info = waiter
2929

3030
# populate BUILD files
3131
repository_ctx.file("BUILD.bazel", "exports_files(glob([\"**\"]))".format(args["output"]))
@@ -36,7 +36,8 @@ def _gcs_file_impl(repository_ctx):
3636

3737
# wait for download to finish
3838
if have_unblocked_downloads():
39-
waiter.wait()
39+
download_info = waiter.wait()
40+
return update_integrity_attr(repository_ctx, _gcs_file_attrs, download_info)
4041

4142
_gcs_file_doc = """Downloads a file from a GCS bucket and makes it available to be used as a file group.
4243

gcs/private/util.bzl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ See the License for the specific language governing permissions and
1515
limitations under the License.
1616
"""
1717

18+
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "update_attrs")
1819
load("//gcs/private:jsonpath.bzl", "walk_jsonpath")
1920
load("//gcs/private:url_encoding.bzl", "url_encode")
2021

@@ -141,3 +142,13 @@ def have_unblocked_downloads():
141142
if version[0] == 7 and version[1] < 1:
142143
return False
143144
return True
145+
146+
def update_integrity_attr(ctx, attrs, download_info):
147+
if not hasattr(ctx, "repo_metadata"):
148+
# Old Bazel versions do not support repo_metadata
149+
return None
150+
# We don't need to override the integrity attribute if sha256 is already specified.
151+
if ctx.attr.sha256 or ctx.attr.integrity:
152+
return ctx.repo_metadata(reproducible = True)
153+
integrity_override = {"integrity": download_info.integrity}
154+
return ctx.repo_metadata(attrs_for_reproducibility = update_attrs(ctx.attr, attrs.keys(), integrity_override))

0 commit comments

Comments
 (0)