Skip to content

Commit 9ac9982

Browse files
committed
Add xattrs rule
Signed-off-by: Roman Mohr <[email protected]>
1 parent 62262a5 commit 9ac9982

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

deps.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@ load(
1414
"@bazeldnf//internal:rpmtree.bzl",
1515
_tar2files = "tar2files",
1616
)
17+
load(
18+
"@bazeldnf//internal:xattrs.bzl",
19+
_xattrs = "xattrs",
20+
)
1721

1822
rpm = _rpm
1923
rpmtree = _rpmtree
2024
tar2files = _tar2files
25+
xattrs = _xattrs
2126

2227
def bazeldnf_dependencies():
2328
_maybe(

internal/xattrs.bzl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Copyright 2014 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
def _xattrs_impl(ctx):
16+
out = ctx.outputs.out
17+
args = ["xattr", "-i", ctx.files.tar[0].path, "-o", out.path]
18+
19+
if ctx.attr.capabilities:
20+
capabilities = []
21+
for k, v in ctx.attr.capabilities.items():
22+
capabilities += [k + "=" + ":".join(v)]
23+
args += ["--capabilities", ",".join(capabilities)]
24+
25+
if ctx.attr.selinux_labels:
26+
selinux_labels = []
27+
for k, v in ctx.attr.selinux_labels.items():
28+
selinux_labels += [k + "=" + v]
29+
args += ["--selinux-labels", ",".join(selinux_labels)]
30+
31+
ctx.actions.run(
32+
inputs = ctx.files.tar,
33+
outputs = [out],
34+
arguments = args,
35+
progress_message = "Enriching %s with xattrs" % ctx.label.name,
36+
executable = ctx.executable._bazeldnf,
37+
)
38+
39+
return [DefaultInfo(files = depset([ctx.outputs.out]))]
40+
41+
_xattrs_attrs = {
42+
"tar": attr.label(allow_single_file = True),
43+
"_bazeldnf": attr.label(
44+
executable = True,
45+
cfg = "exec",
46+
allow_files = True,
47+
default = Label("//cmd:cmd"),
48+
),
49+
"capabilities": attr.string_list_dict(),
50+
"selinux_labels": attr.string_dict(),
51+
"out": attr.output(mandatory = True),
52+
}
53+
54+
_xattrs = rule(
55+
implementation = _xattrs_impl,
56+
attrs = _xattrs_attrs,
57+
)
58+
59+
def xattrs(**kwargs):
60+
basename = kwargs["name"]
61+
tarname = basename + ".tar"
62+
_xattrs(
63+
out = tarname,
64+
**kwargs
65+
)

0 commit comments

Comments
 (0)