1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 # and others. All rights reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
18 import urllib.request as urllib
20 import urllib2 as urllib
24 __author__ = 'spisarski'
27 Utilities for basic file handling
30 logger = logging.getLogger('file_utils')
33 def file_exists(file_path):
35 Returns True if the image file already exists and throws an exception if the path is a directory
38 if os.path.exists(file_path):
39 if os.path.isdir(file_path):
41 return os.path.isfile(file_path)
45 def download(url, dest_path, name=None):
47 Download a file to a destination path given a URL
48 :param url: the endpoint to the file to download
49 :param dest_path: the directory to save the file
50 :param name: the file name (optional)
54 name = url.rsplit('/')[-1]
55 dest = dest_path + '/' + name
56 logger.debug('Downloading file from - ' + url)
57 # Override proxy settings to use localhost to download file
60 if not os.path.isdir(dest_path):
66 with open(dest, 'wb') as f:
67 logger.debug('Saving file to - ' + os.path.abspath(f.name))
68 response = __get_url_response(url)
69 f.write(response.read())
76 def get_content_length(url):
78 Returns the number of bytes to be downloaded from the given URL
79 :param url: the URL to inspect
80 :return: the number of bytes
82 response = __get_url_response(url)
83 return response.headers['Content-Length']
86 def __get_url_response(url):
88 Returns a response object for a given URL
92 proxy_handler = urllib.ProxyHandler({})
93 opener = urllib.build_opener(proxy_handler)
94 urllib.install_opener(opener)
95 return urllib.urlopen(url)
98 def read_yaml(config_file_path):
100 Reads the yaml file and returns a dictionary object representation
101 :param config_file_path: The file path to config
102 :return: a dictionary
104 logger.debug('Attempting to load configuration file - ' + config_file_path)
105 with open(config_file_path) as config_file:
106 config = yaml.safe_load(config_file)
107 logger.info('Loaded configuration')
109 logger.info('Closing configuration file')
113 def read_os_env_file(os_env_filename):
115 Reads the OS environment source file and returns a map of each key/value
116 Will ignore lines beginning with a '#' and will replace any single or double quotes contained within the value
117 :param os_env_filename: The name of the OS environment file to read
118 :return: a dictionary
121 logger.info('Attempting to read OS environment file - ' + os_env_filename)
123 for line in open(os_env_filename):
125 if not line.startswith('#') and line.startswith('export '):
126 line = line.lstrip('export ').strip()
127 tokens = line.split('=')
129 # Remove leading and trailing ' & " characters from value
130 out[tokens[0]] = tokens[1].lstrip('\'').lstrip('\"').rstrip('\'').rstrip('\"')
134 def read_file(filename):
136 Returns the contents of a file as a string
137 :param filename: the name of the file
141 for line in open(filename):