def make_dirs(self): """ Creates directories for direnv installation and backup of existing config. :return: (int) returns 0 if directory exists at end of operation, else 1 """ try: os.makedirs(self.paths.backupspath) except FileNotFoundError as excep: toolkit.print_exceptions(error=excep, switch=switch) # print('FileNotFoundError: {}\t{}'.format(excep, self.paths.backupspath)) except PermissionError as excep: toolkit.print_exceptions(error=excep, switch=switch) toolkit.change_permissions(self.paths.backupspath, topdown=False, initial_exception=excep) except FileExistsError as excep: frontend_msg = "Existing directory {} used.".format( self.paths.backupspath) toolkit.print_exceptions(error=excep, frontend=frontend_msg, switch=switch) except Exception as excep: frontend_msg = 'UnforeseenError: {}\t{}'.format( excep, self.paths.backupspath) toolkit.print_exceptions(error=excep, frontend=frontend_msg, switch=switch) raise excep if os.path.exists(self.paths.backupspath): return 0 else: return 1
def move_binary(self, force: (True, 'ask', False) = False): exceptions_list = [] try: shutil.move(self.paths.copiedfile, self.paths.installationpath) except FileNotFoundError as excep: errors = [] frontend_msg = "Not found: {}".format(self.paths.copiedfile) toolkit.print_exceptions(error=excep, frontend=frontend_msg, switch=switch, errors=errors) self.change_paths(copiedfile=input("Enter correct path:")) except PermissionError as excep: errors = [] toolkit.print_exceptions(error=excep, switch=switch, errors=errors) toolkit.change_permissions(self.paths.installationpath) except (FileExistsError, shutil.Error) as excep: errors = [] file, loc = os.path.split(self.paths.copiedfile) frontend_msg = "{} already exists at {}".format(file, loc) toolkit.print_exceptions(error=excep, frontend=frontend_msg, switch=switch, errors=errors) if force == 'ask': print( "Note: The replacing and existing versions maybe different." ) replace = input("Replace? (y/n)") replace = True if replace.lower == 'y' or 'yes' or 'true' else False if not replace: return elif force: pass else: return # except as excep: # exceptions_list.append(excep) # print(excep) # raise shutil.Error except Exception as excep: exceptions_list.append(excep) print("Unforeseen Error.", '\n', excep) raise excep finally: os.chmod(self.paths.copiedfile, 0o777) os.remove(self.paths.copiedfile) try: shutil.move(self.paths.copiedfile, self.paths.installationpath) except Exception as excep: frontend_msg = ( "Move failed: {}. \n Please ensure the location {} " "has the file {}".format( excep, *os.path.split(self.paths.installedfile))) toolkit.print_exceptions(error=excep, frontend=frontend_msg, switch=switch)
def copy_move_binary(self, task: ('both', 'copy', ' move') = 'both', force: (True, 'ask', False) = False): """ :param force: (bool, str) args: False(default), 'ask', True : Reperforms the operation on error, or gives user the option to do so. :param task: (str) args: 'both'(default), 'copy', 'move' : Specifies the functions action """ copy_move_fn = { 'copy': { 'fn': shutil.copy2, 'args': (self.paths.setupfile, self.paths.copiedfile) }, 'move': { 'fn': shutil.move, 'args': (self.paths.copiedfile, self.paths.installationpath) } } exceptions_list = [] tasks = ['copy', 'move'] tasks = tasks if task not in tasks else [task] for task_ in tasks: try: copy_move_fn[task_]['fn'](*copy_move_fn[task_]['args']) except FileNotFoundError as excep: exceptions_list.append(excep) print("Not found: ", copy_move_fn[task_]['args'][0]) copy_move_fn[task_]['args'][0] = input("Enter correct path:") except PermissionError as excep: exceptions_list.append(excep) toolkit.change_permissions(copy_move_fn[task_]['args'][1]) except FileExistsError as excep: exceptions_list.append(excep) if force == 'ask': print( excep, "Type 'force' (without quotes) and press enter to replace, ", "or press enter to skip.", "The replaced and existing versions maybe different.", sep='\n') else: print(self.paths.copiedfile, "already exists.") return toolkit.check_remove(self.paths.copiedfile) copy_move_fn[task_]['fn'](*copy_move_fn[task_]['args']) except Exception as excep: exceptions_list.append(excep) raise excep finally: try: copy_move_fn[task_]['fn'](*copy_move_fn[task_]['args']) except shutil.Error as excep: print(excep) return exceptions_list
def backup_path_var(sub_shell, msg=True): BackupError = collections.namedtuple('BackupError', 'path_var') curr_path_info = os.environ.get('PATH') backup_dst = os.path.join(sub_shell.paths.backupspath, 'PATH_var.txt') try: with open(backup_dst, 'w') as write_obj: write_obj.write(curr_path_info) except PermissionError as excep: exceptions_list = [excep] toolkit.check_make(sub_shell.paths.backupspath) toolkit.change_permissions(sub_shell.paths.backupspath, nix_perm='0o666') shutil.copy2(shell['file'], sub_shell.paths.backupspath) else: return BackupError(path_var=0)
def test_copy_binary(self): print('test_copy_binary') try: self.assertFalse(os.path.exists(self.subshell.paths.installedfile)) except Exception as excep: toolkit.change_permissions(self.subshell.paths.installedfile) os.remove(self.subshell.paths.installedfile) # raise excep exceptions_list = self.subshell.copy_move_binary(max_attempts=2) try: self.assertTrue(os.path.exists(self.subshell.paths.installedfile)) except AssertionError: print(exceptions_list) raise AssertionError pass
def test_make_dirs(self): print('test_make_dirs') # try: # self.assertFalse(os.path.exists(self.subshell.paths.installationpath)) # except AssertionError: # toolkit.cleanup_tree(os.path.split(self.subshell.paths.installationpath)[0], handle_exceptions=False) # finally: # self.assertFalse(os.path.exists(self.subshell.paths.installationpath)) toolkit.change_permissions(os.path.split( self.paths.installationpath)[0], handle_exceptions=False, notify_init=True) toolkit.cleanup_tree(os.path.split(self.paths.installationpath)[0], handle_exceptions=False, notify_init=True, notify_outcome=True) self.subshell.make_dirs() self.assertTrue(os.path.exists(self.paths.installationpath)) self.assertTrue(os.path.exists(self.paths.backupspath))
def backup_shell_config(shell, sub_shell, handle_excep=True): BackupError = collections.namedtuple('BackupError', 'shell_config exception file') err_log = [] not_found_files = [] for file_ in shell['files']: try: shutil.copy2(file_, sub_shell.paths.backupspath) except PermissionError as excep: toolkit.change_permissions(sub_shell.paths.installationpath, '0o666') shutil.copy2(file_, sub_shell.paths.backupspath) err_log.append(BackupError(shell_config=1, exception=excep, file=file_)) except FileNotFoundError as excep: err_log.append(BackupError(shell_config=1, exception=excep, file=file_)) not_found_files.append(file_) toolkit.print_exceptions(error=excep, switch=switch) else: err_log.append(BackupError(shell_config=0, exception=None, file=file_)) if not_found_files: print("These files were not found at the specified location and were not backed up:") pprint(not_found_files) return err_log