Skip to content

Commit

Permalink
fix(proxies): define environment variable for proxy use for git clone (
Browse files Browse the repository at this point in the history
…#584)

When a proxy is needed for git clone, `dulwich` and high level interface
`porcelain` only use environment variable for proxy definition :
`HTTP_PROXY` / `HTTPS_PROXY`

In this PR we are :
- defining a wrapper for environment variable definition and backup
- use wrapper for all function using porcelain for git command
  • Loading branch information
jmkerloch authored Nov 29, 2024
2 parents 3534857 + cfda597 commit 3c2c7ca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
7 changes: 7 additions & 0 deletions qgis_deployment_toolbelt/profiles/profiles_handler_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# #############################################################################
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
41 changes: 41 additions & 0 deletions qgis_deployment_toolbelt/utils/proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# Standard library
import logging
import os
from functools import lru_cache
from os import environ
from urllib.request import getproxies
Expand Down Expand Up @@ -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 ########
# ##################################
Expand Down

0 comments on commit 3c2c7ca

Please sign in to comment.