Skip to content

Commit 421ba80

Browse files
committed
Break up the path into bucket and relative path
1 parent 6584538 commit 421ba80

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

WORKSPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ load(":cloud_archive.bzl", "s3_archive")
44

55
s3_archive(
66
name = "archive",
7+
bucket = "1e100-temp",
78
build_file = "//:BUILD.archive",
9+
file_path = "cloud_archive_test.zip",
810
sha256 = "d0ff6239646b3a60e4d8926402281311e003ae03183b1ae24f2adba5d9289f04",
911
strip_prefix = "cloud_archive_test",
10-
url = "s3://depthwise-temp/cloud_archive_test.zip",
1112
)

cloud_archive.bzl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33

44

55
def _s3_archive_impl(ctx):
6-
url = ctx.attr.url
6+
url = "s3://{}/{}".format(ctx.attr.bucket, ctx.attr.file_path)
77
filename = ctx.path(url).basename
88

99
# Download
1010
aws_cli_path = ctx.which("aws")
11-
aws_cli_cmd = [aws_cli_path, "s3", "cp", url, "."]
11+
profile_flags = []
12+
if ctx.attr.aws_profile:
13+
profile_flags = ["--profile", ctx.attr.aws_profile]
14+
aws_cli_cmd = [aws_cli_path] + profile_flags + ["s3", "cp", url, "."]
1215
s3_result = ctx.execute(aws_cli_cmd)
1316
if s3_result.return_code != 0:
1417
fail("Failed to download {} from S3: {}".format(url, s3_result.stderr))
@@ -24,25 +27,29 @@ def _s3_archive_impl(ctx):
2427
url, ctx.attr.sha256, sha256))
2528

2629
# Extract the downloaded archive.
27-
print("Extracting {}".format(filename))
2830
ctx.extract(filename, stripPrefix=ctx.attr.strip_prefix)
2931

3032
# Provide external BUILD file if requested.
3133
bash_path = ctx.os.environ.get("BAZEL_SH", "bash")
3234
if ctx.attr.build_file:
33-
ctx.execute([bash_path, "-c", "rm -f BUILD BUILD.bazel"])
34-
ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
35+
ctx.execute([bash_path, "-c", "rm -f BUILD BUILD.bazel"])
36+
ctx.symlink(ctx.attr.build_file, "BUILD.bazel")
3537

3638

3739
s3_archive = repository_rule(
3840
implementation=_s3_archive_impl, attrs={
39-
"url":
40-
attr.string(mandatory=True, doc="S3 URL for the object"),
41+
"bucket":
42+
attr.string(mandatory=True, doc="S3 bucket name"),
43+
"file_path":
44+
attr.string(mandatory=True,
45+
doc="Relative path to the archive file within the bucket"),
46+
"aws_profile":
47+
attr.string(doc="AWS profile to use for authentication"),
4148
"sha256":
4249
attr.string(mandatory=True, doc="SHA256 checksum of the archive"),
4350
"build_file":
4451
attr.label(allow_single_file=True,
45-
doc="BUILD file for the unpacked archive."),
52+
doc="BUILD file for the unpacked archive"),
4653
"strip_prefix":
47-
attr.string(doc="Prefix to strip when archive is unpacked."),
54+
attr.string(doc="Prefix to strip when archive is unpacked"),
4855
})

0 commit comments

Comments
 (0)