From e8018af36b753f7059aa4641b167dd8c05253ea4 Mon Sep 17 00:00:00 2001 From: jmkerloch <53606373+jmkerloch@users.noreply.github.com> Date: Fri, 29 Nov 2024 08:37:19 +0100 Subject: [PATCH 1/3] fix(proxies): define environment variable for proxy use for git clone - define wrapper for environment variable definition and backup - use wrapper for all function using porcelain for git command --- .../profiles/profiles_handler_base.py | 7 ++++ qgis_deployment_toolbelt/utils/proxies.py | 34 +++++++++++++++++++ 2 files changed, 41 insertions(+) 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..339e263f 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,39 @@ def get_proxy_settings_from_pac_file( return proxy_settings +def os_env_proxy(func): + def wrapper(*args, **kwargs): + """Decorator wrapper + + 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 ######## # ################################## From 30fa995360ece05fd2e62cf27e292643f991a350 Mon Sep 17 00:00:00 2001 From: jmkerloch <53606373+jmkerloch@users.noreply.github.com> Date: Fri, 29 Nov 2024 11:37:29 +0000 Subject: [PATCH 2/3] feat(proxy): add docstring for os_env_proxy wrapper --- qgis_deployment_toolbelt/utils/proxies.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/qgis_deployment_toolbelt/utils/proxies.py b/qgis_deployment_toolbelt/utils/proxies.py index 339e263f..28c7cbe7 100644 --- a/qgis_deployment_toolbelt/utils/proxies.py +++ b/qgis_deployment_toolbelt/utils/proxies.py @@ -162,7 +162,14 @@ def get_proxy_settings_from_pac_file( def os_env_proxy(func): def wrapper(*args, **kwargs): - """Decorator wrapper + """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 From cfda5976f0106328c32b555529aec92b3b184139 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 29 Nov 2024 11:38:38 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- qgis_deployment_toolbelt/utils/proxies.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qgis_deployment_toolbelt/utils/proxies.py b/qgis_deployment_toolbelt/utils/proxies.py index 28c7cbe7..92f87a45 100644 --- a/qgis_deployment_toolbelt/utils/proxies.py +++ b/qgis_deployment_toolbelt/utils/proxies.py @@ -165,11 +165,11 @@ 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 + - backup current environment value - define environment value with proxy settings - run function - restore environment value if available - + Returns: _type_: function result