From 6085c1be5a0e0400fda17119acd6c97df6df56b3 Mon Sep 17 00:00:00 2001 From: BjoernAtBosch Date: Thu, 29 Aug 2024 15:14:14 +0200 Subject: [PATCH] Suppress exception if download fails and file is present (#22) --- docs/index.md | 1 + docs/velocitas_lib.md | 19 +++++++++++++++++-- velocitas_lib/__init__.py | 22 +++++++++++++++------- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/docs/index.md b/docs/index.md index 9b91dd8..21ecf9a 100644 --- a/docs/index.md +++ b/docs/index.md @@ -37,6 +37,7 @@ - [`velocitas_lib.get_script_path`](./velocitas_lib.md#function-get_script_path): Return the absolute path to the directory the invoked Python script - [`velocitas_lib.get_valid_arch`](./velocitas_lib.md#function-get_valid_arch): Return a known architecture for the given `arch`. - [`velocitas_lib.get_workspace_dir`](./velocitas_lib.md#function-get_workspace_dir): Return the workspace directory. +- [`velocitas_lib.have_internet_connection`](./velocitas_lib.md#function-have_internet_connection) - [`velocitas_lib.is_uri`](./velocitas_lib.md#function-is_uri): Check if the provided path is a URI. - [`velocitas_lib.obtain_local_file_path`](./velocitas_lib.md#function-obtain_local_file_path): Return the absolute path to the file, specified by a absolute/relative local path or with an URI. - [`velocitas_lib.require_env`](./velocitas_lib.md#function-require_env): Require and return an environment variable. diff --git a/docs/velocitas_lib.md b/docs/velocitas_lib.md index 872c595..5fa9ef3 100644 --- a/docs/velocitas_lib.md +++ b/docs/velocitas_lib.md @@ -259,6 +259,21 @@ Check if the provided path is a URI. +## function `have_internet_connection` + +```python +have_internet_connection() → bool +``` + + + + + + +--- + + + ## function `obtain_local_file_path` ```python @@ -286,7 +301,7 @@ Return the absolute path to the file, specified by a absolute/relative local pat --- - + ## function `extract_zip` @@ -317,7 +332,7 @@ Extract a zip file. --- - + ## function `discover_files_in_filetree` diff --git a/velocitas_lib/__init__.py b/velocitas_lib/__init__.py index 3a58043..b25d9ea 100644 --- a/velocitas_lib/__init__.py +++ b/velocitas_lib/__init__.py @@ -14,8 +14,8 @@ import json import os -import sys import re +import sys import zipfile from io import TextIOWrapper from typing import Any, Dict, List, Optional @@ -157,6 +157,14 @@ def is_uri(path: str) -> bool: return re.match(r"(\w+)\:\/\/(\w+)", path) is not None +def have_internet_connection() -> bool: + try: + requests.head("http://www.google.com/", timeout=5) + return True + except requests.ConnectionError: + return False + + def obtain_local_file_path( path_or_uri: str, download_path: Optional[str] = None ) -> str: @@ -181,14 +189,14 @@ def obtain_local_file_path( download_path = os.path.join( get_project_cache_dir(), "downloads", path_or_uri.split("/")[-1] ) - if os.path.isfile(download_path): - path, file = os.path.split(download_path) - parts = file.split(".", 1) - filename = f"{parts[0]}_1.{parts[1]}" if len(parts) > 1 else f"{parts[0]}_1" - download_path = os.path.join(path, filename) + try: + download_file(path_or_uri, download_path) + except requests.ConnectionError: + if have_internet_connection() or not os.path.exists(download_path): + raise + print(f"[WARING] No internet connection -> using cached file {download_path}") - download_file(path_or_uri, download_path) return download_path