diff options
author | 2017-02-15 09:13:54 -0700 | |
---|---|---|
committer | 2017-02-15 09:15:34 -0700 | |
commit | 57777f3df521553a06cd01a3861b415d2905ceca (patch) | |
tree | f3b3be457baec7b5231309989aa3ffa9658cd25d /snaps/file_utils.py | |
parent | 73ef791a1cde68e0d8d69cddf63534fbb90f3e2d (diff) |
Initial patch with all code from CableLabs repository.
Change-Id: I70a2778718c5e7f21fd14e4ad28c9269d3761cc7
Signed-off-by: spisarski <s.pisarski@cablelabs.com>
Diffstat (limited to 'snaps/file_utils.py')
-rw-r--r-- | snaps/file_utils.py | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/snaps/file_utils.py b/snaps/file_utils.py new file mode 100644 index 0000000..f66ac17 --- /dev/null +++ b/snaps/file_utils.py @@ -0,0 +1,108 @@ +# Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs") +# and others. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os +import urllib2 +import logging + +import yaml + +__author__ = 'spisarski' + +""" +Utilities for basic file handling +""" + +logger = logging.getLogger('file_utils') + + +def file_exists(file_path): + """ + Returns True if the image file already exists and throws an exception if the path is a directory + :return: + """ + if os.path.exists(file_path): + if os.path.isdir(file_path): + return False + return os.path.isfile(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): + """ + Download a file to a destination path given a URL + :rtype : File object + """ + name = url.rsplit('/')[-1] + dest = dest_path + '/' + name + 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: + f.write(response.read()) + return f + + +def read_yaml(config_file_path): + """ + Reads the yaml file and returns a dictionary object representation + :param config_file_path: The file path to config + :return: a dictionary + """ + logger.debug('Attempting to load configuration file - ' + config_file_path) + with open(config_file_path) as config_file: + config = yaml.safe_load(config_file) + logger.info('Loaded configuration') + config_file.close() + logger.info('Closing configuration file') + return config + + +def read_os_env_file(os_env_filename): + """ + Reads the OS environment source file and returns a map of each key/value + Will ignore lines beginning with a '#' and will replace any single or double quotes contained within the value + :param os_env_filename: The name of the OS environment file to read + :return: a dictionary + """ + if os_env_filename: + logger.info('Attempting to read OS environment file - ' + os_env_filename) + out = {} + for line in open(os_env_filename): + line = line.lstrip() + if not line.startswith('#') and line.startswith('export '): + line = line.lstrip('export ').strip() + tokens = line.split('=') + if len(tokens) > 1: + # Remove leading and trailing ' & " characters from value + out[tokens[0]] = tokens[1].lstrip('\'').lstrip('\"').rstrip('\'').rstrip('\"') + return out |