diff --git a/qgis_deployment_toolbelt/profiles/profiles_handler_base.py b/qgis_deployment_toolbelt/profiles/profiles_handler_base.py index ad245bc3..a9a370ca 100644 --- a/qgis_deployment_toolbelt/profiles/profiles_handler_base.py +++ b/qgis_deployment_toolbelt/profiles/profiles_handler_base.py @@ -28,6 +28,7 @@ from giturlparse import validate as git_validate # project +from qgis_deployment_toolbelt.utils import proxies from qgis_deployment_toolbelt.utils.check_path import check_folder_is_empty # ############################################################################# @@ -223,6 +224,7 @@ def _is_url_git_repository( ) return False + @proxies.os_env_proxy def get_active_branch_from_local_repository( self, local_git_repository_path: Path | None = None ) -> str: @@ -302,6 +304,7 @@ def is_branch_existing_in_repository( ) ] + @proxies.os_env_proxy def list_remote_branches( self, source_repository_path_or_url: Path | str | None = None ) -> tuple[str]: @@ -351,6 +354,7 @@ def list_remote_branches( else: return ("",) + @proxies.os_env_proxy def download(self, destination_local_path: Path) -> Repo: """Generic wrapper around the specific logic of this handler. @@ -471,6 +475,7 @@ def clone_or_pull(self, to_local_destination_path: Path, attempt: int = 1) -> Re ) return None + @proxies.os_env_proxy def _clone(self, local_path: Path) -> Repo: """Clone the remote repository to local path. @@ -526,6 +531,7 @@ def _clone(self, local_path: Path) -> Repo: ) return repo_obj + @proxies.os_env_proxy def _fetch(self, local_path: Path) -> Repo: """Fetch the remote repository from the existing local repository. @@ -564,6 +570,7 @@ def _fetch(self, local_path: Path) -> Repo: return destination_local_repository + @proxies.os_env_proxy def _pull(self, local_path: Path) -> Repo: """Pull the remote repository from the existing local repository. diff --git a/qgis_deployment_toolbelt/utils/proxies.py b/qgis_deployment_toolbelt/utils/proxies.py index e64e3ced..92f87a45 100644 --- a/qgis_deployment_toolbelt/utils/proxies.py +++ b/qgis_deployment_toolbelt/utils/proxies.py @@ -12,6 +12,7 @@ # Standard library import logging +import os from functools import lru_cache from os import environ from urllib.request import getproxies @@ -159,6 +160,46 @@ def get_proxy_settings_from_pac_file( return proxy_settings +def os_env_proxy(func): + def wrapper(*args, **kwargs): + """Decorator wrapper to define environment variable for proxy use. + + If a proxy settings is available for https or http we: + - backup current environment value + - define environment value with proxy settings + - run function + - restore environment value if available + + + Returns: + _type_: function result + """ + # Get proxy settings + proxy_settings = get_proxy_settings() + + # Update environment variable and keep current value + prev_http_proxy = None + if "http" in proxy_settings: + prev_http_proxy = environ.get("HTTP_PROXY") + os.environ["HTTP_PROXY"] = proxy_settings["http"] + prev_https_proxy = None + if "https" in proxy_settings: + prev_https_proxy = environ.get("HTTPS_PROXY") + os.environ["HTTPS_PROXY"] = proxy_settings["https"] + + # Run function + result = func(*args, **kwargs) + + # Restore environment variable if available + if prev_http_proxy: + os.environ["HTTP_PROXY"] = prev_http_proxy + if prev_https_proxy: + os.environ["HTTPS_PROXY"] = prev_https_proxy + return result + + return wrapper + + # ############################################################################# # ##### Stand alone program ######## # ##################################