From d5be51eec99108c3809551b615064d0c5cbe628a Mon Sep 17 00:00:00 2001 From: Ondrej Nosek Date: Tue, 28 Mar 2023 19:58:06 +0200 Subject: [PATCH 10/12] `pre-push-check` have to use spectool with --define To get all defined source files and patches from the specfile, the 'spectool' utility needs '--define' argument(s) to set specific paths for the repository. JIRA: RHELCMP-11466 Fixes: #672 Signed-off-by: Ondrej Nosek --- pyrpkg/__init__.py | 57 +++++++++++++++------------ tests/commands/test_pre_push_check.py | 3 +- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/pyrpkg/__init__.py b/pyrpkg/__init__.py index 7a3c9c6..584c141 100644 --- a/pyrpkg/__init__.py +++ b/pyrpkg/__init__.py @@ -4442,30 +4442,41 @@ class Commands(object): sys.exit(1) try: + clone_dir = tempfile.mkdtemp(prefix="pre_push_hook_") + for cmd in [ + ('git', 'clone', self.path, clone_dir), + ('git', 'checkout', ref), + ]: + ret, _, _ = self._run_command(cmd, cwd=clone_dir, + # suppress unwanted printing of command line messages + return_stdout=True, return_stderr=True) + if ret != 0: + self.log.error('Command \'{0}\' failed. Push operation ' + 'was cancelled.'.format(' '.join(cmd))) + self.log.warning(show_hint) + sys.exit(2) + + # get all source files from the specfile (including patches) # Assume, that specfile names are same in the active branch # and in the pushed branch (git checkout f37 && git push origin rawhide) # in this case 'f37' is active branch and 'rawhide' is pushed branch. specfile_path_absolute = os.path.join(self.layout.specdir, self.spec) # convert to relative path specfile_path = os.path.relpath(specfile_path_absolute, start=self.path) - spec_content = self.repo.git.cat_file("-p", "{0}:{1}".format(ref, specfile_path)) - except Exception: - # It might be the case of an empty commit - self.log.warning('Specfile doesn\'t exist. Push operation continues.') - return - - # load specfile content from pushed branch and save it into a temporary file - with tempfile.NamedTemporaryFile(mode="w+") as temporary_spec: - temporary_spec.write(spec_content) - temporary_spec.flush() - # get all source files from the specfile (including patches) - cmd = ('spectool', '-l', temporary_spec.name) - ret, stdout, _ = self._run_command(cmd, return_text=True, return_stdout=True) + cmd = ['spectool', '-l', os.path.join(clone_dir, specfile_path)] + # extract just '--define' arguments from rpmdefines + for opt, val in zip(self.rpmdefines[0::2], self.rpmdefines[1::2]): + if opt == '--define': + cmd.extend((opt, val)) + ret, stdout, _ = self._run_command(cmd, cwd=clone_dir, + return_text=True, return_stdout=True) if ret != 0: self.log.error('Command \'{0}\' failed. Push operation ' 'was cancelled.'.format(' '.join(cmd))) self.log.warning(show_hint) - sys.exit(2) + sys.exit(3) + finally: + self._cleanup_tmp_dir(clone_dir) source_files = [] # extract source files from the spectool's output @@ -4490,22 +4501,16 @@ class Commands(object): sources_file_path_absolute = self.sources_filename # convert to relative path sources_file_path = os.path.relpath(sources_file_path_absolute, start=self.path) - sources_file_content = self.repo.git.cat_file( - '-p', '{0}:{1}'.format(ref, sources_file_path)) + + # parse 'sources' files content + sourcesf = SourcesFile(sources_file_path, self.source_entry_type) + sourcesf_entries = set(item.file for item in sourcesf.entries) except Exception: self.log.warning('\'sources\' file doesn\'t exist. Push operation continues.') # NOTE: check doesn't fail when 'sources' file doesn't exist. Just skips the rest. # it might be the case of the push without 'sources' = retiring the repository return - # load 'sources' file content from pushed branch and save it into a temporary file - with tempfile.NamedTemporaryFile(mode="w+") as temporary_sources_file: - temporary_sources_file.write(sources_file_content) - temporary_sources_file.flush() - # parse 'sources' files content - sourcesf = SourcesFile(temporary_sources_file.name, self.source_entry_type) - sourcesf_entries = set(item.file for item in sourcesf.entries) - # list of all files (their relative paths) in the commit repo_entries = set(item.path for item in commit.tree.traverse() if item.type != "tree") @@ -4518,7 +4523,7 @@ class Commands(object): 'nor tracked in git. ' 'Push operation was cancelled'.format(source_file)) self.log.warning(show_hint) - sys.exit(3) + sys.exit(4) # verify all file entries in 'sources' were uploaded to the lookaside cache for entry in sourcesf.entries: @@ -4532,6 +4537,6 @@ class Commands(object): self.log.error('Source file (or tarball) \'{}\' wasn\'t uploaded to the lookaside ' 'cache. Push operation was cancelled.'.format(filename)) self.log.warning(show_hint) - sys.exit(4) + sys.exit(5) return 0 # The push operation continues diff --git a/tests/commands/test_pre_push_check.py b/tests/commands/test_pre_push_check.py index 5e314b9..ee151c1 100644 --- a/tests/commands/test_pre_push_check.py +++ b/tests/commands/test_pre_push_check.py @@ -37,6 +37,7 @@ class TestPrePushCheck(CommandTestCase): def setUp(self): super(TestPrePushCheck, self).setUp() + self.dist = "rhel-8" self.make_new_git(self.module) moduledir = os.path.join(self.gitroot, self.module) @@ -87,7 +88,7 @@ Patch3: d.patch with self.assertRaises(SystemExit) as exc: self.cmd.pre_push_check("HEAD") - self.assertEqual(exc.exception.code, 3) + self.assertEqual(exc.exception.code, 4) log_error.assert_called_once_with("Source file 'b.patch' was neither listed in the " "'sources' file nor tracked in git. Push operation " "was cancelled") -- 2.39.2