From 9e4287321f0159673571cff68cc3f09a3fcf8acf Mon Sep 17 00:00:00 2001 From: Cole Robinson Date: Tue, 29 Jan 2019 17:00:30 -0500 Subject: [PATCH] urlfetcher: Drop explicit prepare/cleanup calls We can do this automatically easily enough. The downsides of not cleaning up are pretty minor these days anyways without the 'mount' handling --- tests/test_urls.py | 6 +----- virtManager/create.py | 4 ++-- virtinst/installertreemedia.py | 35 +++++++--------------------------- virtinst/urlfetcher.py | 16 ++++++++++------ 4 files changed, 20 insertions(+), 41 deletions(-) diff --git a/tests/test_urls.py b/tests/test_urls.py index 65ec9680..4bfd0b4e 100644 --- a/tests/test_urls.py +++ b/tests/test_urls.py @@ -198,11 +198,7 @@ def _testURL(fetcher, testdata): def _fetchWrapper(url, cb): fetcher = urlfetcher.fetcherForURI(url, "/tmp", meter) - try: - fetcher.prepareLocation() - return cb(fetcher) - finally: - fetcher.cleanupLocation() + return cb(fetcher) def _testURLWrapper(testdata): diff --git a/virtManager/create.py b/virtManager/create.py index d3844a52..9eb9e2ef 100644 --- a/virtManager/create.py +++ b/virtManager/create.py @@ -217,6 +217,8 @@ class vmmCreate(vmmGObjectUI): if self._storage_browser: self._storage_browser.close() self._set_conn(None) + self._failed_guest = None + self._guest = None def _cleanup(self): if self._storage_browser: @@ -331,8 +333,6 @@ class vmmCreate(vmmGObjectUI): Reset all UI state to default values. Conn specific state is populated in _populate_conn_state """ - self._failed_guest = None - self._guest = None self.reset_finish_cursor() self.widget("create-pages").set_current_page(PAGE_NAME) diff --git a/virtinst/installertreemedia.py b/virtinst/installertreemedia.py index 9887d41b..7d75d17c 100644 --- a/virtinst/installertreemedia.py +++ b/virtinst/installertreemedia.py @@ -106,7 +106,6 @@ class InstallerTreeMedia(object): return self._cached_fetcher def _get_store(self, guest, fetcher): - # Caller is responsible for calling fetcher prepare/cleanup if needed if not self._cached_store: self._cached_store = urldetect.getDistroStore(guest, fetcher) return self._cached_store @@ -143,17 +142,7 @@ class InstallerTreeMedia(object): def prepare(self, guest, meter): fetcher = self._get_fetcher(guest, meter) - try: - try: - fetcher.prepareLocation() - except ValueError as e: - logging.debug("Error preparing install location", - exc_info=True) - raise ValueError(_("Invalid install location: ") + str(e)) - - return self._prepare_kernel_url(guest, fetcher) - finally: - fetcher.cleanupLocation() + return self._prepare_kernel_url(guest, fetcher) def cleanup(self, guest): ignore = guest @@ -176,25 +165,15 @@ class InstallerTreeMedia(object): if self._media_type not in [MEDIA_URL]: return True - try: - fetcher = self._get_fetcher(guest, None) - fetcher.prepareLocation() - - # This will throw an error for us - ignore = self._get_store(guest, fetcher) - finally: - fetcher.cleanupLocation() + fetcher = self._get_fetcher(guest, None) + # This will throw an error for us + ignore = self._get_store(guest, fetcher) return True def detect_distro(self, guest): if self._media_type in [MEDIA_ISO]: return InstallerTreeMedia.detect_iso_distro(guest, self.location) - try: - fetcher = self._get_fetcher(guest, None) - fetcher.prepareLocation() - - store = self._get_store(guest, fetcher) - return store.get_osdict_info() - finally: - fetcher.cleanupLocation() + fetcher = self._get_fetcher(guest, None) + store = self._get_store(guest, fetcher) + return store.get_osdict_info() diff --git a/virtinst/urlfetcher.py b/virtinst/urlfetcher.py index 57ee42d6..bbdc96fd 100644 --- a/virtinst/urlfetcher.py +++ b/virtinst/urlfetcher.py @@ -33,6 +33,7 @@ class _URLFetcher(object): self.meter = meter logging.debug("Using scratchdir=%s", scratchdir) + self._prepare() #################### @@ -103,18 +104,21 @@ class _URLFetcher(object): """ return self._is_iso - def prepareLocation(self): + def _prepare(self): """ Perform any necessary setup """ pass - def cleanupLocation(self): + def _cleanup(self): """ Perform any necessary cleanup """ pass + def __del__(self): + self._cleanup() + def can_access(self): """ Return True if the location URL seems to be valid @@ -165,10 +169,10 @@ class _URLFetcher(object): class _HTTPURLFetcher(_URLFetcher): _session = None - def prepareLocation(self): + def _prepare(self): self._session = requests.Session() - def cleanupLocation(self): + def _cleanup(self): if self._session: try: self._session.close() @@ -219,7 +223,7 @@ class _HTTPURLFetcher(_URLFetcher): class _FTPURLFetcher(_URLFetcher): _ftp = None - def prepareLocation(self): + def _prepare(self): if self._ftp: return @@ -246,7 +250,7 @@ class _FTPURLFetcher(_URLFetcher): return urlobj, size - def cleanupLocation(self): + def _cleanup(self): if not self._ftp: return