tests/functional/qemu_test: Split huge fetch() function in asset.py
The fetch() function has become really huge and pylint complains about that. Extract the internal retry-three-times-download loop into a separate function to make it a little bit more readable and to make pylint happy about this file again. Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Signed-off-by: Thomas Huth <thuth@redhat.com> Message-ID: <20260324163543.55503-4-thuth@redhat.com>
Thomas Huth committed
Mar 24, 2026 at 17:35 UTC
bfda4354ee36654b3985ab44bec2e92bd4db19eb
1 file changed
+24
-19
tests/functional/qemu_test/asset.py
+24
-19
@@ -133,24 +133,7 @@ class Asset:
133
'''
134
self.cache_file.with_suffix(".stamp").write_text(f"{int(time.time())}")
135
136
- def fetch(self):
137
- '''Download the asset from the internet'''
138
- if not self.cache_dir.exists():
139
- self.cache_dir.mkdir(parents=True, exist_ok=True)
140
-
141
- if self.valid():
142
- self.log.debug("Using cached asset %s for %s",
143
- self.cache_file, self.url)
144
- self._save_time_stamp()
145
- return str(self.cache_file)
146
-
147
- if not self.fetchable():
148
- raise AssetError(self,
149
- "Asset cache is invalid and downloads disabled")
150
-
151
- self.log.info("Downloading %s to %s...", self.url, self.cache_file)
152
- tmp_cache_file = self.cache_file.with_suffix(".download")
153
-
136
+ def _try_to_fetch(self, tmp_cache_file):
137
for _retries in range(3):
138
try:
139
with tmp_cache_file.open("xb") as dst:
@@ -176,7 +159,7 @@ class Asset:
159
"waiting for other thread to finish...",
160
tmp_cache_file)
161
if self._wait_for_other_download(tmp_cache_file):
179
- return str(self.cache_file)
162
+ return True
163
self.log.debug("%s seems to be stale, "
164
"deleting and retrying download...",
165
tmp_cache_file)
@@ -213,6 +196,28 @@ class Asset:
196
tmp_cache_file.unlink()
197
raise AssetError(self, f"Unable to download: {e}",
198
transient=True) from e
199
+ return False
200
+
201
+ def fetch(self):
202
+ '''Download the asset from the internet'''
203
+ if not self.cache_dir.exists():
204
+ self.cache_dir.mkdir(parents=True, exist_ok=True)
205
+
206
+ if self.valid():
207
+ self.log.debug("Using cached asset %s for %s",
208
+ self.cache_file, self.url)
209
+ self._save_time_stamp()
210
+ return str(self.cache_file)
211
+
212
+ if not self.fetchable():
213
+ raise AssetError(self,
214
+ "Asset cache is invalid and downloads disabled")
215
+
216
+ self.log.info("Downloading %s to %s...", self.url, self.cache_file)
217
+ tmp_cache_file = self.cache_file.with_suffix(".download")
218
+
219
+ if self._try_to_fetch(tmp_cache_file):
220
+ return str(self.cache_file)
221
222
if not os.path.exists(tmp_cache_file):
223
raise AssetError(self, "Download retries exceeded", transient=True)