|
| 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