Skip to content
Snippets Groups Projects
Unverified Commit ec9ed11f authored by Oliver Smith's avatar Oliver Smith
Browse files

pmb.helpers.repo.download: proper error for 404

Flush the progress bar displayed by the function before logging a
warning message for 404 not found responses from the server. Without
flushing, this message gets lost.

In addition to the warning, display a NOTE and ERROR at the end to
explain what went wrong:

  [20:16:50] Update package index for x86_64 (4 file(s))
  [20:16:51] WARNING: file not found: http://mirror.postmarketos.org/postmarketos_get_404_test/edge/main/x86_64/APKINDEX.tar.gz
  [20:16:51] NOTE: check the [mirrors] section in 'pmbootstrap config'
  [20:16:51] ERROR: getting APKINDEX from binary package mirror failed!

Without handling this properly, pmbootstrap would fail slightly later
with a much less useful error:

  [15:14:28] ERROR: expected str, bytes or os.PathLike object, not NoneType

Fixes: pmbootstrap issue 2424
parent 50321d41
No related branches found
No related tags found
No related merge requests found
Pipeline #209064 failed
......@@ -7,6 +7,7 @@ import os
from pathlib import Path
import shutil
import urllib.request
import pmb.helpers.cli
from pmb.core.context import get_context
import pmb.helpers.run
......@@ -17,7 +18,8 @@ def cache_file(prefix: str, url: str) -> Path:
return Path(f"{prefix}_{hashlib.sha256(url.encode('utf-8')).hexdigest()}")
def download(url, prefix, cache=True, loglevel=logging.INFO, allow_404=False):
def download(url, prefix, cache=True, loglevel=logging.INFO, allow_404=False,
flush_progress_bar_on_404=False):
"""Download a file to disk.
:param url: the http(s) address of to the file to download
......@@ -30,6 +32,8 @@ def download(url, prefix, cache=True, loglevel=logging.INFO, allow_404=False):
point in showing a dozen messages.
:param allow_404: do not raise an exception when the server responds with a 404 Not Found error.
Only display a warning on stdout (no matter if loglevel is changed).
:param flush_progress_bar_on_404: download happens while a progress bar is
displayed, flush it before printing a warning for 404
:returns: path to the downloaded file in the cache or None on 404
"""
......@@ -58,6 +62,8 @@ def download(url, prefix, cache=True, loglevel=logging.INFO, allow_404=False):
# Handle 404
except urllib.error.HTTPError as e:
if e.code == 404 and allow_404:
if flush_progress_bar_on_404:
pmb.helpers.cli.progress_flush()
logging.warning("WARNING: file not found: " + url)
return None
raise
......
......@@ -10,6 +10,7 @@ See also:
import os
import hashlib
from pmb.helpers.exceptions import NonBugError
from pmb.core.context import get_context
from pmb.core.arch import Arch
from pmb.core.pkgrepo import pkgrepo_names
......@@ -209,7 +210,10 @@ def update(arch: Arch | None = None, force=False, existing_only=False):
# Download and move to right location
for i, (url, target) in enumerate(outdated.items()):
pmb.helpers.cli.progress_print(i / len(outdated))
temp = pmb.helpers.http.download(url, "APKINDEX", False, logging.DEBUG, True)
temp = pmb.helpers.http.download(url, "APKINDEX", False, logging.DEBUG, True, True)
if not temp:
logging.info("NOTE: check the [mirrors] section in 'pmbootstrap config'")
raise NonBugError("getting APKINDEX from binary package mirror failed!")
target_folder = os.path.dirname(target)
if not os.path.exists(target_folder):
pmb.helpers.run.root(["mkdir", "-p", target_folder])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment