import os from functools import partial def mod_time(path): return os.stat(path).st_mtime def find_newest_cert(root_dir, hostname): look_for = { "{}.key.pem".format(hostname): None, "{}.chain.pem".format(hostname): None, } for root, _, files in os.walk(root_dir): for file in files: if file in look_for.keys(): fullpath = os.path.join(root, file) if look_for[file] is None: look_for[file] = fullpath elif mod_time(fullpath) > mod_time(look_for[file]): look_for[file] = fullpath return look_for def ensure_keys(key_dir, hostname): files = (part.format(hostname) for part in ("{}.key.pem", "{}.chain.pem"))