diff options
author | spisarski <s.pisarski@cablelabs.com> | 2017-04-20 14:15:30 -0600 |
---|---|---|
committer | spisarski <s.pisarski@cablelabs.com> | 2017-04-26 09:18:20 +0200 |
commit | becbbabc63b0eff15ed6979947aeb75ddf6819c9 (patch) | |
tree | e88dfc06420ddaa66745df6523a25ceb80f3417f /snaps/file_utils.py | |
parent | 02fa5b4fcc78e8ea7cb34acd1175ede6af48df00 (diff) |
Added support for Glance v2
Updated copyright date on new and edited files to current year.
JIRA: SNAPS-66
Change-Id: I491157d6ced8bd9322f99272fc14e00168faaf29
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/file_utils.py')
-rw-r--r-- | snaps/file_utils.py | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/snaps/file_utils.py b/snaps/file_utils.py index f66ac17..34eb30c 100644 --- a/snaps/file_utils.py +++ b/snaps/file_utils.py @@ -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"); @@ -51,14 +51,16 @@ def get_file(file_path): 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 :rtype : File object """ - name = url.rsplit('/')[-1] + if not name: + name = url.rsplit('/')[-1] dest = dest_path + '/' + name try: + logger.debug('Downloading file from - ' + url) # Override proxy settings to use localhost to download file proxy_handler = urllib2.ProxyHandler({}) opener = urllib2.build_opener(proxy_handler) @@ -68,10 +70,24 @@ def download(url, dest_path): raise Exception with open(dest, 'wb') as f: + logger.debug('Saving file to - ' + dest) f.write(response.read()) return f +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 + """ + proxy_handler = urllib2.ProxyHandler({}) + opener = urllib2.build_opener(proxy_handler) + urllib2.install_opener(opener) + response = urllib2.urlopen(url) + return response.headers['Content-Length'] + + def read_yaml(config_file_path): """ Reads the yaml file and returns a dictionary object representation |