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