Skip to content

Commit

Permalink
Add transport adapters to support ftp and file fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
dashea committed May 7, 2015
1 parent 403e5e2 commit c0b5fa2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
10 changes: 6 additions & 4 deletions pyanaconda/geoloc.py
Expand Up @@ -99,6 +99,7 @@
* cell tower geolocation
"""
from pyanaconda.iutil import requests_session
import requests
import urllib
import dbus
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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':
Expand Down Expand Up @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions pyanaconda/iutil.py
Expand Up @@ -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
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion pyanaconda/kickstart.py
Expand Up @@ -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)
Expand Down
8 changes: 6 additions & 2 deletions pyanaconda/packaging/__init__.py
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pyanaconda/packaging/livepayload.py
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c0b5fa2

Please sign in to comment.