Blame SOURCES/virt-manager-urlfetcher-Factor-out-ISOReader-class.patch

daf011
From dae2f3471a56f3967952e6951f60f523060c89a0 Mon Sep 17 00:00:00 2001
daf011
From: Cole Robinson <crobinso@redhat.com>
daf011
Date: Wed, 7 Apr 2021 09:51:41 -0400
daf011
Subject: [PATCH] urlfetcher: Factor out ISOReader class
daf011
daf011
This contains all the isoinfo command logic. This will be used
daf011
to add an xorriso backend as well
daf011
daf011
Signed-off-by: Cole Robinson <crobinso@redhat.com>
daf011
(cherry picked from commit b13b5e0f5edf8efabae643d28f12693f43f094db)
daf011
daf011
Resolves: https://bugzilla.redhat.com/show_bug.cgi?id=1973236
daf011
daf011
Signed-off-by: Jonathon Jongsma <jjongsma@redhat.com>
daf011
---
daf011
 virtinst/install/urlfetcher.py | 64 +++++++++++++++++++++++++---------
daf011
 1 file changed, 48 insertions(+), 16 deletions(-)
daf011
daf011
diff --git a/virtinst/install/urlfetcher.py b/virtinst/install/urlfetcher.py
daf011
index f531fe50..3cacab1a 100644
daf011
--- a/virtinst/install/urlfetcher.py
daf011
+++ b/virtinst/install/urlfetcher.py
daf011
@@ -18,6 +18,45 @@ import requests
daf011
 from ..logger import log
daf011
 
daf011
 
daf011
+#########################
daf011
+# isoreader abstraction #
daf011
+#########################
daf011
+
daf011
+class _ISOReader:
daf011
+    def __init__(self, location):
daf011
+        self._location = location
daf011
+
daf011
+    def grabFile(self, url):
daf011
+        raise NotImplementedError()
daf011
+    def hasFile(self, url):
daf011
+        raise NotImplementedError()
daf011
+
daf011
+
daf011
+class _ISOinfoReader(_ISOReader):
daf011
+    """
daf011
+    Handle reading reading files off an iso
daf011
+    """
daf011
+    def __init__(self, location):
daf011
+        super().__init__(location)
daf011
+        self._cache_file_list = self._make_file_list()
daf011
+
daf011
+    def _make_file_list(self):
daf011
+        cmd = ["isoinfo", "-J", "-i", self._location, "-f"]
daf011
+
daf011
+        log.debug("Running isoinfo: %s", cmd)
daf011
+        output = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
daf011
+        return output.splitlines(False)
daf011
+
daf011
+    def grabFile(self, url):
daf011
+        cmd = ["isoinfo", "-J", "-i", self._location, "-x", url]
daf011
+
daf011
+        log.debug("Running isoinfo: %s", cmd)
daf011
+        return subprocess.check_output(cmd)
daf011
+
daf011
+    def hasFile(self, url):
daf011
+        return url.encode("ascii") in self._cache_file_list
daf011
+
daf011
+
daf011
 ###########################
daf011
 # Fetcher implementations #
daf011
 ###########################
daf011
@@ -302,39 +341,32 @@ class _LocalURLFetcher(_URLFetcher):
daf011
 
daf011
 
daf011
 class _ISOURLFetcher(_URLFetcher):
daf011
-    _cache_file_list = None
daf011
+    _isoreader = None
daf011
     _is_iso = True
daf011
 
daf011
     def _make_full_url(self, filename):
daf011
         return os.path.join("/", filename)
daf011
 
daf011
+    def _get_isoreader(self):
daf011
+        if not self._isoreader:
daf011
+            self._isoreader = _ISOinfoReader(self.location)
daf011
+        return self._isoreader
daf011
+
daf011
     def _grabber(self, url):
daf011
         """
daf011
         Use isoinfo to grab the file
daf011
         """
daf011
         if not self._hasFile(url):
daf011
-            raise RuntimeError("isoinfo didn't find file=%s" % url)
daf011
-
daf011
-        cmd = ["isoinfo", "-J", "-i", self.location, "-x", url]
daf011
-
daf011
-        log.debug("Running isoinfo: %s", cmd)
daf011
-        output = subprocess.check_output(cmd)
daf011
+            raise RuntimeError("iso doesn't have file=%s" % url)
daf011
 
daf011
+        output = self._get_isoreader().grabFile(url)
daf011
         return io.BytesIO(output), len(output)
daf011
 
daf011
     def _hasFile(self, url):
daf011
         """
daf011
         Use isoinfo to list and search for the file
daf011
         """
daf011
-        if not self._cache_file_list:
daf011
-            cmd = ["isoinfo", "-J", "-i", self.location, "-f"]
daf011
-
daf011
-            log.debug("Running isoinfo: %s", cmd)
daf011
-            output = subprocess.check_output(cmd, stderr=subprocess.DEVNULL)
daf011
-
daf011
-            self._cache_file_list = output.splitlines(False)
daf011
-
daf011
-        return url.encode("ascii") in self._cache_file_list
daf011
+        return self._get_isoreader().hasFile(url)
daf011
 
daf011
 
daf011
 class DirectFetcher(_URLFetcher):
daf011
-- 
daf011
2.31.1
daf011