Skip to content

Commit

Permalink
Add xattrs rule
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Mohr <[email protected]>
  • Loading branch information
rmohr committed Aug 6, 2021
1 parent 62262a5 commit 9ac9982
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ load(
"@bazeldnf//internal:rpmtree.bzl",
_tar2files = "tar2files",
)
load(
"@bazeldnf//internal:xattrs.bzl",
_xattrs = "xattrs",
)

rpm = _rpm
rpmtree = _rpmtree
tar2files = _tar2files
xattrs = _xattrs

def bazeldnf_dependencies():
_maybe(
Expand Down
65 changes: 65 additions & 0 deletions internal/xattrs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Copyright 2014 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def _xattrs_impl(ctx):
out = ctx.outputs.out
args = ["xattr", "-i", ctx.files.tar[0].path, "-o", out.path]

if ctx.attr.capabilities:
capabilities = []
for k, v in ctx.attr.capabilities.items():
capabilities += [k + "=" + ":".join(v)]
args += ["--capabilities", ",".join(capabilities)]

if ctx.attr.selinux_labels:
selinux_labels = []
for k, v in ctx.attr.selinux_labels.items():
selinux_labels += [k + "=" + v]
args += ["--selinux-labels", ",".join(selinux_labels)]

ctx.actions.run(
inputs = ctx.files.tar,
outputs = [out],
arguments = args,
progress_message = "Enriching %s with xattrs" % ctx.label.name,
executable = ctx.executable._bazeldnf,
)

return [DefaultInfo(files = depset([ctx.outputs.out]))]

_xattrs_attrs = {
"tar": attr.label(allow_single_file = True),
"_bazeldnf": attr.label(
executable = True,
cfg = "exec",
allow_files = True,
default = Label("//cmd:cmd"),
),
"capabilities": attr.string_list_dict(),
"selinux_labels": attr.string_dict(),
"out": attr.output(mandatory = True),
}

_xattrs = rule(
implementation = _xattrs_impl,
attrs = _xattrs_attrs,
)

def xattrs(**kwargs):
basename = kwargs["name"]
tarname = basename + ".tar"
_xattrs(
out = tarname,
**kwargs
)

0 comments on commit 9ac9982

Please sign in to comment.