11import logging
2- import tempfile
3- from typing import Optional , Any , AsyncGenerator
42
5- import aiohttp
63from aiohttp import ClientResponseError , ClientError
74from lxml .etree import LxmlError
85from gettext import gettext as _
9- from os import environ
106
117from rest_framework import serializers
128
2925from bandersnatch .master import Master
3026from bandersnatch .configuration import BandersnatchConfig
3127from packaging .requirements import Requirement
32- from urllib .parse import urljoin , urlsplit , urlunsplit
28+ from urllib .parse import urljoin
3329
3430logger = logging .getLogger (__name__ )
3531
@@ -114,23 +110,14 @@ async def run(self):
114110 """
115111 If includes is specified, then only sync those,else try to sync all other packages
116112 """
117- # Prevent bandersnatch from reading actual .netrc file, set to empty file
118- # See discussion on https://github.com/pulp/pulp_python/issues/581
119- fake_netrc = tempfile .NamedTemporaryFile (dir = "." , delete = False )
120- environ ["NETRC" ] = fake_netrc .name
121- # TODO Change Bandersnatch internal API to take proxy settings in from config parameters
122- if proxy_url := self .remote .proxy_url :
123- if self .remote .proxy_username or self .remote .proxy_password :
124- parsed_proxy = urlsplit (proxy_url )
125- creds = f"{ self .remote .proxy_username } :{ self .remote .proxy_password } "
126- netloc = f"{ creds } @{ parsed_proxy .netloc } "
127- proxy_url = urlunsplit ((parsed_proxy .scheme , netloc , "" , "" , "" ))
128- environ ['http_proxy' ] = proxy_url
129- environ ['https_proxy' ] = proxy_url
130113 # Bandersnatch includes leading slash when forming API urls
131114 url = self .remote .url .rstrip ("/" )
132- # local & global timeouts defaults to 10secs and 5 hours
133- async with PulpMaster (url , tls = self .remote .tls_validation ) as master :
115+ async with Master (url ) as master :
116+ # Replace the session with the remote's downloader session
117+ old_session = master .session
118+ factory = self .remote .download_factory
119+ master .session = factory ._session
120+
134121 deferred_download = self .remote .policy != Remote .IMMEDIATE
135122 workers = self .remote .download_concurrency or self .remote .DEFAULT_DOWNLOAD_CONCURRENCY
136123 async with ProgressReport (
@@ -150,25 +137,8 @@ async def run(self):
150137 Requirement (pkg ).name for pkg in self .remote .includes
151138 ]
152139 await pmirror .synchronize (packages_to_sync )
153-
154-
155- class PulpMaster (Master ):
156- """
157- Pulp Master Class for Pulp specific overrides
158- """
159-
160- def __init__ (self , * args , tls = True , ** kwargs ):
161- self .tls = tls
162- super ().__init__ (* args , ** kwargs )
163-
164- async def get (
165- self , path : str , required_serial : Optional [int ], ** kw : Any
166- ) -> AsyncGenerator [aiohttp .ClientResponse , None ]:
167- """Support tls=false"""
168- if not self .tls :
169- kw ["ssl" ] = False
170- async for r in super ().get (path , required_serial , ** kw ):
171- yield r
140+ # place back old session so that it is properly closed
141+ master .session = old_session
172142
173143
174144class PulpMirror (Mirror ):
0 commit comments