Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proxies): define environment variable for proxy use for git clone #584

Merged
merged 3 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
jmkerloch marked this conversation as resolved.
Show resolved Hide resolved
"""
# 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