From 679343353a271f69d9f37629cbeb7cb6bcb96fc7 Mon Sep 17 00:00:00 2001 From: Ondrej Nosek Date: Jan 19 2022 01:18:40 +0000 Subject: 'lint' can run with different 'rpmlint' versions 'lint' subcommand internally executes 'rpmlint' binary. With 'rpmlint' package version 2.x it changed some of its input arguments. Rpkg has to recognize the installed 'rpmlint' version and use the corresponding switch/argument. Specifically, '-f' switch for user config file has changed to '-r' in the newer version. Additionally, fixes some paths to source RPMs. JIRA: RHELCMP-7842 Resolves: rhbz#1967821 Signed-off-by: Ondrej Nosek --- diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 879a42e..3bc1829 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -1296,7 +1296,7 @@ class Commands(object): def _get_build_arches_from_spec(self): """Given the path to an spec, retrieve the build arches""" - spec = os.path.join(self.path, self.spec) + spec = os.path.join(self.layout.specdir, self.spec) try: hdr = rpm.spec(spec) except Exception: @@ -2649,7 +2649,7 @@ class Commands(object): mockdir = os.path.join( "results_%s" % self.repo_name, self.ver, self.rel) - if (os.path.exists(mockdir)): + if os.path.exists(mockdir): log.info("Mockbuild results directory found. " "Linting mockbuild results.") rpm_globs.add(os.path.join(mockdir, '*.rpm')) @@ -2682,13 +2682,26 @@ class Commands(object): log.warning('No rpm found') cmd = ['rpmlint'] + + user_config_file_switch = '-f' # switch that uses rpmlint version 1.x + # get rpmlint version installed in the system + ret, stdout, _ = self._run_command(cmd + ['--version'], + return_stdout=True, + return_text=True) + if ret == 0: + res = re.search(r'(\d)\.\d', stdout) + if res is not None: + if res.groups()[0] == '2': + user_config_file_switch = '-r' + self.log.debug('rpmlint version 2.x detected') + default_rpmlintconf = '{0}.rpmlintrc'.format(self.repo_name) if info: cmd.extend(['-i']) if rpmlintconf: - cmd.extend(["-f", os.path.join(self.path, rpmlintconf)]) + cmd.extend([user_config_file_switch, os.path.join(self.path, rpmlintconf)]) elif os.path.isfile(os.path.join(self.path, default_rpmlintconf)): - cmd.extend(["-f", os.path.join(self.path, default_rpmlintconf)]) + cmd.extend([user_config_file_switch, os.path.join(self.path, default_rpmlintconf)]) elif os.path.isfile(os.path.join(self.path, ".rpmlint")): self.log.warning('.rpmlint file usage as default rpmlint configuration is deprecated ' 'Use {0} instead'.format(default_rpmlintconf)) diff --git a/tests/test_cli.py b/tests/test_cli.py index 133380b..e7545bf 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1055,13 +1055,16 @@ class TestLint(CliTestCase): self.checkout_branch(git.Repo(self.cloned_repo_path), 'eng-rhel-7') cli_cmd = ['rpkg', '--name', 'docpkg', '--path', self.cloned_repo_path, 'lint'] + _run_command.return_value = [0, "version X.X", None] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() cli.lint() - rpmlint = ['rpmlint', os.path.join(cli.cmd.path, cli.cmd.spec)] - _run_command.assert_called_once_with(rpmlint, shell=True) + _run_command.assert_has_calls([ + call(['rpmlint', '--version'], return_stdout=True, return_text=True), + call(['rpmlint', os.path.join(cli.cmd.path, cli.cmd.spec)], shell=True), + ]) @patch('pyrpkg.Commands._run_command') def test_lint_warning_with_info(self, _run_command): @@ -1069,13 +1072,16 @@ class TestLint(CliTestCase): cli_cmd = ['rpkg', '--name', 'docpkg', '--path', self.cloned_repo_path, 'lint', '--info'] + _run_command.return_value = [0, "version X.X", None] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() cli.lint() - rpmlint = ['rpmlint', '-i', os.path.join(cli.cmd.path, cli.cmd.spec)] - _run_command.assert_called_once_with(rpmlint, shell=True) + _run_command.assert_has_calls([ + call(['rpmlint', '--version'], return_stdout=True, return_text=True), + call(['rpmlint', '-i', os.path.join(cli.cmd.path, cli.cmd.spec)], shell=True), + ]) @patch('pyrpkg.Commands._run_command') def test_lint_with_default_config_file(self, _run_command): @@ -1085,13 +1091,37 @@ class TestLint(CliTestCase): open(lint_config_path, 'a').close() cli_cmd = ['rpkg', '--name', 'docpkg', '--path', self.cloned_repo_path, 'lint'] + _run_command.return_value = [0, "version 1.1", None] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() cli.lint() - rpmlint = ['rpmlint', '-f', lint_config_path, os.path.join(cli.cmd.path, cli.cmd.spec)] - _run_command.assert_called_once_with(rpmlint, shell=True) + _run_command.assert_has_calls([ + call(['rpmlint', '--version'], return_stdout=True, return_text=True), + call(['rpmlint', '-f', lint_config_path, os.path.join(cli.cmd.path, cli.cmd.spec)], + shell=True), + ]) + + @patch('pyrpkg.Commands._run_command') + def test_lint_with_default_config_file_newer_lint_package(self, _run_command): + self.checkout_branch(git.Repo(self.cloned_repo_path), 'eng-rhel-7') + + lint_config_path = os.path.join(self.cloned_repo_path, 'docpkg.rpmlintrc') + open(lint_config_path, 'a').close() + + cli_cmd = ['rpkg', '--name', 'docpkg', '--path', self.cloned_repo_path, 'lint'] + _run_command.return_value = [0, "version 2.0", None] + + with patch('sys.argv', new=cli_cmd): + cli = self.new_cli() + cli.lint() + + _run_command.assert_has_calls([ + call(['rpmlint', '--version'], return_stdout=True, return_text=True), + call(['rpmlint', '-r', lint_config_path, os.path.join(cli.cmd.path, cli.cmd.spec)], + shell=True), + ]) @patch('pyrpkg.Commands._run_command') def test_lint_with_default_and_deprecated_config_files(self, _run_command): @@ -1103,13 +1133,17 @@ class TestLint(CliTestCase): open(deprecated_lint_config_path, 'a').close() cli_cmd = ['rpkg', '--name', 'docpkg', '--path', self.cloned_repo_path, 'lint'] + _run_command.return_value = [0, "version 1.1", None] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() cli.lint() - rpmlint = ['rpmlint', '-f', lint_config_path, os.path.join(cli.cmd.path, cli.cmd.spec)] - _run_command.assert_called_once_with(rpmlint, shell=True) + _run_command.assert_has_calls([ + call(['rpmlint', '--version'], return_stdout=True, return_text=True), + call(['rpmlint', '-f', lint_config_path, os.path.join(cli.cmd.path, cli.cmd.spec)], + shell=True), + ]) @patch('pyrpkg.Commands._run_command') def test_lint_with_custom_config_file(self, _run_command): @@ -1122,14 +1156,18 @@ class TestLint(CliTestCase): cli_cmd = ['rpkg', '--name', 'docpkg', '--path', self.cloned_repo_path, 'lint', '--rpmlintconf', 'custom.rpmlintrc'] + _run_command.return_value = [0, "version 1.1", None] with patch('sys.argv', new=cli_cmd): cli = self.new_cli() cli.lint() - rpmlint = ['rpmlint', '-f', custom_lint_config_path, os.path.join(cli.cmd.path, - cli.cmd.spec)] - _run_command.assert_called_once_with(rpmlint, shell=True) + _run_command.assert_has_calls([ + call(['rpmlint', '--version'], return_stdout=True, return_text=True), + call(['rpmlint', '-f', custom_lint_config_path, + os.path.join(cli.cmd.path, cli.cmd.spec)], + shell=True), + ]) class TestGitUrl(CliTestCase): diff --git a/tests/test_commands.py b/tests/test_commands.py index e3ab001..e984db0 100644 --- a/tests/test_commands.py +++ b/tests/test_commands.py @@ -1004,12 +1004,16 @@ class TestLint(CommandTestCase): glob.side_effect = _mock_glob cmd._get_build_arches_from_spec = Mock( return_value=['x86_64', 'x86_64']) + run.return_value = [0, "version 1.1", None] cmd.lint() self.assertEqual( run.call_args_list, - [call(['rpmlint', + [call(['rpmlint', '--version'], + return_stdout=True, + return_text=True), + call(['rpmlint', os.path.join(cmd.path, 'docpkg.spec'), srpm_path, bin_path, @@ -1040,12 +1044,16 @@ class TestLint(CommandTestCase): exists.side_effect = _mock_exists glob.side_effect = _mock_glob + run.return_value = [0, "version 1.1", None] cmd.lint() self.assertEqual( run.call_args_list, - [call(['rpmlint', + [call(['rpmlint', '--version'], + return_stdout=True, + return_text=True), + call(['rpmlint', os.path.join(cmd.path, 'docpkg.spec'), srpm_path, bin_path, @@ -1072,12 +1080,16 @@ class TestLint(CommandTestCase): exists.side_effect = _mock_exists glob.side_effect = _mock_glob + run.return_value = [0, "version 1.1", None] cmd.lint() self.assertEqual( run.call_args_list, - [call(['rpmlint', + [call(['rpmlint', '--version'], + return_stdout=True, + return_text=True), + call(['rpmlint', os.path.join(cmd.path, 'dockpkg.spec'), srpm_path, bin_path,