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 from cryptography.hazmat.primitives import serialization
21 import urllib.request as urllib
23 import urllib2 as urllib
27 __author__ = 'spisarski'
30 Utilities for basic file handling
33 logger = logging.getLogger('file_utils')
36 def file_exists(file_path):
38 Returns True if the image file already exists and throws an exception if
39 the path is a directory
42 expanded_path = os.path.expanduser(file_path)
43 if os.path.exists(expanded_path):
44 if os.path.isdir(expanded_path):
46 return os.path.isfile(expanded_path)
50 def download(url, dest_path, name=None):
52 Download a file to a destination path given a URL
53 :param url: the endpoint to the file to download
54 :param dest_path: the directory to save the file
55 :param name: the file name (optional)
59 name = url.rsplit('/')[-1]
60 dest = dest_path + '/' + name
61 logger.debug('Downloading file from - ' + url)
62 # Override proxy settings to use localhost to download file
65 if not os.path.isdir(dest_path):
71 with open(dest, 'wb') as download_file:
72 logger.debug('Saving file to - %s',
73 os.path.abspath(download_file.name))
74 response = __get_url_response(url)
75 download_file.write(response.read())
82 def save_keys_to_files(keys=None, pub_file_path=None, priv_file_path=None):
84 Saves the generated RSA generated keys to the filesystem
85 :param keys: the keys to save generated by cryptography
86 :param pub_file_path: the path to the public keys
87 :param priv_file_path: the path to the private keys
92 pub_expand_file = os.path.expanduser(pub_file_path)
93 pub_dir = os.path.dirname(pub_expand_file)
95 if not os.path.isdir(pub_dir):
100 public_handle = open(pub_expand_file, 'wb')
101 public_bytes = keys.public_key().public_bytes(
102 serialization.Encoding.OpenSSH,
103 serialization.PublicFormat.OpenSSH)
104 public_handle.write(public_bytes)
107 public_handle.close()
109 os.chmod(pub_expand_file, 0o400)
110 logger.info("Saved public key to - " + pub_expand_file)
113 priv_expand_file = os.path.expanduser(priv_file_path)
114 priv_dir = os.path.dirname(priv_expand_file)
115 if not os.path.isdir(priv_dir):
118 private_handle = None
120 private_handle = open(priv_expand_file, 'wb')
121 private_handle.write(
123 encoding=serialization.Encoding.PEM,
124 format=serialization.PrivateFormat.TraditionalOpenSSL,
125 encryption_algorithm=serialization.NoEncryption()))
128 private_handle.close()
130 os.chmod(priv_expand_file, 0o400)
131 logger.info("Saved private key to - " + priv_expand_file)
134 def save_string_to_file(string, file_path, mode=None):
137 :param string: the string contents to store
138 :param file_path: the file path to create
139 :param mode: the file's mode
140 :return: the file object
142 save_file = open(file_path, 'w')
144 save_file.write(string)
146 os.chmod(file_path, mode)
152 def get_content_length(url):
154 Returns the number of bytes to be downloaded from the given URL
155 :param url: the URL to inspect
156 :return: the number of bytes
158 response = __get_url_response(url)
159 return response.headers['Content-Length']
162 def __get_url_response(url):
164 Returns a response object for a given URL
166 :return: the response
168 proxy_handler = urllib.ProxyHandler({})
169 opener = urllib.build_opener(proxy_handler)
170 urllib.install_opener(opener)
171 return urllib.urlopen(url)
174 def read_yaml(config_file_path):
176 Reads the yaml file and returns a dictionary object representation
177 :param config_file_path: The file path to config
178 :return: a dictionary
180 logger.debug('Attempting to load configuration file - ' + config_file_path)
183 with open(config_file_path) as config_file:
184 config = yaml.safe_load(config_file)
185 logger.info('Loaded configuration')
189 logger.info('Closing configuration file')
193 def read_os_env_file(os_env_filename):
195 Reads the OS environment source file and returns a map of each key/value
196 Will ignore lines beginning with a '#' and will replace any single or
197 double quotes contained within the value
198 :param os_env_filename: The name of the OS environment file to read
199 :return: a dictionary
202 logger.info('Attempting to read OS environment file - %s',
207 env_file = open(os_env_filename)
208 for line in env_file:
210 if not line.startswith('#') and line.startswith('export '):
211 line = line.lstrip('export ').strip()
212 tokens = line.split('=')
214 # Remove leading and trailing ' & " characters from
216 out[tokens[0]] = tokens[1].lstrip('\'').lstrip('\"').rstrip('\'').rstrip('\"')
223 def read_file(filename):
225 Returns the contents of a file as a string
226 :param filename: the name of the file
232 the_file = open(filename)
233 for line in the_file: