Merge "Default OSCreds cacert attribute to False."
[snaps.git] / snaps / file_utils.py
index f66ac17..a7ed13c 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
+# Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
 #                    and others.  All rights reserved.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # See the License for the specific language governing permissions and
 # limitations under the License.
 import os
-import urllib2
 import logging
+try:
+    import urllib.request as urllib
+except ImportError:
+    import urllib2 as urllib
 
 import yaml
 
@@ -39,37 +42,57 @@ def file_exists(file_path):
     return False
 
 
-def get_file(file_path):
-    """
-    Returns True if the image file has already been downloaded
-    :return: the image file object
-    :raise Exception when file cannot be found
-    """
-    if file_exists(file_path):
-        return open(file_path, 'r')
-    else:
-        raise Exception('File with path cannot be found - ' + file_path)
-
-
-def download(url, dest_path):
+def download(url, dest_path, name=None):
     """
     Download a file to a destination path given a URL
+    :param url: the endpoint to the file to download
+    :param dest_path: the directory to save the file
+    :param name: the file name (optional)
     :rtype : File object
     """
-    name = url.rsplit('/')[-1]
+    if not name:
+        name = url.rsplit('/')[-1]
     dest = dest_path + '/' + name
+    logger.debug('Downloading file from - ' + url)
+    # Override proxy settings to use localhost to download file
+    f = None
+
+    if not os.path.isdir(dest_path):
+        try:
+            os.mkdir(dest_path)
+        except:
+            raise
     try:
-        # Override proxy settings to use localhost to download file
-        proxy_handler = urllib2.ProxyHandler({})
-        opener = urllib2.build_opener(proxy_handler)
-        urllib2.install_opener(opener)
-        response = urllib2.urlopen(url)
-    except (urllib2.HTTPError, urllib2.URLError):
-        raise Exception
+        with open(dest, 'wb') as f:
+            logger.debug('Saving file to - ' + os.path.abspath(f.name))
+            response = __get_url_response(url)
+            f.write(response.read())
+        return f
+    finally:
+        if f:
+            f.close()
+
+
+def get_content_length(url):
+    """
+    Returns the number of bytes to be downloaded from the given URL
+    :param url: the URL to inspect
+    :return: the number of bytes
+    """
+    response = __get_url_response(url)
+    return response.headers['Content-Length']
 
-    with open(dest, 'wb') as f:
-        f.write(response.read())
-    return f
+
+def __get_url_response(url):
+    """
+    Returns a response object for a given URL
+    :param url: the URL
+    :return: the response
+    """
+    proxy_handler = urllib.ProxyHandler({})
+    opener = urllib.build_opener(proxy_handler)
+    urllib.install_opener(opener)
+    return urllib.urlopen(url)
 
 
 def read_yaml(config_file_path):
@@ -106,3 +129,16 @@ def read_os_env_file(os_env_filename):
                     # Remove leading and trailing ' & " characters from value
                     out[tokens[0]] = tokens[1].lstrip('\'').lstrip('\"').rstrip('\'').rstrip('\"')
         return out
+
+
+def read_file(filename):
+    """
+    Returns the contents of a file as a string
+    :param filename: the name of the file
+    :return:
+    """
+    out = str()
+    for line in open(filename):
+        out += line
+
+    return out