[master 1/1] Add transport adapters to support ftp and file fetching

dashea installerbot-noreply at redhat.com
Thu May 7 17:29:05 UTC 2015


From: David Shea <dshea at redhat.com>

---
 pyanaconda/geoloc.py                | 10 ++++++----
 pyanaconda/iutil.py                 | 11 +++++++++++
 pyanaconda/kickstart.py             |  2 +-
 pyanaconda/packaging/__init__.py    |  8 ++++++--
 pyanaconda/packaging/livepayload.py |  4 ++--
 5 files changed, 26 insertions(+), 9 deletions(-)

diff --git a/pyanaconda/geoloc.py b/pyanaconda/geoloc.py
index b61aec1..89f0079 100644
--- a/pyanaconda/geoloc.py
+++ b/pyanaconda/geoloc.py
@@ -99,6 +99,7 @@
 * cell tower geolocation
 
 """
+from pyanaconda.iutil import requests_session
 import requests
 import urllib
 import dbus
@@ -444,6 +445,7 @@ class GeolocationBackend(object):
     def __init__(self):
         self._result = None
         self._result_lock = threading.Lock()
+        self._session = requests_session()
 
     def get_name(self):
         """Get name of the backend
@@ -523,7 +525,7 @@ def get_name(self):
 
     def _refresh(self):
         try:
-            reply = requests.get(self.API_URL, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
+            reply = self._session.get(self.API_URL, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
             if reply.status_code == requests.codes.ok:
                 json_reply = reply.json()
                 territory = json_reply.get("country_code", None)
@@ -567,7 +569,7 @@ def get_name(self):
 
     def _refresh(self):
         try:
-            reply = requests.get(self.API_URL, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
+            reply = self._session.get(self.API_URL, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
             if reply.status_code == requests.codes.ok:
                 reply_dict = reply.json()
                 territory = reply_dict.get("country_code", None)
@@ -608,7 +610,7 @@ def _refresh(self):
         if access_points:
             try:
                 url = self._get_url(access_points)
-                reply = requests.get(url, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
+                reply = self._session.get(url, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
                 result_dict = reply.json()
                 status = result_dict.get('status', 'NOT OK')
                 if status == 'OK':
@@ -698,7 +700,7 @@ def _reverse_geocode_nominatim(self, coordinates):
             coordinates.latitude,
             coordinates.longitude)
         try:
-            reply = requests.get(url, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
+            reply = requests_session().get(url, timeout=constants.NETWORK_CONNECTION_TIMEOUT, verify=True)
             if reply.status_code == requests.codes.ok:
                 reply_dict = reply.json()
                 territory_code = reply_dict['address']['country_code'].upper()
diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index 6798c41..8ee8c9e 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -36,6 +36,10 @@
 import gettext
 import signal
 
+import requests
+from requests_file import FileAdapter
+from requests_ftp import FTPAdapter
+
 from gi.repository import GLib
 
 from pyanaconda.flags import flags
@@ -1229,3 +1233,10 @@ def eintr_retry_call(func, *args):
 def parent_dir(directory):
     """Return the parent's path"""
     return "/".join(os.path.normpath(directory).split("/")[:-1])
+
+def requests_session():
+    """Return a requests.Session object with file and ftp support."""
+    session = requests.Session()
+    session.mount("file://", FileAdapter())
+    session.mount("ftp://", FTPAdapter())
+    return session
diff --git a/pyanaconda/kickstart.py b/pyanaconda/kickstart.py
index 6ae0bf3..b67afbd 100644
--- a/pyanaconda/kickstart.py
+++ b/pyanaconda/kickstart.py
@@ -167,7 +167,7 @@ def getEscrowCertificate(escrowCerts, url):
     log.info("escrow: downloading %s", url)
 
     try:
-        request = requests.get(url, verify=True)
+        request = iutil.requests_session().get(url, verify=True)
     except requests.exceptions.SSLError as e:
         msg = _("SSL error while downloading the escrow certificate:\n\n%s") % e
         raise KickstartError(msg)
diff --git a/pyanaconda/packaging/__init__.py b/pyanaconda/packaging/__init__.py
index 13672cb..0a1dc27 100644
--- a/pyanaconda/packaging/__init__.py
+++ b/pyanaconda/packaging/__init__.py
@@ -36,6 +36,8 @@
 import threading
 import re
 
+from pyanaconda.iutil import requests_session
+
 if __name__ == "__main__":
     from pyanaconda import anaconda_log
     anaconda_log.init()
@@ -134,6 +136,8 @@ def __init__(self, data):
         self.instclass = None
         self.txID = None
 
+        self._session = requests_session()
+
     def setup(self, storage, instClass):
         """ Do any payload-specific setup. """
         self.storage = storage
@@ -350,10 +354,10 @@ def _getTreeInfo(self, url, proxy_url, sslverify):
         response = None
         headers = {"user-agent": USER_AGENT}
         try:
-            response = requests.get("%s/.treeinfo" % url, headers=headers, proxies=proxies, verify=sslverify)
+            response = self._session.get("%s/.treeinfo" % url, headers=headers, proxies=proxies, verify=sslverify)
         except requests.exceptions.RequestException as e:
             try:
-                response = requests.get("%s/treeinfo" % url, headers=headers, proxies=proxies, verify=sslverify)
+                response = self._session.get("%s/treeinfo" % url, headers=headers, proxies=proxies, verify=sslverify)
             except requests.exceptions.RequestException as e:
                 log.info("Error downloading treeinfo: %s", e)
                 response = None
diff --git a/pyanaconda/packaging/livepayload.py b/pyanaconda/packaging/livepayload.py
index 0868c26..9cd951c 100644
--- a/pyanaconda/packaging/livepayload.py
+++ b/pyanaconda/packaging/livepayload.py
@@ -268,7 +268,7 @@ def _setup_url_image(self):
 
         error = None
         try:
-            response = requests.get(self.data.method.url, proxies=self._proxies, verify=True)
+            response = self._session.get(self.data.method.url, proxies=self._proxies, verify=True)
 
             # At this point we know we can get the image and what its size is
             # Make a guess as to minimum size needed:
@@ -327,7 +327,7 @@ def _preInstall_url_image(self):
             log.info("Starting image download")
             with open(self.image_path, "wb") as f:
                 ssl_verify = not self.data.method.noverifyssl
-                response = requests.get(self.data.method.url, proxies=self._proxies, verify=ssl_verify, stream=True)
+                response = self._session.get(self.data.method.url, proxies=self._proxies, verify=ssl_verify, stream=True)
                 total_length = response.headers.get('content-length')
                 if total_length is None:  # no content length header
                     # just download the file in one go and fake the progress reporting once done


-- 
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/c0b5fa2735c91e3feef9b58ac94fd191b260b83f


More information about the anaconda-patches mailing list