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.
19 from cryptography.hazmat.primitives import serialization
22 import urllib.request as urllib
24 import urllib2 as urllib
28 __author__ = 'spisarski'
31 Utilities for basic file handling
34 logger = logging.getLogger('file_utils')
37 def file_exists(file_path):
39 Returns True if the image file already exists and throws an exception if
40 the path is a directory
43 expanded_path = os.path.expanduser(file_path)
44 if os.path.exists(expanded_path):
45 if os.path.isdir(expanded_path):
47 return os.path.isfile(expanded_path)
51 def download(url, dest_path, name=None):
53 Download a file to a destination path given a URL
54 :param url: the endpoint to the file to download
55 :param dest_path: the directory to save the file
56 :param name: the file name (optional)
60 name = url.rsplit('/')[-1]
61 dest = dest_path + '/' + name
62 logger.debug('Downloading file from - ' + url)
63 # Override proxy settings to use localhost to download file
66 if not os.path.isdir(dest_path):
72 with open(dest, 'wb') as download_file:
73 logger.debug('Saving file to - %s',
74 os.path.abspath(download_file.name))
75 response = __get_url_response(url)
76 download_file.write(response.read())
83 def save_keys_to_files(keys=None, pub_file_path=None, priv_file_path=None):
85 Saves the generated RSA generated keys to the filesystem
86 :param keys: the keys to save generated by cryptography
87 :param pub_file_path: the path to the public keys
88 :param priv_file_path: the path to the private keys
93 pub_expand_file = os.path.expanduser(pub_file_path)
94 pub_dir = os.path.dirname(pub_expand_file)
96 if not os.path.isdir(pub_dir):
101 public_handle = open(pub_expand_file, 'wb')
102 public_bytes = keys.public_key().public_bytes(
103 serialization.Encoding.OpenSSH,
104 serialization.PublicFormat.OpenSSH)
105 public_handle.write(public_bytes)
108 public_handle.close()
110 os.chmod(pub_expand_file, 0o400)
111 logger.info("Saved public key to - " + pub_expand_file)
114 priv_expand_file = os.path.expanduser(priv_file_path)
115 priv_dir = os.path.dirname(priv_expand_file)
116 if not os.path.isdir(priv_dir):
119 private_handle = None
121 private_handle = open(priv_expand_file, 'wb')
122 private_handle.write(
124 encoding=serialization.Encoding.PEM,
125 format=serialization.PrivateFormat.TraditionalOpenSSL,
126 encryption_algorithm=serialization.NoEncryption()))
129 private_handle.close()
131 os.chmod(priv_expand_file, 0o400)
132 logger.info("Saved private key to - " + priv_expand_file)
135 def save_string_to_file(string, file_path, mode=None):
138 :param string: the string contents to store
139 :param file_path: the file path to create
140 :param mode: the file's mode
141 :return: the file object
143 save_file = open(file_path, 'w')
145 save_file.write(string)
147 os.chmod(file_path, mode)
153 def get_content_length(url):
155 Returns the number of bytes to be downloaded from the given URL
156 :param url: the URL to inspect
157 :return: the number of bytes
159 response = __get_url_response(url)
160 return response.headers['Content-Length']
163 def __get_url_response(url):
165 Returns a response object for a given URL
167 :return: the response
169 proxy_handler = urllib.ProxyHandler({})
170 opener = urllib.build_opener(proxy_handler)
171 urllib.install_opener(opener)
172 return urllib.urlopen(url)
175 def read_yaml(config_file_path):
177 Reads the yaml file and returns a dictionary object representation
178 :param config_file_path: The file path to config
179 :return: a dictionary
181 logger.debug('Attempting to load configuration file - ' + config_file_path)
184 with open(config_file_path, 'r') as config_file:
185 config = yaml.safe_load(config_file)
186 logger.info('Loaded configuration')
190 logger.info('Closing configuration file')
194 def persist_dict_to_yaml(the_dict, file_name):
196 Creates a YAML file from a dict
197 :param the_dict: the dictionary to store
198 :param conf_dir: the directory used to store the config file
199 :return: the file object
201 logger.info('Persisting %s to [%s]', the_dict, file_name)
202 file_path = os.path.expanduser(file_name)
203 yaml_from_dict = yaml.dump(
204 the_dict, default_flow_style=False, default_style='')
205 return save_string_to_file(yaml_from_dict, file_path)
208 def read_os_env_file(os_env_filename):
210 Reads the OS environment source file and returns a map of each key/value
211 Will ignore lines beginning with a '#' and will replace any single or
212 double quotes contained within the value
213 :param os_env_filename: The name of the OS environment file to read
214 :return: a dictionary
217 logger.info('Attempting to read OS environment file - %s',
222 env_file = open(os_env_filename)
223 for line in env_file:
225 if not line.startswith('#') and line.startswith('export '):
226 line = line.lstrip('export ').strip()
227 tokens = line.split('=')
229 # Remove leading and trailing ' & " characters from
231 out[tokens[0]] = tokens[1].lstrip('\'').lstrip('\"').rstrip('\'').rstrip('\"')
238 def read_file(filename):
240 Returns the contents of a file as a string
241 :param filename: the name of the file
247 the_file = open(filename)
248 for line in the_file: