Browse Source

west: blobs: fetch exit immediately on HTTP error

If an HTTP error occurs during `west blobs fetch`, the response was written
to a file (even if the response body is empty), and then the checksum
validation fails, which can be somewhat confusing.

Add an immediate error message and exit-with-error-code when the HTTP
request fails.

Tested by modify a blob manifest to have an invalid URL:

```bash
# test with invalid URL
❯ west blobs fetch nrf_wifi
Fetching blob nrf_wifi: .../wifi_fw_bins/default/nrf70.bin
ERROR: HTTP error occurred: 404 Client Error: Not Found for url: ...

# test with networking disabled
❯ west blobs fetch nrf_wifi
Fetching blob nrf_wifi: .../wifi_fw_bins/default/nrf70.bin
ERROR: An error occurred: HTTPSConnectionPool(host='git.... \
  Max retries exceeded with url: \
  .../zzzz/nrf_wifi/bin/zephyr/default/nrf70.bin
...
```

Signed-off-by: Noah Pendleton <noah.pendleton@gmail.com>
pull/90432/head
Noah Pendleton 2 months ago committed by Benjamin Cabé
parent
commit
7f1a639327
  1. 13
      scripts/west_commands/fetchers/http.py

13
scripts/west_commands/fetchers/http.py

@ -2,7 +2,9 @@ @@ -2,7 +2,9 @@
#
# SPDX-License-Identifier: Apache-2.0
import os
import requests
import sys
from west import log
@ -16,5 +18,14 @@ class HTTPFetcher(ZephyrBlobFetcher): @@ -16,5 +18,14 @@ class HTTPFetcher(ZephyrBlobFetcher):
def fetch(self, url, path):
log.dbg(f'HTTPFetcher fetching {url} to {path}')
try:
resp = requests.get(url)
open(path, "wb").write(resp.content)
resp.raise_for_status() # Raises an HTTPError for bad status codes (4xx or 5xx)
except requests.exceptions.HTTPError as e:
log.err(f'HTTP error occurred: {e}')
sys.exit(os.EX_NOHOST)
except requests.exceptions.RequestException as e:
log.err(f'An error occurred: {e}')
sys.exit(os.EX_DATAERR)
with open(path, "wb") as f:
f.write(resp.content)

Loading…
Cancel
Save