Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add %posttrans scriptlet to RPM package #799

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/make_rpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def SetupWorkdir(self,
post_scriptlet_path=None,
preun_scriptlet_path=None,
postun_scriptlet_path=None,
posttrans_scriptlet_path=None,
changelog_file=None,
file_list_path=None):
"""Create the needed structure in the workdir."""
Expand Down Expand Up @@ -264,6 +265,8 @@ def SetupWorkdir(self,
SlurpFile(os.path.join(original_dir, preun_scriptlet_path)) if preun_scriptlet_path is not None else ''
self.postun_scriptlet = \
SlurpFile(os.path.join(original_dir, postun_scriptlet_path)) if postun_scriptlet_path is not None else ''
self.posttrans_scriptlet = \
SlurpFile(os.path.join(original_dir, posttrans_scriptlet_path)) if posttrans_scriptlet_path is not None else ''

# Then prepare for textual substitution. This is typically only the case for the
# experimental `pkg_rpm`.
Expand All @@ -272,6 +275,7 @@ def SetupWorkdir(self,
'POST_SCRIPTLET': ("%post\n" + self.post_scriptlet) if self.post_scriptlet else "",
'PREUN_SCRIPTLET': ("%preun\n" + self.preun_scriptlet) if self.preun_scriptlet else "",
'POSTUN_SCRIPTLET': ("%postun\n" + self.postun_scriptlet) if self.postun_scriptlet else "",
'POSTTRANS_SCRIPTLET': ("%posttrans\n" + self.posttrans_scriptlet) if self.posttrans_scriptlet else "",
'CHANGELOG': ""
}

Expand Down Expand Up @@ -430,6 +434,7 @@ def Build(self, spec_file, out_file,
post_scriptlet_path=None,
preun_scriptlet_path=None,
postun_scriptlet_path=None,
posttrans_scriptlet_path=None,
file_list_path=None,
changelog_file=None,
rpmbuild_args=None):
Expand All @@ -452,6 +457,7 @@ def Build(self, spec_file, out_file,
post_scriptlet_path=post_scriptlet_path,
preun_scriptlet_path=preun_scriptlet_path,
postun_scriptlet_path=postun_scriptlet_path,
posttrans_scriptlet_path=posttrans_scriptlet_path,
changelog_file=changelog_file)
status = self.CallRpmBuild(dirname, rpmbuild_args or [])
self.SaveResult(out_file)
Expand Down Expand Up @@ -501,6 +507,8 @@ def main(argv):
help='File containing the RPM %preun scriptlet, if to be substituted')
parser.add_argument('--postun_scriptlet',
help='File containing the RPM %postun scriptlet, if to be substituted')
parser.add_argument('--posttrans_scriptlet',
help='File containing the RPM %posttrans scriptlet, if to be substituted')
parser.add_argument('--changelog',
help='File containing the RPM changelog text')

Expand All @@ -526,6 +534,7 @@ def main(argv):
post_scriptlet_path=options.post_scriptlet,
preun_scriptlet_path=options.preun_scriptlet,
postun_scriptlet_path=options.postun_scriptlet,
posttrans_scriptlet_path=options.posttrans_scriptlet,
changelog_file=options.changelog,
rpmbuild_args=options.rpmbuild_args)
except NoRpmbuildFoundError:
Expand Down
4 changes: 3 additions & 1 deletion pkg/rpm/template.spec.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ ${PREUN_SCRIPTLET}

${POSTUN_SCRIPTLET}

${CHANGELOG}
${POSTTRANS_SCRIPTLET}

${CHANGELOG}
22 changes: 22 additions & 0 deletions pkg/rpm_pfg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,18 @@ def _pkg_rpm_impl(ctx):
ctx.actions.write(scriptlet_file, ctx.attr.postun_scriptlet)
args.append("--postun_scriptlet=" + scriptlet_file.path)

if ctx.attr.posttrans_scriptlet_file:
if ctx.attr.posttrans_scriptlet:
fail("Both posttrans_scriptlet and posttrans_scriptlet_file attributes were specified")
posttrans_scriptlet_file = ctx.file.posttrans_scriptlet_file
files.append(posttrans_scriptlet_file)
args.append("--posttrans_scriptlet=" + posttrans_scriptlet_file.path)
elif ctx.attr.posttrans_scriptlet:
scriptlet_file = ctx.actions.declare_file(ctx.label.name + ".posttrans_scriptlet")
files.append(scriptlet_file)
ctx.actions.write(scriptlet_file, ctx.attr.posttrans_scriptlet)
args.append("--posttrans_scriptlet=" + scriptlet_file.path)

#### Expand the spec file template; prepare data files

spec_file = ctx.actions.declare_file("%s.spec" % rpm_name)
Expand Down Expand Up @@ -910,6 +922,16 @@ pkg_rpm = rule(
doc = """File containing the RPM `%postun` scriptlet""",
allow_single_file = True,
),
"posttrans_scriptlet": attr.string(
doc = """RPM `%posttrans` scriptlet. Currently only allowed to be a shell script.

`posttrans_scriptlet` and `posttrans_scriptlet_file` are mutually exclusive.
""",
),
"posttrans_scriptlet_file": attr.label(
doc = """File containing the RPM `%posttrans` scriptlet""",
allow_single_file = True,
),
"conflicts": attr.string_list(
doc = """List of capabilities that conflict with this package when it is installed.

Expand Down
78 changes: 66 additions & 12 deletions tests/rpm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,31 @@ pkg_filegroup(
],
)

############################################################################
# scriptlets for testing
############################################################################

_POST_SCRIPTLET = "echo post"
_POSTUN_SCRIPTLET = "echo postun"
_PRE_SCRIPTLET = "echo pre"
_PREUN_SCRIPTLET = "echo preun"
_POSTTRANS_SCRIPTLET = "echo posttrans"

[
genrule(
name = name,
outs = ["{}.sh".format(name)],
cmd = "echo '{}' > $@".format(content),
)
for name, content in [
("post", _POST_SCRIPTLET),
("postun", _POSTUN_SCRIPTLET),
("pre", _PRE_SCRIPTLET),
("preun", _PREUN_SCRIPTLET),
("posttrans", _POSTTRANS_SCRIPTLET),
]
]

############################################################################
# Test RPMs
############################################################################
Expand All @@ -136,10 +161,11 @@ pkg_rpm(
conflicts = ["not-a-test"],
description = """pkg_rpm test rpm description""",
license = "Apache 2.0",
post_scriptlet = """echo post""",
postun_scriptlet = """echo postun""",
pre_scriptlet = """echo pre""",
preun_scriptlet = """echo preun""",
post_scriptlet = _POST_SCRIPTLET,
postun_scriptlet = _POSTUN_SCRIPTLET,
pre_scriptlet = _PRE_SCRIPTLET,
preun_scriptlet = _PREUN_SCRIPTLET,
posttrans_scriptlet = _POSTTRANS_SCRIPTLET,
provides = ["test"],
release = "2222",
requires = ["test-lib > 1.0"],
Expand All @@ -160,10 +186,11 @@ pkg_rpm(
conflicts = ["not-a-test"],
description = """pkg_rpm test rpm description""",
license = "Apache 2.0",
post_scriptlet = """echo post""",
postun_scriptlet = """echo postun""",
pre_scriptlet = """echo pre""",
preun_scriptlet = """echo preun""",
post_scriptlet = _POST_SCRIPTLET,
postun_scriptlet = _POSTUN_SCRIPTLET,
pre_scriptlet = _PRE_SCRIPTLET,
preun_scriptlet = _PREUN_SCRIPTLET,
posttrans_scriptlet = _POSTTRANS_SCRIPTLET,
provides = ["test"],
release = "2222",
requires = ["test-lib > 1.0"],
Expand All @@ -187,10 +214,35 @@ pkg_rpm(
conflicts = ["not-a-test"],
description = """pkg_rpm test rpm description""",
license = "Apache 2.0",
post_scriptlet = """echo post""",
postun_scriptlet = """echo postun""",
pre_scriptlet = """echo pre""",
preun_scriptlet = """echo preun""",
post_scriptlet = _POST_SCRIPTLET,
postun_scriptlet = _POSTUN_SCRIPTLET,
pre_scriptlet = _PRE_SCRIPTLET,
preun_scriptlet = _PREUN_SCRIPTLET,
posttrans_scriptlet = _POSTTRANS_SCRIPTLET,
provides = ["test"],
release = "2222",
requires = ["test-lib > 1.0"],
requires_contextual = {"preun": ["bash"]},
spec_template = "template-test.spec.tpl",
summary = "pkg_rpm test rpm summary",
version = "1.1.1",
)

# Like the first one, except we use files for scriptlets
pkg_rpm(
name = "test_rpm_scriptlets_files",
srcs = [
":test_pfg",
],
architecture = "noarch",
conflicts = ["not-a-test"],
description = """pkg_rpm test rpm description""",
license = "Apache 2.0",
post_scriptlet_file = ":post",
postun_scriptlet_file = ":postun",
pre_scriptlet_file = ":pre",
preun_scriptlet_file = ":preun",
posttrans_scriptlet_file = ":posttrans",
provides = ["test"],
release = "2222",
requires = ["test-lib > 1.0"],
Expand Down Expand Up @@ -285,6 +337,7 @@ genrule(
# NOTE: excludes 'rpmlib' requires that may be version-dependent
echo 'capability:sense'
# Common, automatically generated
echo '/bin/sh:interp,posttrans'
echo '/bin/sh:pre,interp'
echo '/bin/sh:post,interp'
echo '/bin/sh:preun,interp'
Expand All @@ -309,6 +362,7 @@ sh_library(
":test_rpm_direct",
":test_rpm_manifest",
":test_rpm_metadata",
":test_rpm_scriptlets_files",
],
)

Expand Down
11 changes: 7 additions & 4 deletions tests/rpm/pkg_rpm_basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ def setUp(self):
"rules_pkg/tests/rpm/test_rpm_direct-1.1.1-2222.noarch.rpm")
self.test_rpm_bzip2_path = self.runfiles.Rlocation(
"rules_pkg/tests/rpm/test_rpm_bzip2-1.1.1-2222.noarch.rpm")
self.test_rpm_scriptlets_files_path = self.runfiles.Rlocation(
"rules_pkg/tests/rpm/test_rpm_scriptlets_files-1.1.1-2222.noarch.rpm")
self.maxDiff = None

def test_scriptlet_content(self):
Expand All @@ -60,12 +62,13 @@ def test_scriptlet_content(self):
echo preun
postuninstall scriptlet (using /bin/sh):
echo postun
posttrans scriptlet (using /bin/sh):
echo posttrans
"""

output = subprocess.check_output(
["rpm", "-qp", "--scripts", self.test_rpm_path])

self.assertEqual(output, expected)
for path in (self.test_rpm_path, self.test_rpm_scriptlets_files_path):
output = subprocess.check_output(["rpm", "-qp", "--scripts", path])
self.assertEqual(output, expected)

def test_basic_headers(self):
fields = {
Expand Down
2 changes: 2 additions & 0 deletions tests/rpm/template-test.spec.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ ${POST_SCRIPTLET}
${PREUN_SCRIPTLET}

${POSTUN_SCRIPTLET}

${POSTTRANS_SCRIPTLET}