|
| 1 | +import hashlib |
| 2 | +import os |
| 3 | +from os.path import realpath, join as opj, sep as pathsep |
| 4 | +import sys |
| 5 | +from configparser import ConfigParser |
| 6 | + |
| 7 | + |
| 8 | +def attempt_load_config(): |
| 9 | + """ |
| 10 | + tries to load config file from expected path in instances where neither a |
| 11 | + filepath or dict-like object is provided |
| 12 | + """ |
| 13 | + splitpath = realpath(__file__).split(pathsep) |
| 14 | + try: |
| 15 | + try: |
| 16 | + # get path to project root directory |
| 17 | + splitroot = splitpath[: splitpath.index('cluster-tools-dartmouth') + 1] |
| 18 | + project_root = pathsep.join(splitroot) |
| 19 | + config_dir = opj(project_root, 'configs') |
| 20 | + except ValueError as e: |
| 21 | + # pass exceptions onto broad outer exception for function |
| 22 | + raise FileNotFoundError(f"cluster-tools-dartmouth not found in path\ |
| 23 | + {realpath(__file__)}").with_traceback(e.__traceback__) |
| 24 | + |
| 25 | + configs = os.listdir(config_dir) |
| 26 | + # filter out hidden files and the template config |
| 27 | + configs = [f for f in configs if not (f.startswith('template') or f.startswith('.'))] |
| 28 | + if len(configs) == 1: |
| 29 | + config_path = opj(config_dir, configs[0]) |
| 30 | + config = parse_config(config_path) |
| 31 | + return config |
| 32 | + else: |
| 33 | + # fail if multiple or no config files are found |
| 34 | + raise FileNotFoundError(f"Unable to determine which config file to \ |
| 35 | + read from {len(configs)} choices in {config_dir}") |
| 36 | + |
| 37 | + except FileNotFoundError as e: |
| 38 | + raise FileNotFoundError("Failed to load config file from expected \ |
| 39 | + location").with_traceback(e.__traceback__) |
| 40 | + |
| 41 | + |
| 42 | +def md5_checksum(filepath): |
| 43 | + """ |
| 44 | + computes the MD5 checksum of a local file to compare against remote |
| 45 | +
|
| 46 | + NOTE: MD5 IS CONSIDERED CRYPTOGRAPHICALLY INSECURE |
| 47 | + (see https://en.wikipedia.org/wiki/MD5#Security) |
| 48 | + However, it's still very much suitable in cases (like ours) where one |
| 49 | + wouldn't expect **intentional** data corruption |
| 50 | + """ |
| 51 | + hash_md5 = hashlib.md5() |
| 52 | + with open(filepath, 'rb') as f: |
| 53 | + # avoid having to read the whole file into memory at once |
| 54 | + for chunk in iter(lambda: f.read(4096), b''): |
| 55 | + hash_md5.update(chunk) |
| 56 | + return hash_md5.hexdigest() |
| 57 | + |
| 58 | + |
| 59 | +def parse_config(config_path): |
| 60 | + """ |
| 61 | + parses various user-specifc options from config file in configs dir |
| 62 | + """ |
| 63 | + raw_config = ConfigParser(inline_comment_prefixes='#') |
| 64 | + with open(config_path, 'r') as f: |
| 65 | + raw_config.read_file(f) |
| 66 | + |
| 67 | + config = dict(raw_config['CONFIG']) |
| 68 | + config['confirm_overwrite_on_upload'] = raw_config.getboolean( |
| 69 | + 'CONFIG', 'confirm_overwrite_on_upload' |
| 70 | + ) |
| 71 | + return config |
| 72 | + |
| 73 | + |
| 74 | +def prompt_input(question, default=None): |
| 75 | + """ |
| 76 | + given a question, prompts user for command line input |
| 77 | + returns True for 'yes'/'y' and False for 'no'/'n' responses |
| 78 | +
|
| 79 | + """ |
| 80 | + assert default in ('yes', 'no', None), \ |
| 81 | + "Default response must be either 'yes', 'no', or None" |
| 82 | + |
| 83 | + valid_responses = { |
| 84 | + 'yes': True, |
| 85 | + 'y': True, |
| 86 | + 'no': False, |
| 87 | + 'n': False |
| 88 | + } |
| 89 | + |
| 90 | + if default is None: |
| 91 | + prompt = "[y/n]" |
| 92 | + elif default == 'yes': |
| 93 | + prompt = "[Y/n]" |
| 94 | + else: |
| 95 | + prompt = "[y/N]" |
| 96 | + |
| 97 | + while True: |
| 98 | + sys.stdout.write(f"{question}\n{prompt}") |
| 99 | + response = input().lower() |
| 100 | + # if user hits return without typing, return default response |
| 101 | + if (default is not None) and (not response): |
| 102 | + return valid_responses[default] |
| 103 | + elif response in valid_responses: |
| 104 | + return valid_responses[response] |
| 105 | + else: |
| 106 | + sys.stdout.write("Please respond with either 'yes' (or 'y') \ |
| 107 | + or 'no' (or 'n')\n") |
0 commit comments