From 4ed9026d0feeebf97b319f17f306dd63bb3f7e36 Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Tue, 24 Sep 2024 09:12:09 +0200 Subject: [PATCH] De-duplicate two opinionated doChroot() calls See #1456. --- mock/py/mockbuild/buildroot.py | 18 ++++++++++++++++++ mock/py/mockbuild/plugins/rpkg_preprocessor.py | 16 +--------------- mock/py/mockbuild/plugins/rpmautospec.py | 12 +----------- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/mock/py/mockbuild/buildroot.py b/mock/py/mockbuild/buildroot.py index 6d260b287..9f596cd8b 100644 --- a/mock/py/mockbuild/buildroot.py +++ b/mock/py/mockbuild/buildroot.py @@ -426,6 +426,24 @@ def doChroot(self, command, nosync=False, *args, **kargs): self.uid_manager.restorePrivs() return result + def doChrootPlugin(self, command, cwd=None): + """ + Execute command (specified as array, not a shell command string) in this + buildroot in `cwd`, as a non-privileged user. Used by plugins. + """ + private_network = not self.config.get('rpmbuild_networking', False) + self.doChroot( + command, + shell=False, + cwd=cwd, + logger=self.build_log, + uid=self.chrootuid, + gid=self.chrootgid, + user=self.chrootuser, + unshare_net=private_network, + printOutput=self.config.get('print_main_output', True) + ) + def all_chroot_packages(self): """package set, result of rpm -qa in the buildroot""" self.nuke_rpm_db() diff --git a/mock/py/mockbuild/plugins/rpkg_preprocessor.py b/mock/py/mockbuild/plugins/rpkg_preprocessor.py index 878a08775..ae7eff53a 100644 --- a/mock/py/mockbuild/plugins/rpkg_preprocessor.py +++ b/mock/py/mockbuild/plugins/rpkg_preprocessor.py @@ -104,18 +104,4 @@ def _preprocess(self, host_chroot_spec, host_chroot_sources): command_str = self.opts.get('cmd') % {'source_spec': chroot_sources_spec, 'target_spec': chroot_spec} command = shlex.split(command_str) - - # determine whether to use private network or not based on rpmbuild_networking - private_network = (not self.config.get('rpmbuild_networking', False)) - - self.buildroot.doChroot( - command, - shell=False, - cwd=chroot_sources, - logger=self.buildroot.build_log, - uid=self.buildroot.chrootuid, - gid=self.buildroot.chrootgid, - user=self.buildroot.chrootuser, - unshare_net=private_network, - printOutput=self.config.get('print_main_output', True) - ) + self.buildroot.doChrootPlugin(command, cwd=chroot_sources) diff --git a/mock/py/mockbuild/plugins/rpmautospec.py b/mock/py/mockbuild/plugins/rpmautospec.py index 3d8fc195f..fd501e9de 100644 --- a/mock/py/mockbuild/plugins/rpmautospec.py +++ b/mock/py/mockbuild/plugins/rpmautospec.py @@ -114,14 +114,4 @@ def attempt_process_distgit( # bonus, spec files will be processed in the environment they will be # built for, reducing the impact of the host system on the outcome, # leading to more deterministic results and better repeatable builds. - self.buildroot.doChroot( - command, - shell=False, - cwd=chroot_sources, - logger=self.buildroot.build_log, - uid=self.buildroot.chrootuid, - gid=self.buildroot.chrootgid, - user=self.buildroot.chrootuser, - unshare_net=not self.config.get("rpmbuild_networking", False), - printOutput=self.config.get("print_main_output", True), - ) + self.buildroot.doChrootPlugin(command, cwd=chroot_sources)