forked from Mirror/pmbootstrap
pmb.helpers.repo.download: proper error for 404 (MR 2462)
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
This commit is contained in:
parent
3ead8f8859
commit
7269d05b20
2 changed files with 13 additions and 2 deletions
|
@ -7,6 +7,7 @@ import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
import urllib.request
|
import urllib.request
|
||||||
|
import pmb.helpers.cli
|
||||||
|
|
||||||
from pmb.core.context import get_context
|
from pmb.core.context import get_context
|
||||||
import pmb.helpers.run
|
import pmb.helpers.run
|
||||||
|
@ -17,7 +18,9 @@ def cache_file(prefix: str, url: str) -> Path:
|
||||||
return Path(f"{prefix}_{hashlib.sha256(url.encode('utf-8')).hexdigest()}")
|
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.
|
"""Download a file to disk.
|
||||||
|
|
||||||
:param url: the http(s) address of to the file to download
|
:param url: the http(s) address of to the file to download
|
||||||
|
@ -30,6 +33,8 @@ def download(url, prefix, cache=True, loglevel=logging.INFO, allow_404=False):
|
||||||
point in showing a dozen messages.
|
point in showing a dozen messages.
|
||||||
:param allow_404: do not raise an exception when the server responds with a 404 Not Found error.
|
: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).
|
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
|
:returns: path to the downloaded file in the cache or None on 404
|
||||||
"""
|
"""
|
||||||
|
@ -58,6 +63,8 @@ def download(url, prefix, cache=True, loglevel=logging.INFO, allow_404=False):
|
||||||
# Handle 404
|
# Handle 404
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
if e.code == 404 and allow_404:
|
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)
|
logging.warning("WARNING: file not found: " + url)
|
||||||
return None
|
return None
|
||||||
raise
|
raise
|
||||||
|
|
|
@ -10,6 +10,7 @@ See also:
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import hashlib
|
import hashlib
|
||||||
|
from pmb.helpers.exceptions import NonBugError
|
||||||
from pmb.core.context import get_context
|
from pmb.core.context import get_context
|
||||||
from pmb.core.arch import Arch
|
from pmb.core.arch import Arch
|
||||||
from pmb.core.pkgrepo import pkgrepo_names
|
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
|
# Download and move to right location
|
||||||
for i, (url, target) in enumerate(outdated.items()):
|
for i, (url, target) in enumerate(outdated.items()):
|
||||||
pmb.helpers.cli.progress_print(i / len(outdated))
|
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)
|
target_folder = os.path.dirname(target)
|
||||||
if not os.path.exists(target_folder):
|
if not os.path.exists(target_folder):
|
||||||
pmb.helpers.run.root(["mkdir", "-p", target_folder])
|
pmb.helpers.run.root(["mkdir", "-p", target_folder])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue