From 73220678da4aff9e7605d425deb3dbbe6fcd5d79 Mon Sep 17 00:00:00 2001 From: Tomasz Wojno Date: Wed, 20 Dec 2023 17:36:00 +0000 Subject: [PATCH] Add %posttrans scriptlet to RPM package --- pkg/make_rpm.py | 9 +++++++++ pkg/rpm/template.spec.tpl | 4 +++- pkg/rpm_pfg.bzl | 22 ++++++++++++++++++++++ tests/rpm/BUILD | 4 ++++ tests/rpm/pkg_rpm_basic_test.py | 2 ++ tests/rpm/template-test.spec.tpl | 2 ++ 6 files changed, 42 insertions(+), 1 deletion(-) diff --git a/pkg/make_rpm.py b/pkg/make_rpm.py index 4ccd750b..dd30a490 100644 --- a/pkg/make_rpm.py +++ b/pkg/make_rpm.py @@ -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.""" @@ -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`. @@ -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': "" } @@ -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): @@ -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) @@ -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') @@ -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: diff --git a/pkg/rpm/template.spec.tpl b/pkg/rpm/template.spec.tpl index 36bd5dc4..ba25db8a 100644 --- a/pkg/rpm/template.spec.tpl +++ b/pkg/rpm/template.spec.tpl @@ -19,4 +19,6 @@ ${PREUN_SCRIPTLET} ${POSTUN_SCRIPTLET} -${CHANGELOG} \ No newline at end of file +${POSTTRANS_SCRIPTLET} + +${CHANGELOG} diff --git a/pkg/rpm_pfg.bzl b/pkg/rpm_pfg.bzl index 756482b3..29010eca 100644 --- a/pkg/rpm_pfg.bzl +++ b/pkg/rpm_pfg.bzl @@ -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) @@ -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. diff --git a/tests/rpm/BUILD b/tests/rpm/BUILD index b1dd70bb..293ab8dc 100644 --- a/tests/rpm/BUILD +++ b/tests/rpm/BUILD @@ -140,6 +140,7 @@ pkg_rpm( postun_scriptlet = """echo postun""", pre_scriptlet = """echo pre""", preun_scriptlet = """echo preun""", + posttrans_scriptlet = """echo posttrans""", provides = ["test"], release = "2222", requires = ["test-lib > 1.0"], @@ -164,6 +165,7 @@ pkg_rpm( postun_scriptlet = """echo postun""", pre_scriptlet = """echo pre""", preun_scriptlet = """echo preun""", + posttrans_scriptlet = """echo posttrans""", provides = ["test"], release = "2222", requires = ["test-lib > 1.0"], @@ -191,6 +193,7 @@ pkg_rpm( postun_scriptlet = """echo postun""", pre_scriptlet = """echo pre""", preun_scriptlet = """echo preun""", + posttrans_scriptlet = """echo posttrans""", provides = ["test"], release = "2222", requires = ["test-lib > 1.0"], @@ -285,6 +288,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' diff --git a/tests/rpm/pkg_rpm_basic_test.py b/tests/rpm/pkg_rpm_basic_test.py index 218103ad..d2c95036 100644 --- a/tests/rpm/pkg_rpm_basic_test.py +++ b/tests/rpm/pkg_rpm_basic_test.py @@ -60,6 +60,8 @@ 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( diff --git a/tests/rpm/template-test.spec.tpl b/tests/rpm/template-test.spec.tpl index f771f379..3f975f90 100644 --- a/tests/rpm/template-test.spec.tpl +++ b/tests/rpm/template-test.spec.tpl @@ -43,3 +43,5 @@ ${POST_SCRIPTLET} ${PREUN_SCRIPTLET} ${POSTUN_SCRIPTLET} + +${POSTTRANS_SCRIPTLET}