Skip to content

Commit

Permalink
buildifier: move out of array addition
Browse files Browse the repository at this point in the history
When mutating arrays the linter prefers to use extend/append instead of
+= to the existing array.

Also moving from arrays to ctx.actions.attr instead of arrays when
passing values into ctx.actions, this reduces the memory preassure during
the bazel analysis phase and delays it until the action graph gets
executed.
  • Loading branch information
manuelnaranjo committed Jul 17, 2024
1 parent 65d147c commit 0f67c8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
13 changes: 6 additions & 7 deletions internal/bazeldnf.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ def _bazeldnf_impl(ctx):
out_file = ctx.actions.declare_file(ctx.label.name + ".bash")
args = []
if ctx.attr.command:
args += [ctx.attr.command]
args.append(ctx.attr.command)
if ctx.attr.rulename:
args += ["--name", ctx.attr.rulename]
args.extend(["--name", ctx.attr.rulename])
if ctx.attr.rpmtree:
args += ["--rpmtree", ctx.attr.rpmtree]
args.extend(["--rpmtree", ctx.attr.rpmtree])
if ctx.file.tar:
args += ["--input", ctx.file.tar.path]
transitive_dependencies += [ctx.attr.tar.files]
for lib in ctx.attr.libs:
args += [lib]
args.extend(["--input", ctx.file.tar.path])
transitive_dependencies.extend(ctx.attr.tar.files)
args.extend(ctx.attr.libs)

toolchain = ctx.toolchains[BAZELDNF_TOOLCHAIN]

Expand Down
37 changes: 19 additions & 18 deletions internal/rpmtree.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,55 +22,56 @@ make available to bazel
load("//bazeldnf:toolchain.bzl", "BAZELDNF_TOOLCHAIN")

def _rpm2tar_impl(ctx):
rpms = []
for rpm in ctx.files.rpms:
rpms += ["--input", rpm.path]
args = ctx.actions.args()

out = ctx.outputs.out
args = ["rpm2tar", "--output", out.path]
args.add_all(["rpm2tar", "--output", out])

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

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

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)]
selinux_labels.append(k + "=" + v)
args.add_joined("--selinux-labels", selinux_labels, join_with = ",")

args += rpms
for rpm in ctx.files.rpms:
args.add_all(["--input", rpm.path])

ctx.actions.run(
inputs = ctx.files.rpms,
outputs = [out],
arguments = args,
arguments = [args],
mnemonic = "Rpm2Tar",
progress_message = "Converting %s to tar" % ctx.label.name,
executable = ctx.toolchains[BAZELDNF_TOOLCHAIN]._tool,
)

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

def _expand_path(files):
return [x.path for x in files]

def _tar2files_impl(ctx):
out = ctx.outputs.out
files = []
for out in ctx.outputs.out:
files += [out.path]
args = ctx.actions.args()

args.add_all(["tar2files", "--file-prefix", ctx.attr.prefix, "--input", ctx.files.tar[0]])
args.add_all([ctx.outputs.out], map_each = _expand_path)

args = ["tar2files", "--file-prefix", ctx.attr.prefix, "--input", ctx.files.tar[0].path] + files
ctx.actions.run(
inputs = ctx.files.tar,
outputs = ctx.outputs.out,
arguments = args,
arguments = [args],
mnemonic = "Tar2Files",
progress_message = "Extracting files",
executable = ctx.toolchains[BAZELDNF_TOOLCHAIN]._tool,
Expand Down

0 comments on commit 0f67c8d

Please sign in to comment.