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
36 the path is a directory
39 if os.path.exists(file_path):
40 if os.path.isdir(file_path):
42 return os.path.isfile(file_path)
46 def download(url, dest_path, name=None):
48 Download a file to a destination path given a URL
49 :param url: the endpoint to the file to download
50 :param dest_path: the directory to save the file
51 :param name: the file name (optional)
55 name = url.rsplit('/')[-1]
56 dest = dest_path + '/' + name
57 logger.debug('Downloading file from - ' + url)
58 # Override proxy settings to use localhost to download file
61 if not os.path.isdir(dest_path):
67 with open(dest, 'wb') as download_file:
68 logger.debug('Saving file to - ' + os.path.abspath(download_file.name))
69 response = __get_url_response(url)
70 download_file.write(response.read())
77 def get_content_length(url):
79 Returns the number of bytes to be downloaded from the given URL
80 :param url: the URL to inspect
81 :return: the number of bytes
83 response = __get_url_response(url)
84 return response.headers['Content-Length']
87 def __get_url_response(url):
89 Returns a response object for a given URL
93 proxy_handler = urllib.ProxyHandler({})
94 opener = urllib.build_opener(proxy_handler)
95 urllib.install_opener(opener)
96 return urllib.urlopen(url)
99 def read_yaml(config_file_path):
101 Reads the yaml file and returns a dictionary object representation
102 :param config_file_path: The file path to config
103 :return: a dictionary
105 logger.debug('Attempting to load configuration file - ' + config_file_path)
108 with open(config_file_path) as config_file:
109 config = yaml.safe_load(config_file)
110 logger.info('Loaded configuration')
114 logger.info('Closing configuration file')
118 def read_os_env_file(os_env_filename):
120 Reads the OS environment source file and returns a map of each key/value
121 Will ignore lines beginning with a '#' and will replace any single or
122 double quotes contained within the value
123 :param os_env_filename: The name of the OS environment file to read
124 :return: a dictionary
127 logger.info('Attempting to read OS environment file - %s',
132 env_file = open(os_env_filename)
133 for line in env_file:
135 if not line.startswith('#') and line.startswith('export '):
136 line = line.lstrip('export ').strip()
137 tokens = line.split('=')
139 # Remove leading and trailing ' & " characters from
141 out[tokens[0]] = tokens[1].lstrip('\'').lstrip('\"').rstrip('\'').rstrip('\"')
148 def read_file(filename):
150 Returns the contents of a file as a string
151 :param filename: the name of the file
157 the_file = open(filename)
158 for line in the_file: