From 12e13bf333880429df4a7df949fc412522ff997b Mon Sep 17 00:00:00 2001 From: Pavel Raiskup Date: Dec 22 2021 15:50:53 +0000 Subject: mockbuild: allow enforcing local mock config in fedpkg The default automatic decision based on the (non-)existing local Mock configuration causes misleading user failures for Fedora EPEL 8: a) The new mock-core-configs package dropped the 'epel-8-*' configs: https://rpm-software-management.github.io/mock/Release-Notes-2.16 b) We fallback to downloading epel-8* configs from Kojihub, but this is useless because the Mock configuration in Koji uses some Koji-private RHEL repourls that can not be used to download packages on users' machines. c) That causes 403 repo download errors; hiding the default Mock's error message for absent epel-8 configuration. For Fedora packages, we can actually afford defaulting to Mock's configuration, _always_. For other packagers (e.g. RHEL) we probably can not because the related Mock config files aren't normally installed with mock-core-configs (so while the local config is still preferred, we usually have to fall-back to the downloaded config). So this commit provides a new protected method Commands.use_local_mock_config() that can be safely overridden in Fedpkg which will default to "local config". While the other pyrpkg clones may keep the _default_ detection mechanism (== do nothing), or override the method as well. While on it, two new options are added, '--use-koji-mock-config' and '--use-local-mock-config' that can be used to override the default specified by any pyrpkg fork. By default, with both patched pyrpkg and fedpkg, mock's help output is printed out: $ fedpkg mockbuild ... ERROR: Could not find required config file: /etc/mock/epel-8-x86_64.cfg ERROR: There are those alternatives: ... See also: https://pagure.io/rpkg/pull-request/597 https://pagure.io/fedpkg/pull-request/461 Signed-off-by: Pavel Raiskup --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index c153f16..32114d3 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -2800,7 +2800,32 @@ class Commands(object): raise rpkgError('Failed to remove temporary directory' ' %s. Reason: %s.' % (tmp_dir, error)) - def mockbuild(self, mockargs=[], root=None, hashtype=None, shell=None): + @staticmethod + def use_local_mock_config(root, force_local): + """ + Decide if local mock configuration should be used, based on Mock root + name and command-line arguments. + + :param str root: mock chroot, the -r argument + :param bool force_local: enforce download of the Mock configuration + from Kojihub (when False), or enforce local Mock config (when + True). If None, local configuration is used if the config file + exists. + :return: bool, True for local config, False for downloaded config. + """ + if force_local is not None: + return force_local + + chroot_cfg = '/etc/mock/%s.cfg' % root + home_chroot_cfg = '~/.config/mock/%s.cfg' % root + home_chroot_cfg = os.path.expanduser(home_chroot_cfg) + if os.path.exists(chroot_cfg) or os.path.exists(home_chroot_cfg): + return True + return False + + + def mockbuild(self, mockargs=[], root=None, hashtype=None, shell=None, + force_local_mock_config=None): """Build the package in mock, using mockargs Log the output and returns nothing @@ -2812,6 +2837,9 @@ class Commands(object): :param str hashtype: used to generate SRPM only if there is no SRPM generated before. :param bool shell: indicate whether to go into chroot. + :param bool koji_config: enforce download of the Mock configuration + from Kojihub (True), or enforce local Mock config (False). If + None local configuration is used if present. .. versionadded:: 1.56 Parameter shell. @@ -2829,12 +2857,8 @@ class Commands(object): config_dir = None if not root: root = self.mockconfig - chroot_cfg = '/etc/mock/%s.cfg' % root - home_chroot_cfg = '~/.config/mock/%s.cfg' % root - home_chroot_cfg = os.path.expanduser(home_chroot_cfg) - if not os.path.exists(chroot_cfg) and not os.path.exists(home_chroot_cfg): - self.log.debug('Mock config %s was not found. Going to' - ' request koji to create new one.', chroot_cfg) + if not self.use_local_mock_config(root, force_local_mock_config): + self.log.debug('Going to download Mock config from Kojihub') try: config_dir = self._config_dir_basic(root=root) except rpkgError as error: diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py index 63d25d5..aa0feba 100644 --- a/pyrpkg/cli.py +++ b/pyrpkg/cli.py @@ -1118,6 +1118,17 @@ class cliClient(object): "extra_args", default=None, nargs=argparse.REMAINDER, help="Custom arguments that are passed to the 'mock'. " "Use '--' to separate them from other arguments.") + mock_config_group = mockbuild_parser.add_mutually_exclusive_group() + mock_config_group.add_argument( + '--use-koji-mock-config', default=None, dest="local_mock_config", + action='store_false', + help="Download Mock configuration from Kojihub, instead of using " + "the local Mock configuration in mock-core-configs.rpm.") + mock_config_group.add_argument( + '--use-local-mock-config', default=None, dest="local_mock_config", + action='store_true', + help="Enforce use of local Mock configuration.") + mockbuild_parser.set_defaults(command=self.mockbuild) def register_mock_config(self): @@ -2308,9 +2319,10 @@ class cliClient(object): # there were no args pass try: - self.cmd.mockbuild(mockargs, self.args.root, - hashtype=self.args.hash, - shell=self.args.shell) + self.cmd.mockbuild( + mockargs, self.args.root, hashtype=self.args.hash, + shell=self.args.shell, + force_local_mock_config=self.args.local_mock_config) except Exception as e: raise rpkgError(e)