Skip to content

Commit 8d72ea1

Browse files
committed
Refactor urllib download
1 parent a707cdd commit 8d72ea1

1 file changed

Lines changed: 12 additions & 52 deletions

File tree

abraia/hailo/core.py

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66
import time
77
import yaml
88
import shlex
9-
import tempfile
9+
import requests
1010
import subprocess
11-
import urllib.request
11+
1212
from pathlib import Path
1313
from functools import lru_cache
1414
from dataclasses import dataclass
1515
from typing import Any, Optional, Tuple, Dict
1616
from concurrent.futures import ThreadPoolExecutor, as_completed
1717

18+
from ..utils import download_url
19+
1820
from .hailo_logger import get_logger
1921
hailo_logger = get_logger(__name__)
2022

21-
2223
# Base Defaults
2324
HAILO8_ARCH = "hailo8"
2425
HAILO8L_ARCH = "hailo8l"
@@ -303,35 +304,13 @@ def is_none_value(value: Any) -> bool:
303304
def get_remote_file_size(url: str, timeout: int = 30) -> Optional[int]:
304305
"""Get Content-Length of a remote file via HEAD request."""
305306
try:
306-
req = urllib.request.Request(url, method='HEAD', headers={'User-Agent': DEFAULT_USER_AGENT})
307-
with urllib.request.urlopen(req, timeout=timeout) as resp:
308-
length = resp.headers.get('Content-Length')
309-
return int(length) if length else None
307+
r = requests.head(url, headers={'User-Agent': DEFAULT_USER_AGENT}, timeout=timeout, allow_redirects=True)
308+
length = r.headers.get('Content-Length')
309+
return int(length) if length else None
310310
except Exception:
311311
return None
312312

313313

314-
class ProgressTracker:
315-
"""Simple terminal progress bar for downloads."""
316-
def __init__(self, show_progress: bool = True):
317-
self.show_progress = show_progress
318-
self._last_percent = -1
319-
320-
def update(self, downloaded: int, total_size: int):
321-
if not self.show_progress or total_size <= 0:
322-
return
323-
percent = min(100, (downloaded * 100) // total_size)
324-
if percent == self._last_percent:
325-
return
326-
self._last_percent = percent
327-
bar = '=' * (percent // 2.5) + '-' * (40 - int(percent // 2.5))
328-
print(f"\r[{bar}] {percent}% ({downloaded/1e6:.2f}/{total_size/1e6:.2f} MB)", end='', flush=True)
329-
330-
def finish(self):
331-
if self.show_progress:
332-
print()
333-
334-
335314
class ResourceDownloader:
336315
"""Manages downloading of models and resources from various sources."""
337316
RESOURCE_MAP = {
@@ -373,37 +352,18 @@ def _download_file_with_retry(self, task: DownloadTask) -> DownloadResult:
373352
task.dest_path.parent.mkdir(parents=True, exist_ok=True)
374353
last_err = None
375354
for attempt in range(self.download_config.max_retries):
376-
temp_path = None
377355
try:
378-
fd, temp_path_str = tempfile.mkstemp(dir=task.dest_path.parent, prefix=f".{task.name}.", suffix=".tmp")
379-
os.close(fd)
380-
temp_path = Path(temp_path_str)
381-
382-
req = urllib.request.Request(task.url, headers={'User-Agent': DEFAULT_USER_AGENT})
383-
progress = ProgressTracker(self.download_config.show_progress)
384356
hailo_logger.info(f"Downloading: {task.url}")
357+
download_url(task.url, str(task.dest_path))
385358

386-
with urllib.request.urlopen(req, timeout=self.download_config.timeout) as resp:
387-
total = int(resp.headers.get('Content-Length', 0))
388-
with open(temp_path, 'wb') as f:
389-
downloaded = 0
390-
while chunk := resp.read(8192):
391-
f.write(chunk)
392-
downloaded += len(chunk)
393-
progress.update(downloaded, total)
394-
progress.finish()
395-
396-
if remote_size and temp_path.stat().st_size != remote_size:
397-
raise ValueError(f"Size mismatch: got {temp_path.stat().st_size}, expected {remote_size}")
359+
if remote_size and task.dest_path.stat().st_size != remote_size:
360+
raise ValueError(f"Size mismatch: got {task.dest_path.stat().st_size}, expected {remote_size}")
398361

399-
if task.dest_path.exists():
400-
task.dest_path.unlink()
401-
temp_path.rename(task.dest_path)
402362
return DownloadResult(task, True, "Success", False, task.dest_path.stat().st_size)
403363
except Exception as e:
404364
last_err = e
405-
if temp_path and temp_path.exists():
406-
temp_path.unlink()
365+
if task.dest_path.exists():
366+
task.dest_path.unlink()
407367
if attempt < self.download_config.max_retries - 1:
408368
time.sleep(self.download_config.retry_delay * (2 ** attempt))
409369

0 commit comments

Comments
 (0)