From 41b276ffcdc805bee5d815d7b8c6a00c01d38f51 Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Thu, 31 Jan 2019 13:42:02 -0500 Subject: [PATCH] installertreemedia: Don't carry around _Distro internally Detect the distro, and pull out all the data we need into an internal cache. This will simplify future improvements --- tests/test_urls.py | 15 +++++++++---- virtinst/installertreemedia.py | 39 ++++++++++++++++++++++++---------- virtinst/urldetect.py | 16 +++++--------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/tests/test_urls.py b/tests/test_urls.py index db258a49..7bacfac8 100644 --- a/tests/test_urls.py +++ b/tests/test_urls.py @@ -103,21 +103,28 @@ def _testGuest(testdata, guest): if guest is xenguest: return + # Do this only after the distro detection, since we actually need + # to fetch files for that part + treemedia = installer._treemedia # pylint: disable=protected-access + fetcher = treemedia._cached_fetcher # pylint: disable=protected-access + def fakeAcquireFile(filename): + logging.debug("Fake acquiring %s", filename) + return filename + fetcher.acquireFile = fakeAcquireFile + # Fetch regular kernel - store = installer._treemedia._cached_store - kernel, initrd = store.check_kernel_paths() + kernel, initrd, kernelargs = treemedia.prepare(guest, meter) dummy = initrd if testdata.kernelregex and not re.match(testdata.kernelregex, kernel): raise AssertionError("kernel=%s but testdata.kernelregex='%s'" % (kernel, testdata.kernelregex)) - kernelargs = store.get_kernel_url_arg() if testdata.kernelarg == "None": if bool(kernelargs): raise AssertionError("kernelargs='%s' but testdata.kernelarg='%s'" % (kernelargs, testdata.kernelarg)) elif testdata.kernelarg: - if not kernelargs == testdata.kernelarg: + if testdata.kernelarg != str(kernelargs).split("=")[0]: raise AssertionError("kernelargs='%s' but testdata.kernelarg='%s'" % (kernelargs, testdata.kernelarg)) diff --git a/virtinst/installertreemedia.py b/virtinst/installertreemedia.py index 79bc2f29..7a4ad956 100644 --- a/virtinst/installertreemedia.py +++ b/virtinst/installertreemedia.py @@ -27,6 +27,13 @@ def _is_url(url): url.startswith("ftp://")) +class _LocationData(object): + def __init__(self, os_variant, kernel_pairs, kernel_url_arg): + self.os_variant = os_variant + self.kernel_pairs = kernel_pairs + self.kernel_url_arg = kernel_url_arg + + class InstallerTreeMedia(object): """ Class representing --location Tree media. Can be one of @@ -61,7 +68,7 @@ class InstallerTreeMedia(object): self.initrd_injections = [] self._cached_fetcher = None - self._cached_store = None + self._cached_data = None self._tmpfiles = [] self._tmpvols = [] @@ -98,23 +105,33 @@ class InstallerTreeMedia(object): self._cached_fetcher.meter = meter return self._cached_fetcher - def _get_store(self, guest, fetcher): - if not self._cached_store: - self._cached_store = urldetect.getDistroStore(guest, fetcher) - return self._cached_store + def _get_cached_data(self, guest, fetcher): + if not self._cached_data: + store = urldetect.getDistroStore(guest, fetcher) + self._cached_data = _LocationData( + store.get_osdict_info(), + store.get_kernel_paths(), + store.get_kernel_url_arg()) + return self._cached_data def _prepare_kernel_url(self, guest, fetcher): - store = self._get_store(guest, fetcher) - kernelpath, initrdpath = store.check_kernel_paths() + cache = self._get_cached_data(guest, fetcher) + def _check_kernel_pairs(): + for kpath, ipath in cache.kernel_pairs: + if fetcher.hasFile(kpath) and fetcher.hasFile(ipath): + return kpath, ipath + raise RuntimeError(_("Couldn't find kernel for install tree.")) + + kernelpath, initrdpath = _check_kernel_pairs() kernel = fetcher.acquireFile(kernelpath) self._tmpfiles.append(kernel) initrd = fetcher.acquireFile(initrdpath) self._tmpfiles.append(initrd) args = "" - if not self.location.startswith("/") and store.get_kernel_url_arg(): - args += "%s=%s" % (store.get_kernel_url_arg(), self.location) + if not self.location.startswith("/") and cache.kernel_url_arg: + args += "%s=%s" % (cache.kernel_url_arg, self.location) perform_initrd_injections(initrd, self.initrd_injections, @@ -156,5 +173,5 @@ class InstallerTreeMedia(object): def detect_distro(self, guest): fetcher = self._get_fetcher(guest, None) - store = self._get_store(guest, fetcher) - return store.get_osdict_info() + cache = self._get_cached_data(guest, fetcher) + return cache.os_variant diff --git a/virtinst/urldetect.py b/virtinst/urldetect.py index 216829a6..341985fb 100644 --- a/virtinst/urldetect.py +++ b/virtinst/urldetect.py @@ -292,7 +292,7 @@ def getDistroStore(guest, fetcher): if not sclass.is_valid(cache): continue - store = sclass(fetcher, arch, _type, cache) + store = sclass(fetcher.location, arch, _type, cache) logging.debug("Detected class=%s osvariant=%s", store.__class__.__name__, store.get_osdict_info()) return store @@ -325,11 +325,10 @@ class _DistroTree(object): PRETTY_NAME = None matching_distros = [] - def __init__(self, fetcher, arch, vmtype, cache): - self.fetcher = fetcher + def __init__(self, location, arch, vmtype, cache): self.type = vmtype self.arch = arch - self.uri = fetcher.location + self.uri = location self.cache = cache if self.cache.libosinfo_os_variant: @@ -374,13 +373,8 @@ class _DistroTree(object): def is_valid(cls, cache): raise NotImplementedError - def check_kernel_paths(self): - for kpath, ipath in self._kernel_paths: - if self.fetcher.hasFile(kpath) and self.fetcher.hasFile(ipath): - return kpath, ipath - raise RuntimeError(_("Couldn't find kernel for " - "%(distro)s tree.") % - {"distro": self.PRETTY_NAME}) + def get_kernel_paths(self): + return self._kernel_paths def get_osdict_info(self): """