blob: 59f02141c0d07a56b2cea13186e763f56753ebea [file] [log] [blame] [edit]
# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
Repository rule for downloading files from Google Cloud Storage.
"""
_BUILD_TEMPLATE = """
# AUTO-GENERATED FILE. DO NOT EDIT.
#
# File downloaded from Google Cloud Storage.
filegroup(
name = "file",
srcs = ["{file}"],
# Use public visibility since bzlmod repo namespacing prevents unwanted
# visibility.
visibility = ["//visibility:public"],
)
"""
GS_ATTRS = {
"downloaded_file_path": attr.string(
doc = "Path assigned to the downloaded file.",
),
"url": attr.string(
doc = "gs:// URL from where the file is downloaded.",
mandatory = True,
),
"_gsutil": attr.label(
executable = True,
cfg = "exec",
default = Label("@chromite//:bin/gsutil"),
),
}
def download_gs_file(repository_ctx):
repository_ctx.report_progress("Downloading from GS.")
repository_ctx.execute(["mkdir", "file"])
url = repository_ctx.attr.url
if not url.startswith("gs://"):
fail("URL must start with \"gs://\". Got %s" % repository_ctx.url)
filename = repository_ctx.attr.downloaded_file_path
if not filename:
filename = url.split("/")[-1]
cmd = [
repository_ctx.attr._gsutil,
"cp",
url,
repository_ctx.path("file/" + filename),
]
st = repository_ctx.execute(
cmd,
working_directory = str(repository_ctx.workspace_root),
)
if st.return_code:
fail("Error running command %s:\n%s%s" % (cmd, st.stdout, st.stderr))
repository_ctx.file(
"file/BUILD.bazel",
_BUILD_TEMPLATE.format(file = filename),
)
gs_file = repository_rule(
implementation = download_gs_file,
doc = """
Downloads a file from Google Cloud Storage and and makes it available as a
file group.
""",
attrs = GS_ATTRS,
)